C#: How to let Click event by calling from somewhere else?

VAer-4038 776 Reputation points
2021-01-03T17:07:22.157+00:00

Let us say, I have below code in one child form, but other forms have same combobox, and they need the same code. So how can I write something in class, and how to call it in comboBoxCountry_Click.

Let us say, I would like to write something GenerateDropdownListForCountry in GlobalCombo.cs, then calling GlobalCombo.GenerateDropdownListForCountry

How to write the syntax?

Or how to call a button_click in another form?

Thanks.

   private void comboBoxCountry_Click(object sender, EventArgs e)
          {
              comboBoxCountry.Items.Clear();

              OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);
              Cn.Open();

              string qCountry = "SELECT DISTINCT Country from Place";

              using (var cmd = new OdbcCommand(qCountry, Cn))
              {
                  using (var reader = cmd.ExecuteReader())
                  {
                      while (reader.Read())
                      {
                          string sColString = reader.GetString(0);
                          comboBoxCountry.Items.Add(sColString);
                      }
                  }
              }

              Cn.Close();


          }
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Dylan Zhu-MSFT 6,431 Reputation points
    2021-01-04T08:10:21.56+00:00

    Hi VAer,

    >how to call a button_click in another form?

    You can set the click event from private to public

        public void comboBoxCountry_Click(object sender, EventArgs e)  
               {  
                         .......  
               }  
    

    Then you could call it in another form:
    53241-image.png

    Note:

    But it may cause other problems, I suggest you create a public class for your shared methods.

    Best Regards, Dylan

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our * *documentation* to enable e-mail notifications if you want to receive the related email notification for this thread.**


1 additional answer

Sort by: Most helpful
  1. VAer-4038 776 Reputation points
    2021-01-05T03:56:14.323+00:00

    Thanks. I am actually dealing more validating textbox input character, and it has messagebox. It seems messagebox is not allowed in class.

    Anyway, for your code example, can Form2 comboBox1_Click still work if Form1 is closed?

    Thanks.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.