Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
Hi Friends,

I am beginner in C# programming.

I want to draw button dynamically and arrange it automatically using Windows Forms Application.

For example:

I have 6 button and i show only 4 button in my client machine, and 5 button for another client machine.

How i can manage this type of situation.

Please help me.

Thanks in Advance.
Posted
Updated 24-May-11 21:54pm
v3
Comments
Sergey Alexandrovich Kryukov 25-May-11 0:48am    
Good idea, my 4. Now tag it! WPF or Forms, or what?
--SA
Tiwari A K 25-May-11 2:54am    
Thank you SA
i want it in win form application with client-server approach. there are a server and 2 client. i want to show/hide buttons with my choice to client machine.
Sergey Alexandrovich Kryukov 25-May-11 4:06am    
Thanks for tagging. It's for your benefit -- you want experts to see the class of your problem before they open the file.
What you want looks simple. What's the problem?
--SA
Tiwari A K 25-May-11 4:24am    
I want to access control on my application for client machine. clients saw only what I want.
Sergey Alexandrovich Kryukov 25-May-11 4:07am    
Did you get satisfactory answers? If not, I'll think how to answer...
--SA

Sure. You have some data, it can change. Populate it. Do something like this:

C#
Button button = new Button();

//...

button.Location = //.. from data
button.Text = //.. from data, calculate parent size, arrangement
parent.Controls.Add(button); // some parent Control; at this moment button appears on screen
button.Click += (sender, eventArgs) => { /* whatever you need on click... */ }

//...


All this you can do dynamically, when UI is already shown or not.

This is just the idea, because you did not tag what UI library you want to use. It would look pretty much like that anyway.

—SA
 
Share this answer
 
Comments
Tiwari A K 25-May-11 4:27am    
Thank you Very Much SA
I want to access control on my application for client machine. clients saw only what I want.
Sergey Alexandrovich Kryukov 27-May-11 13:59pm    
You're welcome.
Thank you for accepting this answer.
If your problem is not yet solved, feed free to ask a follow-up question.
Good luck,
--SA
This may useful to you...
//here we assum that there is 5 element... you have to create an array with number of button
string[] ButtonText = {"FirstValue", "SecondValue", "ThirdValue", "FourthValue", "FifthValue" };
//after creating an array of ButtonName call the below function from where you want to generate your dynamic buttons...
CreateButtonDynamic(5,ButtonText);//call of function

//function to create dynamic buttons
private void CreateButtonDynamic(int numberOfButton, string[] ButtonText)
{
   for (int i = 0; i < numberOfButton; i++)
   {
      Button temp = new Button();
      temp.Name = "button"+(i+1).ToString();
      temp.Text = ButtonText[i];
      temp.AutoSize = true;
      temp.Location = new System.Drawing.Point(100, 100 + (25 * i));//please adjust location as per your need
      temp.Tag = i;
      temp.Click += new EventHandler(OnButtonClick); 
      this.Controls.Add(temp);//this will add the control to form if you have panel or some other container then you have to specify the name of that container like...
      //this.panel1.Controls.Add(temp);
   }
}
private void OnButtonClick(object sender, EventArgs e)
{
   Button temp = (Button)sender;
   MessageBox.Show("You have clicked on  : " + temp.Text);
}
 
Share this answer
 
v2
Comments
Tiwari A K 25-May-11 4:26am    
Thank you very much Sir
parmar_punit 25-May-11 5:10am    
Well come :)
For windows application
just specify this

C#
this.demotextbox.Location = new System.Drawing.Point(139, 127);


or

create all the buttons and check which for the user and then make the button visible accordingly
and in case of web application create a table at run time and add the controls to it
Click Here
and know more about it
 
Share this answer
 

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