隐藏在c#.net中动态添加的按钮? [英] Hide a button that is dynamically add in c#.net?

查看:94
本文介绍了隐藏在c#.net中动态添加的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

the main problem is this code when i click 1st category it generate sub button but when i click 2nd category it does not show sub button of 2nd category, it is hide by 1 sub category







我尝试过:




.

What I have tried:

private void WindowsForm_Load(object sender, EventArgs e)
       {
           DataTable dt = bllcat.getallcategory();
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               Button newPanelcategory = new Button();
               newPanelcategory.Name = dt.Rows[i]["category_id"].ToString();
               newPanelcategory.Text = dt.Rows[i]["category_name"].ToString();
               newPanelcategory.Location = System.Drawing.Point.Add(new Point(4 + i * 100, 4), new Size(50, 50));// here make use of your logic.
               newPanelcategory.Height = 50;
               newPanelcategory.Width = 50;
               this.Controls.Add(newPanelcategory);
               newPanelcategory.Click += new EventHandler(newPanelcategory_Click);


           }

       }



       public void newPanelcategory_Click(object sender, EventArgs e)
       {
           label1.Text = "\r\n" + ((Button)sender).Name;
           Button newPanelButton = new Button();


           DataTable dts = blpro.getproductid(Convert.ToInt32(Convert.ToInt32(label1.Text)));

           for (int i = 0; i < dts.Rows.Count; i++)
           {

               newPanelButton.Name = dts.Rows[i]["category_id"].ToString();
               newPanelButton.Text = dts.Rows[i]["product_name"].ToString();
               newPanelButton.Location = System.Drawing.Point.Add(new Point(4 + i * 100, 4), new Size(100, 100));// here make use of your logic.
               newPanelButton.Height = 50;
               newPanelButton.Width = 50;
               this.Controls.Add(newPanelButton);


           }

推荐答案

您需要获取要隐藏的控件的引用。在这种情况下,您很幸运地将其作为参数。

You need to get a reference to the control you want to hide. In this case you luckily get it as a parameter.
public void newPanelcategory_Click(object sender, EventArgs e)
{
    Button buttonToHide = sender as Button;
    if(buttonToHide == null)
    {
        // Method has been called by a control that is not a button.
        //   That is unexpected.
    }
    buttonToHide.Visible = false;

    // Remaining stuff
    // ...
}


newPanelcategory.Visible = false;


这篇关于隐藏在c#.net中动态添加的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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