Other forms are assigned to the ComboBox and the assigned value is selected (Gets selectedindex based on text)

Source: Internet
Author: User

Form1 This drop-down box for the shipping unit ComboBox1 has the name field of the Database test table already bound, with a lot of organization names

For example: A company, B company ...

1, Form1 ComboBox1 first bind the Database data table test

using(Sqliteconnection con =Newsqliteconnection (data_source)) {con.                Open (); using(Sqlitecommand cmd =NewSqlitecommand ()) {cmd. Connection=con; Cmd.commandtext=string. Format ("SELECT * FROM Test t"); introws =cmd.                    ExecuteNonQuery (); Sqlitedataadapter SDA=Newsqlitedataadapter (CMD); DataSet DS=NewDataSet (); Sda.                    Fill (DS); DataTable DT= ds. tables[0];  This. Combobox1.datasource =DT; //Delete DelegateCombobox1.selectedindexchanged-=NewEventHandler (combobox1_selectedindexchanged);//This is superfluous. This. Combobox1.displaymember ="name";//drop-down box to display the fields of the database This. Combobox1.valuemember ="ID"; The ID of the table that corresponds to the field of the displayed database This. Combobox1.selectedindex =-1;//First comboBox1 shown as empty//Add DelegateCombobox1.selectedindexchanged + =NewEventHandler (combobox1_selectedindexchanged);This is superfluous.
} }

2. Form Form2 The drop-down box to assign values to the form Form1:

Let this drop-down box property modifies to public, in Form2 FHDW this variable to Form1 drop-down box

Form1 z = new Form1 (); z.combobox1.text = FHDW;

Form2 assignment has been completed, can be seen in the Form1, the drop-down box has been shown to have a value, but only has a value, and is not in the selected state, you cannot use it SelectedValue

3. How to make this value selected after assignment:

In Form1 's comboBox1 has been bound to many units, looking for this passed over the text value,

stringFHDW =""; intFhdw_index = This. combobox1.findstring (Combobox1.text);//return 0 after finding This. Combobox1.selectedindex =Fhdw_index; SelectedIndex equals 0, which is the equivalent of selectingif(Combobox1.selectedindex = =-1)//Verify that you have selected {MessageBox.Show ("Please select a shipping unit"); return; } FHDW= ComboBox1.SelectedValue.ToString ();//Because it is selected, it can be used SelectedValue

Other forms are assigned to the ComboBox and the assigned value is selected (Gets selectedindex based on text)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.