如何通过参数/变量更改ComboBox的名称?

huangapple go评论65阅读模式
英文:

How Can i Change the ComboBox Name by parameter/variable?

问题

我写了这个方法,这样我就可以使用它而不是重复代码。但是如何将 cmb1(ComboBox 名称)更改为类似 cmb(i) 的东西呢?我的意思是如果 i = 10,则 ComboBox 名称 = cmb10。对于代码的这部分(cmb1.Items.Add(srd.GetValue(0).ToString());),我想根据 i 更改数字。这可能吗?如果可以的话,请帮助我!

这是我正在尝试的方法

public void ComboText(string text, int i)
{
    try
    {
        string connectionString = @"Data Source=DESKTOP-V0HE9JH\SQLEXPRESS01;Initial Catalog=Parts_Shop;Integrated Security=True";
        SqlConnection connection = new SqlConnection(@connectionString);

        connection.Open();
        SqlCommand command = new SqlCommand("select manufacturer from Parts where item_type = '" + text + "'", connection);
        SqlDataReader srd = command.ExecuteReader();

        while (srd.Read())
        {
            cmb1.Items.Add(srd.GetValue(0).ToString());
        }

        connection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
英文:

I wrote this method so i can use it instead of repeating the code.but how can i change cmb1 (ComboBox Name) to something like cmb(i). what i mean if i =10 ComboBox name = cmb10.for this part of the code(cmb1.Items.Add(srd.GetValue(0).ToString());).i want to change the number regarding to i.is it possible?if it is please help me!

This is the method im trying

public void ComboText(string text,int i)
{
    try
    {
        string connectionString = @"Data Source=DESKTOP-V0HE9JH\SQLEXPRESS01;Initial Catalog=Parts_Shop;Integrated Security=True";
        SqlConnection connection = new SqlConnection(@connectionString);

        connection.Open();
        SqlCommand command = new SqlCommand("select manufacturer from Parts where item_type = '" + text + "'", connection);
        SqlDataReader srd = command.ExecuteReader();

        while (srd.Read())
        {
            cmb1.Items.Add(srd.GetValue(0).ToString());
        }

        connection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

答案1

得分: 2

Controls 集合有两个索引器重载,一个接受一个 int,另一个接受一个表示控件名称的 string

var cmb = (ComboBox)Controls["cmb" + i];

如果组合框直接放置在窗体上,这段代码可行。如果它被放置在另一个控件上,比如一个面板,那么请访问该容器控件的 Controls 属性:

var cmb = (ComboBox)panel1.Controls["cmb" + i];
英文:

The Controls collection has two indexer overloads one accepting an int and another a string representing the name of the control.

var cmb = (ComboBox)Controls["cmb" + i];

This works if the combo box is placed directly on the form. If it is placed on another control like e.g. a panel, then access this container control's Controls property:

var cmb = (ComboBox)panel1.Controls["cmb" + i];

答案2

得分: 1

如果我理解正确,您指的是 Windows Forms 的 ComboBox。如果是这样,您可以自由更改其 Name 属性,但无法更改其在代码中的名称。

如果您的所有组合框已经设置好,可能的解决方案是事先设置好 Name 属性(例如 cmb1.Name = "1"cmb2.Name = "2" 等),然后在您的 void ComboText(string text,int i) 中循环遍历窗口中的所有组合框(例如像 这里 或使用 System.Linq 的 FirstOrDefault() 方法),并获取具有 Name == i.ToString() 的组合框。

英文:

If I understood correctly, you meant Windows Forms ComboBox. If that is so, you are free to change its Name property, but you can't change its in-code name.

If all your comboboxes are already set, possible solution would be to set up the Name property beforehand (cmb1.Name = "1", cmb2.Name = "2" etc.) and then, in your void ComboText(string text,int i) loop through all the comboboxes in the window (e.g like here or use System.Linq's FirstOrDefault() method) and get the one with the Name == i.ToString().

huangapple
  • 本文由 发表于 2023年6月22日 19:12:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定