Andrew Martin

Andrew Martin

  • NA
  • 1
  • 1.5k

comboBox Selected item to clipboard

Jun 6 2014 3:31 PM

Good evening,

Can someone please advise me how I get around the exception I'm getting here?
I have four combo boxes with items in.  When I select one I get the following error.  I am using VS2013

 
System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=OMCAssist
  StackTrace:
       at OMCAssist.incLog.comboBox1_SelectedIndexChanged(Object sender, EventArgs e) in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\OMCAssist\OMCAssist\incLog.cs:line 33
       at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
       at OMCAssist.incLog.comboBox1_SelectedIndexChanged(Object sender, EventArgs e) in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\OMCAssist\OMCAssist\incLog.cs:line 34
       at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ComboBox.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException:
 
This is my code below.  I've been trying for the past week to find out where I've gone wrong.  Any help would be much appreciated.
Here is my code.


private void incLog_Load(object sender, EventArgs e)

{

}

public Object SelectedItem { get; set; }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

int selectedIndex = comboBox1.SelectedIndex;

Object selectedItem = comboBox1.SelectedItem;

Clipboard.SetText(selectedItem.ToString() + "\r\n");

comboBox1.SelectedIndex = -1;

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)

{

int selectedIndex = comboBox2.SelectedIndex;

Object selectedItem = comboBox2.SelectedItem;

Clipboard.SetText(selectedItem.ToString() + "\r\n");

comboBox2.SelectedIndex = -1;

}

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)

{

int selectedIndex = comboBox3.SelectedIndex;

Object selectedItem = comboBox3.SelectedItem;

Clipboard.SetText(selectedItem.ToString() + "\r\n");

comboBox3.SelectedIndex = -1;

}

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)

{

int selectedIndex = comboBox4.SelectedIndex;

Object selectedItem = comboBox4.SelectedItem;

Clipboard.SetText(selectedItem.ToString() + "\n");

comboBox4.SelectedIndex = -1;

}