473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Adding buttons to the form dynamically

HELP NEEDED IN C# SHARP??

I have been working on a project in c sharp i am trying to add buttons to the panel in my form when the user clicks a button "add more".

How will i do that add buttons to the panel in the form dynamically that (new button) should be placed on nextline.
May 18 '08 #1
11 2449
vanc
211 Expert 100+
HELP NEEDED IN C# SHARP??

I have been working on a project in c sharp i am trying to add buttons to the panel in my form when the user clicks a button "add more".

How will i do that add buttons to the panel in the form dynamically that (new button) should be placed on nextline.
Button aButton = new Button();
aButton.Name = "Button"; //you also can set height width
targetPanel.Controls.Add(aButton);

Cheers.
May 19 '08 #2
Button aButton = new Button();
aButton.Name = "Button"; //you also can set height width
targetPanel.Controls.Add(aButton);

Cheers.

You r right but this only creates one button dynamically
I want to create buttons as long as user wants
I am posting my code that will add a textbox and a label to the panel of the form
It only prints one textbox and and label dynamically

private void button1_Click(object sender, EventArgs e)
{
j = j + 25;// increment the position of the labael and text box
this.textbox2.Size = new System.Drawing.Size(75, 20);
this.textbox2.Location = new System.Drawing.Point(91, j);
this.textbox2.Name.Equals("textbox" + i);

this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, j);
this.label2.Name.Equals("Enter number" + i);
this.label2.Size = new System.Drawing.Size(72, 13);
this.label2.Text = "Enter Number" + i;

this.panel1.Controls.Add(this.textbox2);
this.panel1.Controls.Add(this.label2);
i++;//increments the number of textbox an label added
}

[IMG]C:\Documents and Settings\NOUMAN\Desktop\untitled.bmp[/IMG]
May 19 '08 #3
Plater
7,872 Expert 4TB
What do you mean only creates one button dynamically?
It will make one button dynamically as many times as you call that code, for example on every button click.

I see your code does not actually create a new textbox and label but mearly move around some existing ones. Be sure to actually create NEW instances of the controls you want.
May 19 '08 #4
What do you mean only creates one button dynamically?
It will make one button dynamically as many times as you call that code, for example on every button click.

I see your code does not actually create a new textbox and label but mearly move around some existing ones. Be sure to actually create NEW instances of the controls you want.

you r right when i run the program only the position of the label and textbox changes of that particular textbox and label nothing else .

the same textbox and label moves down the panel but nothing is added to the panel

how do i declare and initialize label and text box dynamically so that every time a button is clicked it declares and initialize a new textbox and label and adds it to the panel contol......
thx
May 19 '08 #5
Plater
7,872 Expert 4TB
It is shown in the code for adding a button given prior.
Expand|Select|Wrap|Line Numbers
  1. Textbox temptb = new TextBox();
  2. Label templb = new Label();
  3. //do stuff with them.
  4. //like setting locations and whatnot
  5.  
  6. //then add them to your form
  7. this.Controls.Add(temptb);
  8. this.Controls.Add(templb);
  9.  
May 19 '08 #6
It is shown in the code for adding a button given prior.
Expand|Select|Wrap|Line Numbers
  1. Textbox temptb = new TextBox();
  2. Label templb = new Label();
  3. //do stuff with them.
  4. //like setting locations and whatnot
  5.  
  6. //then add them to your form
  7. this.Controls.Add(temptb);
  8. this.Controls.Add(templb);
  9.  

THANKS PLATER IT REALLY WORKED

THANK YOU

THANK YOU
May 19 '08 #7
It is shown in the code for adding a button given prior.
Expand|Select|Wrap|Line Numbers
  1. Textbox temptb = new TextBox();
  2. Label templb = new Label();
  3. //do stuff with them.
  4. //like setting locations and whatnot
  5.  
  6. //then add them to your form
  7. this.Controls.Add(temptb);
  8. this.Controls.Add(templb);
  9.  

Thanks it worked my new modified function

