Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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:

C#
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);


           }
Posted
Updated 8-Feb-16 19:43pm
v2
Comments
[no name] 8-Feb-16 4:05am    
Register your dynamic created buttons in an Array to Access them later in your code.
Member 12242717 8-Feb-16 4:29am    
but sir i want to add a button from database values..
[no name] 8-Feb-16 4:53am    
And what is the Problem? Use something like this:
Button[] panelButtons = new Button[dt.Rows.Count];
for (int xIx = 0; xIx < dt.Rows.Count; xIx++)
{
panelButtons[xIx] = new Button();
panelButtons[xIx].Tag= xIx;
// panelButtons[xIx].Text = ...
// panelButtons[xIx].....

this.Controls.Add(panelButtons[xIx]);
}

BillWoodruff 8-Feb-16 13:56pm    
This is very confused code: why are you first creating a set of buttons, and then when one of those is clicked, creating another set of buttons ? All these buttons are being added to the same container (a Form, I assume), and you are giving them same names.

Please state clearly what your goal is here, and rewrite your question.
Member 12242717 9-Feb-16 0:45am    
i'm creating POS touch system so when i touch in category it show all the sub category.

You need to get a reference to the control you want to hide. In this case you luckily get it as a parameter.
C#
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
    // ...
}
 
Share this answer
 
Comments
Member 12242717 8-Feb-16 4:32am    
it hide main button.. i want to hide child button after click second button..
lukeer 8-Feb-16 4:55am    
It hides the button that user has been clicking on. If that's not what you wanted please Improve your question so it describes clearly what buttons are there, what is the "main button", which button shall be hidden.
Ralf Meier 8-Feb-16 6:40am    
I agree with Lukeer.
What is the dependancy between the Button, which is clicked, and the button (or any other control) you want to hide ? Is it allways the same Button, you want to hide ?
Member 12242717 9-Feb-16 0:50am    
yes sir
Ralf Meier 9-Feb-16 3:43am    
Yes Sir means : Allways the same Button which is to hide ...?
In this case you could use the code-snippet of this solution from Lukeer and you only have to replace in the line "buttonToHide.Visible = false;" the buttonToHide with the Name of the Button which should be hidden. I suppose, that this Button isn't dynamicly created / is a fixed part of your form.
C#
newPanelcategory.Visible = false;
 
Share this answer
 
Comments
lukeer 8-Feb-16 3:59am    
There is no newPanelcategory in newPanelCategory_Click.
Member 12242717 8-Feb-16 4:37am    
then what should i do for it..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900