且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将特定值传输到列表框

更新时间:2023-02-10 14:40:10

如果要为每个按钮添加列表框,则应将其添加到添加的位置按钮,即在 AddBeverageDrinkstoTabbedPanel 中,您应该像对按钮那样进行类似的初始化。 (设置位置,父母等)

此外,我不建议你使用'value'作为变量名,因为它是一个关键字。请参阅此处: [ ^ ]
If you want to add a listbox with each button, you should add it at where you add your button, that is, in AddBeverageDrinkstoTabbedPanel and you should do the similar initialization as you do for button. (set position, parent etc)
Moreover I don't advice you to use 'value' as variable name since it is a keyword. see here: value[^]


您正在尝试从包含no的对象中检索值值,见下文。



You are attempting to retreive values from objects that contain no values, see below.

public void AddBeverageDrinkstoTabbedPanel(TabPage tp1, ListBox lst1)
{
    // The scope of the new objects below reside only within this method.
    // The New objects only contains non imitated members of the class.
    food getbeverage = new food();
    // Attempting to retrieve values from an uninitiated class?
    // Where should the values come from?
    string[] bev_product = getbeverage.name1;
    FlowLayoutPanel flp1 = new FlowLayoutPanel();

    flp1.Dock = DockStyle.Fill;
    // There are no values, thus no text to add
    foreach (string value in bev_product) .
    {
        Button b = new Button();
        b.Size = new Size(80, 80);
        b.Text = value.ToString();
        b.Tag = value;
        b.Click += new EventHandler(beverageclick);

        beverages.Add(value);
        flp1.Controls.Add(b);
    }
    tp1.Controls.Add(flp1);
}



祝你好运。


Good luck.