what I want to do is to add more than one control from code behind to my website, I know how to add one control, but I want to add two controls at the same time, when someone click on an other button!
here is my code, but it only add the second button!
protected void Unnamed2_Click(object sender, EventArgs e)
{
Button b = new Button();
for (int i = 0; i < 2; i++)
{
b.ID = i.ToString();
b.Text = i.ToString();
b.Width=250;
b.Height = 100;
b.Style.Add("background-color", "red");
Page.Form.Controls.Add(b);
}
}
The new Button() also needs to be in the loop... otherwise you only create one instance.
for (int i = 0; i < 2; i++)
{
Button b = new Button();
b.ID = i.ToString();
b.Text = i.ToString();
b.Width=250;
b.Height = 100;
b.Style.Add("background-color", "red");
Page.Form.Controls.Add(b);
}
Related
This is my code in which i created dynamic controls in 2 panels.
Now I want to get values from these dynamic controls. I also generated dynamic button and its event but when i click in button it will not hit break point and page is post back.I read on internet that i have to generate control or store their values into PreInit of Asp.net page life cycle. But i can't understand it. If anyone know about it Reply me.
Thanks.
public void showControls(string p)
{
Button btn = new Button();
List<string> lblUD = getUserDetails(p);
List<string> lblli = getWebPagesName();
for (int i = 0; i < o; i++)
{
HtmlAnchor ha = new HtmlAnchor();
CheckBox cb = new CheckBox();
Label lbl = new Label();
cb.Checked = false;
cb.ID = "cb" + i.ToString();
cb.Text = "Allow";
lbl.Text = lblli[i].ToString();
lbl.ID = "lbl" + i.ToString();
ha.Controls.Add(lbl);
ha.Controls.Add(new LiteralControl("<br/>"));
lbl.Style.Add("line-height", "28px");
//lbl.Style.Add("border", "1px solid");
//cb.Style.Add("border", "1px solid");
lblPanel.Controls.Add(ha);
//lblPanel.Controls.Add( new LiteralControl("<br/>"));
cb.Style.Add("line-height", "24px");
cbPanel.Controls.Add(cb);
cbPanel.Controls.Add(new LiteralControl("<br/>"));
Label2.Enabled = true;
Label2.Text = "<h2>Assign Roles</h2>";
}
for (int s = 0; s < lblUD.Count; s++)
{
Label lblud = new Label();
lblud.ID = "lblud" + s.ToString();
lblud.Text = lblUD[s].ToString();
lblud.Style.Add("line-height", "28px");
userdetailPanel.Controls.Add(lblud);
userdetailPanel.Controls.Add(new LiteralControl("<br/>"));
}
btn.ID = "btn_Submit";
btn.Text = "Save";
btn.Click += new System.EventHandler(this.btn_Submit_click);
cbPanel.Controls.Add(btn);
}
I have a problem, because in my code I am dynamically creating new buttons, and after that, the window looks in that way:
This is code that I used for that:
private void DrawButtons()
{
for (int i = 0; i < 90; i++)
{
Button button = new Button();
button.Location = new Point(15 + 40 * i, 10);
button.Size = new Size(35, 30);
button.Parent = panel4;
button.Tag = i;
Controls.Add(button);
button.BringToFront();
}
}
I want to have scrollable panel, like there, where I created buttons manually:
What I must do to have this effect with programically created elements?
You can use the AutoScroll property. For Panel:
panel4.AutoScroll = true;
But you should also set this property:
button.Anchor = AnchorStyles.Left;
And also add the button to your Panel:
panel4.Controls.Add(button);
So this should be what you want:
private void DrawButtons()
{
for (int i = 0; i < 90; i++)
{
...
button.Anchor = AnchorStyles.Left;
...
panel4.Controls.Add(button);//Add this also
...
}
panel4.AutoScroll = true;
}
The result:
I'm making a program that will make buttons on the screen from a for loop. This is important because the user needs to have access to the number of buttons, and could change with every run. This is the code i have so far:
public Form1()
{
InitializeComponent();
int top = 5;
int left = 5;
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Height = 50;
button.Left = left;
button.Top = top;
this.Controls.Add(button);
left += button.Width + 2;
}
}
What i want is basically something like this:
Button b+i = new Button();
Its like when combining two strings, i want the name of the button on the first run in the loop to be b0, then b1, then b2, and so on.
I use Visual Studio, and InitializeComponent() goes to the editor generated code to make the window and stuff. nothing but the window is made there.
Thank you for your help!
Worrying about the variable names is the wrong way to approach this problem. There are several alternatives:
Use a list
List<Button> buttons;
public Form1()
{
InitializeComponent();
buttons = new List<Button>();
int top = 5;
int left = 5;
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Height = 50;
button.Left = left;
button.Top = top;
this.Controls.Add(button);
buttons.Add(button);
left += button.Width + 2;
}
}
//now instead of 'b1', it's 'buttons[1]'
Which you could also create with the OfType() method:
var buttons = this.Controls.OfType<Button>().ToList();
These can be done in combination with a FlowLayoutPanel or TableLayoutPanel to simplify the code:
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Height = 50;
FlowLayoutPanel1.Controls.Add(button); //panel handles Left/Top location
}
}
The panel also has the advantage of helping your app scale at different dpi's or screen/window sizes.
Any of which could also be adapted to start thinking in terms of connecting to a datasource (and reduce the code):
var buttons = Enumerable.Range(0, 10).Select(b => new Button {
Height = 50,
Left = 5 + ( b * (BUTTONWIDTH + 2) ),
Top = 5,
Name = String.Format("Button{0}", b)
});
//buttons.ToList()
//or
//this.Controls.AddRange(buttons.ToArray())
//or
//FlowLayoutPanel1.Controls.AddRange(buttons.ToArray()) //Remove Left/Top code
But all of this is meaningless until you can get the buttons to actually do something. You need an event handler for (at least) the Click event:
foreach (var b in butons)
{
b.Click += (s,e) =>
{
//click code for all of the buttons goes here
// you can tell the buttons apart by looking at the "s" variable
};
}
since people (rightly) said 'use an array' here is code
public Form1()
{
InitializeComponent();
int top = 5;
int left = 5;
var buttons = new Button[10];
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Height = 50;
button.Left = left;
button.Top = top;
this.Controls.Add(button);
left += button.Width + 2;
buttons[i] = button;
}
}
I try to add check box for loop that when i enter 3 for example in textbox and click the button it automatically add 3 check boxes in the form
i tried this code but only add one check box
private void button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int m = 1;
for (int i = 0; i < x; i++)
{
CheckBox button2 = new System.Windows.Forms.CheckBox();
button2.Location = new System.Drawing.Point(5, m);
button2.Name = "button2 "+ m.ToString();
button2.Size = new System.Drawing.Size(51, 23);
button2.TabIndex = m;
//button2.UseVisualStyleBackColor = true;
this.Controls.Add(button2);
m++;
}
}
You are setting the location of all three buttons to nearly the same place so they are displayed on top of each other. Try spacing them out a little more.
For example change m++; to m += 40;.
You need to space your buttons out a little. Also, you should give each of your buttons a unique ID.
button2.ID = "Button_" + i;
I want to create 10 buttons on my form when I click on button1. No error with this code below but it doesnt work either.
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < buttons.Capacity; i++)
{
this.Controls.Add(buttons[i]);
}
}
You aren't creating any buttons, you just have an empty list.
You can forget the list and just create the buttons in the loop.
private void button1_Click(object sender, EventArgs e)
{
int top = 50;
int left = 100;
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
this.Controls.Add(button);
top += button.Height + 2;
}
}
It doesn't work because the list is empty. Try this:
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < 10; i++)
{
Button newButton = new Button();
buttons.Add(newButton);
this.Controls.Add(newButton);
}
}
You could do something like this:
Point newLoc = new Point(5,5); // Set whatever you want for initial location
for(int i=0; i < 10; ++i)
{
Button b = new Button();
b.Size = new Size(10, 50);
b.Location = newLoc;
newLoc.Offset(0, b.Height + 5);
Controls.Add(b);
}
If you want them to layout in any sort of reasonable fashion it would be better to add them to one of the layout panels (i.e. FlowLayoutPanel) or to align them yourself.
Two problems- List is empty. You need to add some buttons to the list first. Second problem: You can't add buttons to "this". "This" is not referencing what you think, I think. Change this to reference a Panel for instance.
//Assume you have on your .aspx page:
<asp:Panel ID="Panel_Controls" runat="server"></asp:Panel>
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < buttons.Capacity; i++)
{
Panel_Controls.Controls.Add(buttons[i]);
}
}
use button array like this.it will create 3 dynamic buttons bcoz h variable has value of 3
private void button1_Click(object sender, EventArgs e)
{
int h =3;
Button[] buttonArray = new Button[8];
for (int i = 0; i <= h-1; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Size = new Size(20, 43);
buttonArray[i].Name= ""+i+"";
buttonArray[i].Click += button_Click;//function
buttonArray[i].Location = new Point(40, 20 + (i * 20));
panel1.Controls.Add(buttonArray[i]);
} }
I had the same doubt and came up with the following contribution:
int height = this.Size.Height;
int width = this.Size.Width;
int widthOffset = 10;
int heightOffset = 10;
int btnWidth = 100; // Button Widht
int btnHeight = 40; // Button Height
for (int i = 0; i < 50; ++i)
{
if ((widthOffset + btnWidth) >= width)
{
widthOffset = 10;
heightOffset = heightOffset + btnHeight
var button = new Button();
button.Size = new Size(btnWidth, btnHeight);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click; // Button Click Event
button.Location = new Point(widthOffset, heightOffset);
Controls.Add(button);
widthOffset = widthOffset + (btnWidth);
}
else
{
var button = new Button();
button.Size = new Size(btnWidth, btnHeight);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click; // Button Click Event
button.Location = new Point(widthOffset, heightOffset);
Controls.Add(button);
widthOffset = widthOffset + (btnWidth);
}
}
Expected Behaviour:
This will generate the buttons dinamically and using the current window size, "break a line" when the button exceeds the right margin of your window.
First, you aren't actually creating 10 buttons. Second, you need to set the location of each button, or they will appear on top of each other. This will do the trick:
for (int i = 0; i < 10; ++i)
{
var button = new Button();
button.Location = new Point(button.Width * i + 4, 0);
Controls.Add(button);
}
You can't add a Button to an empty list without creating a new instance of that Button.
You are missing the
Button newButton = new Button();
in your code plus get rid of the .Capacity