Try this:
public void button1_Click(object sender, EventArgs e)
{
try
{
int searchnums = Convert.ToInt32(textBox2.Text);
int result = binarysearch(searchnums);
MessageBox.Show(result.ToString());
}
catch (Exception ex)
{
}
}
or take steps to handle potential problems:
public void button1_Click(object sender, EventArgs e)
{
int searchnums;
int result = -1;
if (Int32.TryParse(textBox2.Text, out searchnums))
{
result = binarysearch(searchnums);
}
MessageBox.Show(result.ToString());
}