【问题标题】:Dynamic-usercontrol click event动态用户控件点击事件
【发布时间】:2013-06-06 16:45:00
【问题描述】:

用户控制:

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

表格:

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);
                }
            }
        }

表单从数据库加载记录并在每个用户控件的动态创建的文本框中显示每个 LastName。 点击动态用户控件时如何在表单的文本框中显示地址等附加信息?

尝试:

    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"];
                }

            }
        }
    }

【问题讨论】:

  • 尝试没有成功,为什么?这里没有足够的信息让我们了解您正在尝试做什么 -而且更多的代码不会成为答案。说明您想要什么,您想要什么'正在做,什么不起作用。

标签: c# winforms event-handling click dynamic-usercontrols


【解决方案1】:

首先。在您的第一个代码片段中,您执行“Select ID from ...”,但希望在阅读器中找到“LastName”字段 - 不起作用。

第二。如果您知道您将需要来自Employee 表的更多信息,我建议您将其存储在与LastName 相同的UserControl 中。执行“Select * from ...”,将另一个字段和属性添加到UserControl2,并在Read循环中赋值:

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

第三。在您的第二个代码片段中,使用

UserControl2 usercontrol = (UserControl2)sender;

而不是

UserControl2 usercontrol = new UserControl2();

因为新创建的用户控件不会分配任何LastName

然后:

private void usercontrol_Click(object sender, EventArgs e)
{
    UserControl2 usercontrol = (UserControl2)sender;
    textBox1.Text = usercontrol.Address;
}

【讨论】:

    【解决方案2】:

    在您的用户控件 UserControl2(在构造函数或 InitializeComponent 方法中)您应该将点击事件转发给潜在的侦听器。

    类似的东西:

    public UserControl2()
    {
        ...
        this.lastNameTextBox.Click += (s, a) => OnClick(a);
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多