private void call()
{
TextBox textbox1=new TextBox();
Label label1= new Label();

j = j + 25;
textbox1.Size = new System.Drawing.Size(75, 20);
textbox1.Location = new System.Drawing.Point(91, j);
textbox1.Name.Equals("textbox" + i);

label1.AutoSize = true;
label1.Location = new System.Drawing.Point(13, j);
label1.Name.Equals("Enter number" + i);
label1.Size = new System.Drawing.Size(72, 13);
label1.Text = "Enter Number" + i;

this.panel1.Controls.Add(textbox1);
this.panel1.Controls.Add(label1);
i++;
}

The textboxes and labels works properly that are dynamically created


How do i get the values from these dynamically created textboxes when user clicks a button??


thx
May 19 '08 #8
Here is the new code i have done to get the values from the dynamically created textboxes.
Every time this function is called a new textbox and label is added to the panel of my form.
How do i get the values from these dynamically creadted textboxes.
I have used an array to get the values but the vakues are not fetched from the textbox.

void create_label_textbox()
{
TextBox textBox1 = new TextBox();
Label label1 = new Label();

j = j + 25;
textBox1.Size = new System.Drawing.Size(75, 20);
textBox1.Location = new System.Drawing.Point(91, j);
textBox1.Name.Equals("textbox" + i);

label1.AutoSize = true;
label1.Location = new System.Drawing.Point(13, j);
label1.Name.Equals("Enter number" + i);
label1.Size = new System.Drawing.Size(72, 13);
label1.Text = "Enter Number" + i;

this.panel1.Controls.Add(textBox1);
this.panel1.Controls.Add(label1);

//this is an error the value are not fetched from the text box how
// do i fetch values from the dynamically created textbox
array[i]=int.Parse(textBox1.Text.ToString
i++;
}
May 20 '08 #9
Plater
7,872 Expert 4TB
Loop through the form's Controls collection for the .Name property of the control you want.
May 20 '08 #10
I have made a new function to solve the problem to get the dynamically created controls from the windows forms and the panel inside the form but i am having a problem in it please help

public void call2()
{
string str="";
//Form.ControlCollection formControls = new Form.ControlCollection(Form3);

foreach (Control c in Controls)
{
if (c is TextBox)
str += c.Name;
else if (c is Label)
str += c.Name;
else if (c is Panel)
{
//get the control inside the panel

foreach (Control d in c) //it is an error as it does not allow me to do so
{
if (c is TextBox)
str += c.Name;
else
str += c.Name;
}
}
else if (c is Label)
str += c.Name;
else
str += c.Name;

}
MessageBox.Show(str);
}
May 24 '08 #11
Plater
7,872 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. foreach (Control d in c) //it is an error as it does not allow me to do so
  2.  
c is a control, not a collection of controls, try c.Controls instead.

Also, you only allow for one extra level of children. What if the control you want is a child of a child of the main control? Like a textbox inside a groupbox inside another groupbox?
May 27 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: RussC | last post by:
My C# app has a toolbar with 12 or so buttons on it, all created via the designer. In some circumstances we need to add one or two buttons to it dynamically. I have code that does this and it...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
0
by: Sinisa Ruzin | last post by:
Hi all, I had problem with dynamically adding/removing controls;ascx, Controls.Add(Page.LoadControl... in the same page of the IBuySpy portal. ASP.NET, C#. I added buttons to the main ASCX loaded...
3
by: benoit | last post by:
using this usercontrol - http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx - I manage to create several inputfields and buttons, depending on what user needs. works fine ...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
6
by: Nathan Sokalski | last post by:
I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls such as Label controls, because they can be placed...
3
by: Byron Hopp | last post by:
Anybody have code to add a ToolBar, and its buttons to a MDI Child window. I have added the Toolbar, and added the buttons, but how do you determine what the button is going to execute upon the...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
1
by: Paddy | last post by:
The problem I am facing is as follows: I am populating an HTML table on my webpage with rows of data from a database. The rows may be sometimes 10 and sometimes say,3. I have two buttons on that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.