I am unable to get the controls count on the newly created form on the newly created button, I have created 5 controls but only one is showing. If I cannot get the total control count then I cannot also get the control type, name etc.
private void button2_Click(object sender, EventArgs e)
{
Form frm = new Form();
frm.Text = "new form";
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.AutoSize = true;
Button btn = new Button();
btn.Text = "ok";
tlp.Controls.Add(btn, 0, 4);
frm.Controls.Add(tlp);
for (int i = 3, ii = 0; i >= 0; i--, ii++)
{
TextBox tbx = new TextBox();
tlp.Controls.Add(tbx, 0, ii);
}
frm.Show();
string str = frm.Controls.Count.ToString();
btn.Click += (s, args) =>
{
MessageBox.Show(frm.Text);
MessageBox.Show(ActiveForm.Text);
MessageBox.Show(str);
};
}
In your code the only Control that you've added to your form is a TableLayoutPanel that contains TextBox controls. That is why the count is 1.
Related
Say if I want to create a multiple button on my form based on a loop value of 3 then 3 buttons should be created into that form, in my case I have this textbox input that should determine the loop value on button click. What I've tried:
void Button4Click(object sender, EventArgs e)
{
int get_col_range = Convert.ToInt32(textBox3.Text);
for(int i=0; i<get_col_range; i++) {
Button btn = new Button();
btn.Text = Convert.ToString(i);
this.Controls.Add(btn);
}
}
I put on value of 2 on the TextBox input as test value but turned out that nothing happened why?.
Try the following on a form with no controls in a button click event.
int top = 10;
int heightPadding = 30;
for (int index = 0; index < 3; index++)
{
var button = new Button()
{
Text = $"Button {index}",
Name = $"Button{index}",
Location = new Point(10, top)
};
Controls.Add(button);
top += heightPadding;
}
I want to get all items from CheckedBoxList and add it to a new Form so for every checked item I want a new Label with checkedItem name and a TextBox. So far I'm doing that but when I open the form I got no results at all. I don't know how to get the checked item name and I'm doing this:
labels[i].Text = i.ToString();
private void Button4_Click(object sender, EventArgs e)
{
testForm = new Test();
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
int n = 0;
for (int i=0;i<checkedListBox1.CheckedIndices.Count;i++)
{
txtBox = new TextBox[checkedListBox1.CheckedIndices.Count];
labels = new Label[checkedListBox1.CheckedIndices.Count];
labels[i] = new Label();
labels[i].Text = i.ToString();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(labels[i]);
txtBox[i] = new TextBox();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(txtBox[i]);
}
Controls.Add(tableLayoutPanel);
testForm.ShowDialog();
}
Any suggestions?
Thank you, for the invested time.
The solution was to change the code to:
testForm.Controls.Add(tableLayoutPanel);
I have a textbox and button on c# form and users can enter number.I create a label which users want and each label have a button.Here if I click those buttons i wanna create textbox but if users continue to click,i want to create more textbox.
Button[] Btn= new Button[10];
for (int i = 0; i < labelNumber; i++)
{
Btn[i] = new Button();
Btn[i].Text = "Add";
Btn[i].Location = new Point(40, 100 + i * 29);
Btn[i].Size = new Size(50,20);
this.Controls.Add(Btn[i]);
Btn[i].Click += new EventHandler(addNewTextbox);
}
on the code above; for example; if labelNumber == 3 so i have 3 label and 3 button with them, if i click add button i wanna create textbox near thislabel.
private void addNewTextbox(object sender, EventArgs e)
{
TextBox[] dynamicTextbox = new TextBox[10];
Button dinamikButon = (sender as Button);
int yLocation = (dinamikButon.Location.Y - 100) / 29;
//int xLocation = dinamikButon.Location.X - 100;
dynamicTextbox[yLocation] = new TextBox();
dynamicTextbox[yLocation].Location = new Point(100, 100 + yLocation * 29);
dynamicTextbox[yLocation].Size = new Size(40, 50);
this.Controls.Add(dynamicTextbox[yLocation]);
}
here i change textbox y coordinates but i couldn't it for X. if i change this
dynamicTextbox[yLocation].Location = new Point(100*x, 100 + yLocation * 29);
x++;
it sort equals all of them.
Label1 Button1
Label2 Button2
Label3 Button3
if i click Button1 4 times,it has to create 4 textbox alongside label1. and if i click Button2 2 times,it has to create 2 textbox alongside label2
Please Help ME.
The simplest way is to keep a list containing the created textboxes in the button's Tag property like this
private void addNewTextbox(object sender, EventArgs e)
{
var button = (Button)sender;
var textBoxes = button.Tag as List<TextBox>;
if (textBoxes == null)
button.Tag = textBoxes = new List<TextBox>();
var textBox = new TextBox();
textBoxes.Add(textBox);
textBox.Location = new Point(100 * textBoxes.Count, button.Top);
textbox.Size = new Size(40, 50);
this.Controls.Add(textBox);
}
This way you not only can add a new text box, but also can easily determine the created text boxes by each button at any time if needed.
I have nested controls inside RadPageView control on a Winform application. RadPageView has a child RadPageViewPage. These two controls are on the form however a tab control and inside that tab control some other controls are added dynamically. How can I find and change the value of the Textbox inside the dynamically generated tab control on a click of the button.
public Form1()
{
InitializeComponent();
TabControl tb = new TabControl();
tb.Width = 500;
TabPage tp = new TabPage("Tab 1");
Label lb = new Label();
lb.Text = "Test";
lb.Location = new Point(10, 10);
TextBox txt = new TextBox();
txt.Text = "Textbox";
txt.Location = new Point(200, 10);
tp.Controls.Add(lb);
tp.Controls.Add(txt);
tb.Controls.Add(tp);
radPageViewPage1.Controls.Add(tb);
}
private void button1_Click(object sender, EventArgs e)
{
}
I have found this example over the internet and its working perfectly.
public Form1()
{
InitializeComponent();
TabControl tb = new TabControl();
tb.Width = 500;
TabPage tp = new TabPage("Tab 1");
Label lb = new Label();
lb.Text = "Test";
lb.Name = "lblTest";
lb.Location = new Point(10, 10);
TextBox txt = new TextBox();
txt.Text = "Textbox";
txt.Name = "txtName";
txt.Location = new Point(200, 10);
tp.Controls.Add(lb);
tp.Controls.Add(txt);
tb.Controls.Add(tp);
radPageViewPage1.Controls.Add(tb);
}
private void button1_Click(object sender, EventArgs e)
{
var crl = FindControl("txtName");
MessageBox.Show(crl.Text);
}
Control FindControl(string target)
{
return FindControl(this, target);
}
static Control FindControl(Control root, string target)
{
if (root.Name.Equals(target))
return root;
for (var i = 0; i < root.Controls.Count; ++i)
{
if (root.Controls[i].Name.Equals(target))
return root.Controls[i];
}
for (var i = 0; i < root.Controls.Count; ++i)
{
Control result;
for (var k = 0; k < root.Controls[i].Controls.Count; ++k)
{
result = FindControl(root.Controls[i].Controls[k], target);
if (result != null)
return result;
}
}
return null;
}
I have a form which is an MDI container. In that form i generate 6 child forms each with a label:
for (int i = 0; i < 6; i++)
{
Form window = new Form();
window.Width = 100;
window.Height = 100;
window.MdiParent = this;
window.FormBorderStyle = FormBorderStyle.FixedToolWindow;
Label label = new Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(1, 1);
label.Size = new System.Drawing.Size(35, 13);
label.TabIndex = 1;
label.Name = "label" + i.ToString();
label.Text = window.Top.ToString();
window.LocationChanged += new System.EventHandler(HERE);
window.Controls.Add(label);
window.Show();
}
I added an event on the locationchanged for window. Now how do do it so that label updates to the windows position?
I think this line will do the trick for you:
window.LocationChanged += new EventHandler(delegate(object o, EventArgs evtArgs) {
label.Text = window.Location.ToString();
});
Well, it's easiest to do it with a lambda expression or an anonymous method:
window.LocationChanged += (sender, args) => label.Text = window.Top.ToString();
If you're using C# 1.1 you'd need to be a bit trickier because of the label being captured automatically in C# 2+ - you'd have to create a new class like this:
internal class LocationChangeNotifier
{
private readonly Label label;
internal LocationChangeNotifier(Label label)
{
this.label = label;
}
internal void HandleLocationUpdate(object sender, EventArgs e)
{
label.Text = ((Control) sender).Top.ToString();
}
}
then use it as:
LocationChangeNotifier notifier = new LocationChangeNotifier(label);
window.LocationChanged += new EventHandler(notifier.HandleLocationUpdate);
Aren't captured variables great? :)