如何将特定值传输到列表框 [英] How transfer an specific value to listbox

查看:74
本文介绍了如何将特定值传输到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,早上好。



我正在用C#开发一个小型的个人解决方案。发生我有一个功能,可以向TabbedPanel添加许多按钮,就像代码可以显示



Hello, good morning.

I'm developing an small and personal solution in C#. Happens that i've a function to add many buttons to TabbedPanel like code can show

public void AddBeverageDrinkstoTabbedPanel(TabPage tp1, ListBox lst1)
       {


           food getbeverage = new food();
           string[] bev_product = getbeverage.name1;
           FlowLayoutPanel flp1 = new FlowLayoutPanel();

           flp1.Dock = DockStyle.Fill;

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





会发生什么?当我点击任何按钮时,想要将特定文本添加到列表框中并执行此操作我构建了一个名为beverageclick()的方法,其中包含以下代码:





What happens? When i click in any button, want add an specific text to listbox and to do this i builded an method called beverageclick(), with these code:

public void beverageclick(object sender, EventArgs e)
        {
            Button b = (Button)sender;//button sender

            string value = (string)b.Tag;// value = b.tag (tag of sended button tag = value (value of  AddBeverageDrinkstoTabbedPanel) method)
            
            //get value
            
            beverages.Add(value);//Add value 
            //MessageBox.Show(value);
            ListBox lstbox = new ListBox ();
            lstbox.Items.Add(value);
           
            
            //MessageBox.Show(value);

 

        }





有什么问题?如何在单击按钮时添加特定值。



What is wrong? how can i add an specific value when an button was clicked.

推荐答案

如果要为每个按钮添加列表框,则应将其添加到添加的位置按钮,即在 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.


这篇关于如何将特定值传输到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