haan jae

haan jae

  • NA
  • 22
  • 27.4k

C# argument out of range was unhandled due to the SelectedIndex

Dec 3 2011 3:48 AM
'Argument out of range was unhandled' error occur when button is clicked due to no number or no such index number of client connected is selected. Error show to this code:

workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];

i want to give a message box say 'Please select a correct number'. what should be coded to handle this error?

here is the code:

void indmsgbtn_Click(object sender, EventArgs e)
{
string msg = richtxtindmsg.Text;
msg = "Private Admin Message: " + msg + "\n";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
Socket workerSocket = null;
workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];

if (workerSocket != null)
{
if (workerSocket.Connected)
{
workerSocket.Send(byData);
}

}

}


Answers (7)