internal static void SetValue(string val, IntPtr hwnd, int item) { // PerSharp/PreFast will flag this as warning 6507/56507: Prefer 'string.IsNullOrEmpty(val)' over checks for null and/or emptiness. // An empty strings is valued here, while a null string is not. // Therefore we can not use IsNullOrEmpty() here, suppress the warning. #pragma warning suppress 6507 if (val == null) { throw new ArgumentNullException("val"); } if (!WindowsListView.ListViewEditable(hwnd)) { throw new InvalidOperationException(SR.Get(SRID.ValueReadonly)); } Misc.SetFocus(hwnd); // set focus to the item WindowsListView.SetItemFocused(hwnd, item); // retrieve edit window IntPtr hwndEdit = WindowsListView.ListViewEditLabel(hwnd, item); if (IntPtr.Zero == hwndEdit) { throw new InvalidOperationException(SR.Get(SRID.OperationCannotBePerformed)); } // re-use editbox proxy to do the job // Note: we will pass null and -1 for some of the parameters // this is ok, since the only thing that matters to us is an ability to set the text // and these parameters irrelevant for our task WindowsEditBox editBox = new WindowsEditBox(hwndEdit, null, -1); // get value pattern IValueProvider valueProvider = editBox.GetPatternProvider(ValuePattern.Pattern) as IValueProvider; // try to set user-provided text bool setValueSucceeded = false; try { valueProvider.SetValue(val); setValueSucceeded = true; } finally { // even if there is a problem doing SetValue need to exit // editing mode (e.g. cleanup from editing mode). FinishEditing(setValueSucceeded, hwnd, hwndEdit); } // Need to give some time for the control to do all its processing. bool wasTextSet = false; for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(1); // Now see if the item really got set. if (val.Equals(ListViewItem.GetText(hwnd, item, 0))) { wasTextSet = true; break; } } if (!wasTextSet) { throw new InvalidOperationException(SR.Get(SRID.OperationCannotBePerformed)); } }