c# - change comboBox values based on click i another -


i have 2 functions first 1 filling combobox tables within sql database, below:

   private void fillcombo()     {         combobox1.items.clear();         try         {              string connectionstring = "data source=lpmsw09000012jd\\sqlexpress;initial catalog=pharmacies;integrated security=true";             using (sqlconnection con2 = new sqlconnection(connectionstring))             {                 con2.open();                 string query = "select * information_schema.tables ";                 sqlcommand cmd2 = new sqlcommand(query, con2);                  sqldatareader dr2 = cmd2.executereader();                 while (dr2.read())                 {                     int col = dr2.getordinal("table_name");                     combobox1.items.add(dr2[col].tostring());                 }                 combobox1.selectedindex = 0;             }         }         catch (exception ex)         {             messagebox.show(ex.tostring());         }     } 

and have 1 filling second combobox based on value previous function's combobox. below

    async void fillliguanea()     {          combobox2.items.clear();              try             {                  string connectionstring = "data source=lpmsw09000012jd\\sqlexpress;initial catalog=pharmacies;integrated security=true";                 sqlconnection con = new sqlconnection(connectionstring);                 con.open();                 string query = "select * " + combobox1.text;                 sqlcommand cmd = new sqlcommand(query, con);                 var reader = await cmd.executereaderasync();                 combobox2.beginupdate();                 while (reader.read())                 {                     string scode = reader.getstring(reader.getordinal("code"));                     combobox2.items.add(scode);                 }                 combobox2.endupdate();                 combobox2.selectedindex = 0;              }             catch (exception ex)             {                  messagebox.show(ex.tostring());             }} 

what trying refresh "fillliguanea" function's combobox value based on table selected in "fillcombo" function. eg. if there tables name "cat" , "dog" in combobox filled "fillliguanea" when selected should automatically change combobox filled "fillliguanea" various cat or dog breeds.

i reading , saw selectionchangecommitted event. way go or there better way this?

i've achieved through refresh button targets second combobox rather eliminate use of buttons user

you have use events.

see: https://msdn.microsoft.com/en-us/library/awbftdfh.aspx, https://msdn.microsoft.com/en-us/library/edzehd2t(v=vs.110).aspx

in short, event way trigger code following observer pattern.

basically, event represents multiple methods (i'll refer these "subscribing methods" "subscribe" event) called when event raised within class defined in. "okay," might say. "but when implementing method called when event raised, how ensure have correct parameter types?". delegates.

a delegate represents signature of method (ie, number of parameters/the parameter types/the method's return type). each event declared being type of specific delegate.

for example, let's want event call it's subscribing methods when arbitrary message received. how setup such system using these things called "events"?

  1. define delegate

    public delegate void messagedelegate(string data);

    this means (and enforces) subscribing methods event must contain 1 parameter , it's type must string. each subscribing method must not return value.

  2. define event

    public event messagedelegate messagereceived;

    this says create event (a collection of subscribed methods) each subscribing method must match signature defined delegate messagedelegate.

  3. subscribe event (probably part you're interested in) subscribe event (or add method event's collection of subscribers) first must create method matches delegate:

    private void onmessagereceived(string msg) { //do received message. }

    then comes actual subscribing:

    messagereceived += new messagedelegate(onmessagereceived).

    now whenever event triggered our onmessagereceived method called.

  4. finally, trigger or raise event (the process of calling each subscribing method) can do: messagereceived("hello world!")

all being said, how apply particular problem? well, combobox control contains event. don't have define yourself. means you're responsible subscribing event.

combobox1.selectionchangecommitted += new eventhandler(combobox1_selectionchangecommitted);  private void combobox1_selectionchangecommitted(object sender, eventargs e) {     //refresh combobox 2. } 

i recommend put actual subscription in form's form_load event.

private void form1_load(object sender, eventargs e) {     combobox1.selectionchangecommitted += new eventhandler(combobox1_selectionchangecommitted); } 

now however, must subscribe form's load event. typically done in constructor after call initializecomponent.

public form1() {     initializecomponent();     load += new eventhandler(form1_load); } 

you can of course, bypass form load subscription

public form1() {     initializecomponent();     combobox1.selectionchangecommitted += new eventhandler(combobox1_selectionchangecommitted); } 

lastly, if you're using windowsforms designer in visual studio: somewhere on property pane there should way view events selected control. scroll down desired event. double-click it. visual studio should automatically create method , subscribe event new method.

there power behind events , delegates. highly encourage reading on them. sorry wasn't small envisioned thought try explain why works , not "this how should done.".


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -