I have two tabPages with the ListView and PropertyGrid on each. Every object from the MyListView on the tabPage1 has the field with the name of an object from the MyListView2. When I MouseClick at this field I would like to select tabPage2, then find and select the required object.
I was glad to get the further code work properly in the debug mode. Though when I tried to launch the executable file in the debug folder, I've got "Object reference not set to an instance of an object" exception after the MouseClick at the required field. I guess, this is the aftermath of using ActiveControl inside the PrintPropertyGrid() method, though I have had no idea yet how to realize it the other way.
How to get rid of that exception?
private void MyListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (MyListView.IsHandleCreated)
if (MyListView.SelectedIndices.Count > 0)
PrintPropertyGrid(MyListView.SelectedIndices[0]);
}
private void PrintPropertyGrid(int index)
{
MyPropertyGrid.SelectedObject = MyList[index];
foreach (Control c in MyPropertyGrid.ActiveControl.Controls)
c.MouseClick += new MouseEventHandler(propertyGrid_MouseClick);
}
private void propertyGrid_MouseClick(object sender, MouseEventArgs e)
{
if (MyPropertyGrid.SelectedGridItem.PropertyDescriptor.Name.Equals("Conversion"))
{
string name;
name = MyPropertyGrid.SelectedGridItem.Value.ToString();
tabControl1.SelectedTab = tabPage2;
if (!String.IsNullOrWhiteSpace(name))
{
ListViewItem foundItem = MyListView2.FindItemWithText(name, false, 0, true);
if (foundItem != null)
foundItem.Selected = true;
}
}
}
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at myProject.FormMain.PrintPropertyGrid(Int32 selector, PropertyGrid propertyGrid, Int32 index) in c:\Users\Admin\Documents\Visual Studio2012\Projects\myProject\myProject\FormMain.cs:line 96
at myProject.FormMain.MyListView2_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e) in c:\Users\Admin\Documents\Visual Studio2012\Projects\myProject\myProject\FormMain.cs:line 286
at System.Windows.Forms.ListView.OnItemSelectionChanged(ListViewItemSelectionChangedEventArgs e)
at System.Windows.Forms.ListView.WmReflectNotify(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)