Dynamic-usercontrol click event

2019-06-01 02:22发布

UserControl:

private string lastName;
public string LastName
{
get { return lastName; }
set
{
    lastName = value;
    lastNameTextBox.Text = value;
}
}

Form:

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LasatName from Employee", myDatabaseConnection))
            {
                int i = 0;
                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                while (DR1.Read())
                {
                    i++;
                    UserControl2 usercontrol = new UserControl2();
                    usercontrol.Tag = i;
                    usercontrol.LastName = (string)DR1["LastName"];
                    usercontrol.Click += new EventHandler(usercontrol_Click); 
                    flowLayoutPanel1.Controls.Add(usercontrol);
                }
            }
        }

The form loads the records from database and display each LastName in each usercontrols' textbox that created dynamically. How to show additonal information such as address in form's textbox when a dynamic-usercontrol is click?

Attempt:

    private void usercontrol_Click(object sender, EventArgs e)
    {
        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand mySqlCommand = new SqlCommand("Select Address from Employee where LastName = @LastName ", myDatabaseConnection))

            {
                UserControl2 usercontrol = new UserControl2();
                mySqlCommand.Parameters.AddWithValue("@LastName", usercontrol.LastName;
                SqlDataReader sqlreader = mySqlCommand.ExecuteReader();

                if (sqlreader.Read())
                {
                    textBox1.Text = (string)sqlreader["Address"];
                }

            }
        }
    }

2条回答
啃猪蹄的小仙女
2楼-- · 2019-06-01 02:24

Inside your user control UserControl2 (in the constructor or InitializeComponent method) you should forward the click event to the potential listeners.

Something like that:

public UserControl2()
{
    ...
    this.lastNameTextBox.Click += (s, a) => OnClick(a);
    ...
}
查看更多
【Aperson】
3楼-- · 2019-06-01 02:32

First. In your first code fragment, you execute "Select ID from ...", but expect to find field "LastName" in the reader - not going to work.

Second. If you know that you will need more information from Employee table, I suggest that you store it in the same UserControl where you put the LastName. Execute "Select * from ...", add another field and property to UserControl2 and assign it in the Read loop:

usercontrol.Address = (string)DR1["Address"];

Third. In your second code fragment, use

UserControl2 usercontrol = (UserControl2)sender;

instead of

UserControl2 usercontrol = new UserControl2();

because newly created user control will not have any LastName assigned.

Then:

private void usercontrol_Click(object sender, EventArgs e)
{
    UserControl2 usercontrol = (UserControl2)sender;
    textBox1.Text = usercontrol.Address;
}
查看更多
登录 后发表回答