views:

328

answers:

3

Hi,

I want to find an Index of a combobox using numbers in the text of a textbox, and then remove them. The items that populate the combobox belong to a data base so I use the Delete method to remove the rows.

EDITED:

I've been reading and the findstring finds text within the item list, not the index. Is there anyway to look for the text in the textbox in the index of the combobox?

Can anyone find the problem with this code?

private void button4_Click(object sender, EventArgs e)
    {
        int buscar;
        buscar = comboBox1.FindStringExact(tNumEditBox3.Text, 0);

        comboBox1.SelectedIndex = buscar;

        if (comboBox1.SelectedIndex >= 0 && radioButton1.Checked == true)
        {
                CambiosEnviosDataSet.CambioGRow borrarCambioGFila;
                borrarCambioGFila = cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text));

                borrarCambioGFila.Delete();

                this.cambioGTableAdapter.Update(this.cambiosEnviosDataSet.CambioG);

                CambiosEnviosDataSet.CambioERow borrarCambioEFila;
                borrarCambioEFila = cambiosEnviosDataSet.CambioE.FindByCambioEID(Convert.ToInt16(tNumEditBox3.Text));

                borrarCambioEFila.Delete();

                this.cambioETableAdapter.Update(this.cambiosEnviosDataSet.CambioE);
        }
        else if (comboBox2.SelectedIndex <= 0 && radioButton2.Checked == true)
        {
                CambiosEnviosDataSet.EnviosRow borrarEnvioFila;
                borrarEnvioFila = cambiosEnviosDataSet.Envios.FindByEnvioID(Convert.ToInt16(tNumEditBox3.Text));

                borrarEnvioFila.Delete();

                this.enviosTableAdapter.Update(this.cambiosEnviosDataSet.Envios);
        }
        else 
        {
            MessageBox.Show("The key you are using is not in the index");
        }
    }
A: 

A couple of things spring to mind.

It's either that the value in tNumEditBox3.Text isn't a value present in the combo box. Have you double checked it's value before the call to:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text, 0);

The other alternative is that radioButton2.Checked is false.

BTW you don't need to explicitly test a boolean value against true or false. You can just write:

if (boolean_value)
{
    // Do stuff
}
ChrisF
A: 

Your call to FindStringExact will skip the first item. Unless you want it to only search the items after the first one, you should use the overload that doesn't take a startIndex parameter, like this:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text);

If that isn't you problem, check that the text in the textbox exactly matches one of the items in the combobox, and make sure that radioButton1 is checked.

SLaks
It is checked, I've been reading and the findstring finds text within the item list, not the index. Is there anyway to look for the text in the textbox in the index of the combobox?
Ricardo
A: 

As I understand (correct me if I'm wrong), your textbox has the ID of an item in the combobox (eg, 3).

You need to find the item that has that ID, then set the SelectedItem proeprty of the combobox, like this:

comboBox1.SelectedItem = 
    cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text));
SLaks
Yes, it has de ID of and item in the combobox, I tried the code you wrote but it didn't work. I also tried with SelectedValue but it said that Cannot convert and object of type 'CambioGRow' to 'System.IConvertible'.
Ricardo