Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I am facing problems with location assigning and click events of dynamically created buttons.

I have a button array which will create the no of buttons dynamically, but i am very confused to assigning the location according to the form size horizontally and vertically and the click event of these dynamically created buttons.

For loading the buttons dynamically i wrote like this.
C#
private void frmResturantTables_Load(object sender, EventArgs e)
       {

           createTables(50); // count from my database, for eg 50

       }


Where createTables is my function for creating buttons dynamically.

C#
private void createTables(int length)
        {
            int j = 0;
            Button[] buttons = new Button[length];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new Button();
                buttons[i].Size = new System.Drawing.Size(95, 75);


                buttons[i].Name = "Button" + i.ToString();
                buttons[i].Text = "Table" + (i + 1).ToString();
                buttons[i].Font = new System.Drawing.Font("Comic Sans MS", 14.25F,     System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                buttons[i].Image = global::Biz_Maxx.Properties.Resources.tab;
                buttons[i].TextAlign = System.Drawing.ContentAlignment.BottomCenter;
                buttons[i].UseVisualStyleBackColor = true;
               
                    
                Point p = new Point(10 + i * 100, 10);
                buttons[i].Location = p;
                    
                   
                
                buttons[i].Click += new EventHandler(this.Button_Click);
                Controls.Add(buttons[i]);
            }
        }



Here i am using the button click event to open the forms

C#
protected void Button_Click(object sender, EventArgs e)
        {
            ucSales sale= new ucSales();
        }



Now my actual problem is according to the buttons the ucSales forms to be opened. ie
If i click button[1], that means the first button from the dynamically created, then the ucSales form is also be like ucSale[1] , for button[2], ucSales[2] should be opened, like that for each and every buttons i dynamically created should open the ucSales forms individually .
Also want to check if the form is opened already, then only display it with the data i entered for that particular form.

My second issue is in the allocation of these buttons dynamically displayed in the form, according to the form length and width.how to display the buttons according to the form size like first row,second row.....


I am developing a restaurant software, which have so many restaurant tables in it.. according to my database restaurant tables count, the buttons should shown dynamically in the form. So that for each restaurant table having different names and different billing screens. If i click any one of the restaurant table button from the form, it should display the billing screen.

Any help appreciated
Thanks in advance
Posted
Updated 3-Feb-14 0:58am
v2
Comments
BillWoodruff 3-Feb-14 3:44am    
I'd like to help you out, but I really can't appreciate the idea of a UI with fifty Buttons: that, to me, is not good design. Isn't it the case that what you need here is some selection mechanism, like a linked series of ComboBoxes, or a TreeView, for controlling the choices presented to the user ?

If you tell me that you have fifty different user-choices to represent, and they cannot be categorized, or grouped, into any "clusters," or hierarchy, okay; but, is that really the case here ?
Arun Ashok 4-Feb-14 2:34am    
Thank you guys for all the help you provided..... Using your directions and some code from my part has done the job.... All comments and codes appreciated.

To bypass the positioning problem you could use the FlowLayoutPanel control which will automatically arrange your buttons.

Regarding the click event handling you could put "something" in the button Tag property (let me say an ID or something similar) that will identify the ucSale in the button_click event handler.

For example:
C#
protected void Button_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    var ucSaleID = btn.Tag;
    //etc....
}
 
Share this answer
 
v2
Comments
Arun Ashok 3-Feb-14 5:19am    
I am developing a restaurant software, which have so many restaurant tables in it.. according to my database restaurant tables count, the buttons should shown dynamically in the form. So that for each restaurant table having different names and different billing screens. If i click any one of the restaurant table button from the form, it should display the billing screen.
BillWoodruff 3-Feb-14 5:27am    
Hi Arun, If your goal here is to create a kind of visual representation of the actual arrangement of the tables in space (a diagram), that changes the whole story !

This is a very important clarification of what your project is; I suggest you add this to the original post.
Alessio.NET 3-Feb-14 5:34am    
Maybe I'm wrong but I think that my answer will help you.
If you'll add your dynamically generated buttons to the FlowLayoutPanel it will automatically arrange them (check FlowLayoutPanel documentation for more info).
On the other hand inserting the ID of the ucSale in the property "Tag" of the button you're creating will let you find the ucSale later when you'll click on one of the buttons.
(You could also store the entire ucSale object in the "Tag" property and use it every time the button is clicked)
BillWoodruff 3-Feb-14 6:04am    
+4 A good answer that will help the OP.
Alessio.NET 3-Feb-14 6:15am    
Thank you very much!
Pertaining to your first (1st) problem, i can give you an EventHandler's array with 50 elements in it. Look at the codes illustrated below:

private EventHandler[] events = new EventHandler[50]
{ (object snd, EventArgs e) =>
{ ucSale sale1 = new ucSale(); } ,

(object snd, EventArgs e) =>
{ ucSale sale2 = new ucSale(); } ,

. . . ; // and so forth to the max limit number defined
}
buttons[i].Click += events[i];

--------------------------------------------------------------------------------------------
Now, pertaining to your second problem, we could design to arrange 5 buttons lined up in horizontal position and let it be left 10 buttons aligned in the vertical order on both side.

int btnWidth = 0, btnHeight = 0;

if( Form1.Width % 50 == 0 )
btnWidth = Form1.Width / 50;
else
btnWidth = int32.Parse( Form1.Width / 50 );


if( Form1.Height % 10 == 0 )
btnHeight = Form1.Height / 10;
else
btnHeight = int32.Parse( Form1.Height / 10 );


buttons[i].Width = btnWidth; buttons[i].Height = btnHeight;


I hope that will help, .. Thank's
 
Share this answer
 
v2
Comments
Arun Ashok 4-Feb-14 2:32am    
Thank you guys for all the help you provided..... Using your directions and some code from my part has done the job.... All comments and codes appreciated.

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