System.Windows.Forms.ListView.FindItemWithText C# (CSharp) Method

FindItemWithText() private method

private FindItemWithText ( string text, bool includeSubItemsInSearch, int startIndex, bool isPrefixSearch, bool roundtrip ) : System.Windows.Forms.ListViewItem
text string
includeSubItemsInSearch bool
startIndex int
isPrefixSearch bool
roundtrip bool
return System.Windows.Forms.ListViewItem
		internal ListViewItem FindItemWithText (string text, bool includeSubItemsInSearch, int startIndex, bool isPrefixSearch, bool roundtrip)
		{
			if (startIndex < 0 || startIndex >= items.Count)
				throw new ArgumentOutOfRangeException ("startIndex");

			if (text == null)
				throw new ArgumentNullException ("text");

			if (virtual_mode) {
				SearchForVirtualItemEventArgs args = new SearchForVirtualItemEventArgs (true,
						isPrefixSearch, includeSubItemsInSearch, text, Point.Empty, 
						SearchDirectionHint.Down, startIndex);

				OnSearchForVirtualItem (args);
				int idx = args.Index;
				if (idx >= 0 && idx < virtual_list_size)
					return items [idx];

				return null;
			}

			int i = startIndex;
			while (true) {
				ListViewItem lvi = items [i];

				if (isPrefixSearch) { // prefix search
					if (CultureInfo.CurrentCulture.CompareInfo.IsPrefix (lvi.Text, text, CompareOptions.IgnoreCase))
					       	return lvi;
				} else if (String.Compare (lvi.Text, text, true) == 0) // match
					return lvi;

				if (i + 1 >= items.Count) {
					if (!roundtrip)
						break;

					i = 0;
				} else 
					i++;

				if (i == startIndex)
					break;
			}

			// Subitems have a minor priority, so we have to do a second linear search
			// Also, we don't need to to a roundtrip search for them by now
			if (includeSubItemsInSearch) {
				for (i = startIndex; i < items.Count; i++) {
					ListViewItem lvi = items [i];
					foreach (ListViewItem.ListViewSubItem sub_item in lvi.SubItems)
						if (isPrefixSearch) {
							if (CultureInfo.CurrentCulture.CompareInfo.IsPrefix (sub_item.Text, 
								text, CompareOptions.IgnoreCase))
								return lvi;
						} else if (String.Compare (sub_item.Text, text, true) == 0)
							return lvi;
				}
			}

			return null;
		}

Same methods

ListView::FindItemWithText ( string text ) : System.Windows.Forms.ListViewItem
ListView::FindItemWithText ( string text, bool includeSubItemsInSearch, int startIndex ) : System.Windows.Forms.ListViewItem
ListView::FindItemWithText ( string text, bool includeSubItemsInSearch, int startIndex, bool isPrefixSearch ) : System.Windows.Forms.ListViewItem

Usage Example

Example #1
0
        private Boolean AddItemToListView(ListView listView, String path)
        {
            if (null != listView.FindItemWithText(path))
            {
                return false;
            }

            ListViewItem lvi = new ListViewItem((listView.Items.Count + 1).ToString());

            lvi.SubItems.Add(""); // 완료 여부 컬럼 - 처음에는 빈칸을 넣는다.
            lvi.SubItems.Add(path); //파일 경로

            // 기본적으로 파일 생성날짜를 읽어온다.
            // Exif가 존재한다면 아래 로직에서 Exif에서 생성된 날짜를 읽어온다.
            FileInfo fileInfo = new FileInfo(path);
            String fileCreationTime = fileInfo.LastWriteTime.Date.ToString("yyyy-MM-dd").Substring(0, 10);

            // Exif 지원 확장자인 경우 Exif에서 생성된 날짜를 읽어온다.
            if (true == IsExifSupportExtension(path))
            {
                 String exifDate = GetExifDate(path);

                if (false == String.IsNullOrEmpty(exifDate))
                {
                    exifDate = exifDate.Substring(0, 10);
                    fileCreationTime = exifDate;
                }
            }

            lvi.SubItems.Add(fileCreationTime); // 생성 날짜

            long fileSize = fileInfo.Length;
            // 파일 크기
            if (0 == fileSize)
                lvi.SubItems.Add("0 KB");
            else if (1024 > fileSize)
                lvi.SubItems.Add("1 KB");
            else if (1048576 > fileSize)
                lvi.SubItems.Add(String.Format("{0:N}", (double)(fileSize / 1024)) + " KB");
            else if (1073741824 > fileSize)
                lvi.SubItems.Add(String.Format("{0:N}", (double)(fileSize / 1048576)) + " MB");
            else
                lvi.SubItems.Add(String.Format("{0:N}", (double)(fileSize / 1073741824)) + " GB");

            listView.Items.Add(lvi);
            return true;
        }
All Usage Examples Of System.Windows.Forms.ListView::FindItemWithText
ListView