I have program with multi methods .
We create all the controls with methods .
One of methods is for creating textBox . It is like :
private TextBox textBox1;
public void CreateTextBox()
{
this.textBox1 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(100, Position);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
Position += 30;
this.Controls.Add(this.textBox1);
}
There are several textBoxes in a form (the count of textbox might change between 10 to 20 ) .
So , If I want to create several textBoxes , Call methods like :
CreateTextBox();
CreateTextBox();
CreateTextBox();
If I want to have the Text of this textboxes , A code like this return me the last textBox Text :
MessageBox.Show(textBox1.Text);
My problem is ,,,, How can I detect the text of first called of CreateTextBox() and second called of CreateTextBox() ?
Thank u for read
You can use an array containing all TextBoxes:
var form = new Form();
var boxes = new TextBox[10];
for (int i = 0; i < boxes.Length; i++)
{
var box = new TextBox();
box.Location = new Point(10, 30 + 25 * i);
box.Size = new Size(100, 20);
form.Controls.Add(box);
boxes[i] = box;
}
var button = new Button();
button.Text = "Button";
button.Click += (o, e) =>
{
var message = String.Join(", ", boxes.Select(tb => tb.Text));
MessageBox.Show(message);
};
form.Controls.Add(button);
Application.Run(form);
Related
I have a button in the form. A groupbox is loaded each time the button is clicked (you can refer to my previous question). there are few textboxes and labels in each of the groupbox. I have raised key down events for the textboxes in the groupbox. The keyDown event does a few mathematical calculations.
Link to my previous question:
Loading multiple Groupboxes in the form using the button_click event
It works fine when only one groupbox is loaded. But when the second groupbox is loaded, the event raised doesnot work for the textboxes in the previously loaded groupboxes. It only works for the new groupbox.
What should be done to retain the events for the controls in the previous groupbox when the new group box is loaded.
This is the first thing I tried:
private GroupBox GetGroupBox(int a, int b, int c)
{
System.Windows.Forms.GroupBox gBox = new System.Windows.Forms.GroupBox();
Form1 myObj = new Form1();
gBox.Text = "S" + (a.ToString());
gBox.Name = "S" + (a.ToString());
gBox.Width = 150;
gBox.Height = 150;
gBox.Location = new Point(15 + (count - 1) * (gBox.Width + 10), 400);
// Labels
Label lb1 = new Label();
lb1.Text = "Length";
lb1.Width = 60;
lb1.Height = 20;
Label lb2 = new Label();
lb2.Text = "Angle";
lb2.Width = 50;
lb2.Height = 20;
// Textboxes
tbx1 = new TextBox();
tbx1.Name = "tbx" + b.ToString();
tbx1.Width = 40;
tbx1.Height = 10;
tbx1.Text = "50";
tbx1.KeyDown += Tbx1_KeyDown;
tbx2 = new TextBox();
tbx2.Name = "tbx" + c.ToString();
tbx2.Width = 40;
tbx2.Height = 10;
tbx2.Text = "45";
tbx2.KeyDown += Tbx2_KeyDown;
gBox.Controls.Add(lb1);
lb1.Location = new Point(10, 30);
gBox.Controls.Add(lb2);
lb2.Location = new Point(10, 70);
gBox.Controls.Add(tbx1);
tbx1.Location = new Point(100, 30);
gBox.Controls.Add(tbx2);
tbx2.Location = new Point(100, 70);
return gBox;
}
Then I tried creating an array of all the textboxes then used foreach loop to raise events:
TextBox[] tbxs = {tbx1, tbx2 };
foreach (TextBox tbx1 in tbxs)
{
tbx1.KeyDown += Tbx1_KeyDown1;
tbx2.KeyDown += Tbx2_KeyDown1;
}
But it didn't work out.
I have used a button for creating a set of textboxes as follows:
public partial class Form1 : Form
{
private int a = 75;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox1.Location = new System.Drawing.Point(50, a);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
this.Controls.Add(this.textBox1);
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox2.Location = new System.Drawing.Point(200, a);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 0;
this.Controls.Add(this.textBox2);
this.button1.Location = new System.Drawing.Point(650, a);
a += 25;
}
}
So there might be created many textbox1 and textbox2 for many button clicks. Say 10 textbox1 and 10 textbox2. How can I get data from all of them and to store in database. each textbox1 and textbox2 to be in a row of table?
Any help will be welcomed.
First of all: I think you are looking for a DataGrid.
If not, there are several steps to be taken. First of all, textbox1 and textbox2 are members of the class and not the method, so with every click you edit the same TextBoxes again and again. To create new TextBoxes use
TextBox newBox = new TextBox();
inside the method and continue using it instead of this.textbox1 (or textbox2). Keep in mind, that you should change to Location of the TextBoxes every time you click the button so that they don't overlap. You could use a class variable as click count.
I think the easiest way would be to store the created TextBoxes in a collection, for example a Dictionary:
public partial class...
{
Dictionary<TextBox, TextBox> tbPairs = new Dictionary<TextBox, TextBox>();
private void button1_Click(...)
{
... //create newBox1 and newBox2
tbPairs[newBox1] = newBox2; //adds the Pair to the Dictionary
}
}
To recall the content of the Dictionary after the TextBoxes are filled use
foreach (KeyValuePair<TextBox, TextBox> in tbPairs)
{
... //write to Server here - but that is an issue too big for handling here - look it up
}
For how to write Data to a database check out how to use SqlCommands
Im trying to create event with different index for dynamically created GroupBox. With my actual code event for every groupbox is that same. How can i make event with different index for every groupbox? My Code:
public void LoadGry()
{
// GroupBox groupbox = new GroupBox();
Label nazwagry = new Label();
for(int i = 0; i < myCollection.Count; i++)
{
GroupBox groupbox = new GroupBox();
groupbox.Text = myCollection[i];
groupbox.Size = new Size(290, 131);
groupbox.Location = new Point(6, 150 * (myCollection.Count - i - 1));
groupbox.ForeColor = Color.White;
Label label1 = new Label();
label1.Text = groupbox.Text;
label1.AutoSize = true;
label1.Location = new Point(groupbox.Location.X + 80, groupbox.Location.Y + 20);
groupbox.Controls.Add(label1);
Gry.Controls.Add(label1);
PictureBox picturebox = new PictureBox();
picturebox.Location = new Point(groupbox.Location.X + 5, groupbox.Location.Y + 20);
picturebox.Size = new Size(75, 75);
picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
picturebox.LoadAsync(myCollection3[i]);
groupbox.Click += new EventHandler(delegate {groupboxclick(groupbox, picturebox, i);});
Label label2 = new Label();
label2.Text = "Status: " + "Aktualny";
label2.ForeColor = Color.Green;
label2.AutoSize = true;
label2.Location = new Point(label1.Location.X, label1.Location.Y + 20);
Gry.Controls.Add(label2);
Label zapiszopis = new Label();
zapiszopis.Text = myCollection4[i];
zapiszopis.Visible = false;
Gry.Controls.Add((Control)groupbox);
//MessageBox.Show("pokaz mi wysokosc");
}
}
private void groupboxclick(GroupBox groupbox, PictureBox picturebox, int itest)
{
groupbox.ForeColor = Color.Aqua;
this.pictureBox1.BackgroundImage = picturebox.BackgroundImage;
opishacka.Text = myCollection4[itest];
}
The problem is that the event setup is using the variable K value. For use the number instead you probably needs to create an expression manually to use the current value in each case.
BUT
You can easily do what you want using the following properties to attach values to controls.
1-) Tag in WinForms & WPF:
// Setup
pictureBox.Tag = i;
// Event
int i = (int) pictureBox.Tag;
2-) ViewState in WebForms
// Setup
ViewState[pictureBox.UniqueID] = i;
// Event
int i = (int) ViewState[pictureBox.UniqueID];
You can use many other techniques. I only post one for each popular framework. I guest that you are in a WinFors project.
Hope this help!
I am creating List of Panel. Each panel Contain a Label and Button . I have also a Button(button1). I want to change the label text of (panels[0]) when click button1. How can I do this.This is my c# code:
List<Panel> panels = new List<Panel>();
private void Panel()
{
var x = 0;
for (int i = 1; i < 5; i++)
{
x += 60;
var panel1 = new Panel() { Size = new Size(60, 140), Location = new Point(x, 100), BorderStyle = BorderStyle.FixedSingle };
panel1.Name = "pan" + i;
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = i.ToString();
lbl.Location = new Point(10, 20);
panel1.Controls.Add(lbl);
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
panel1.Controls.Add(button);
panels.Add(panel1);
Controls.Add(panel1);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var p in panels)
{
}
}
Output:
But i want, When i Click button1 it will change Label text of zero index panels(I have pointed it using red mark).
Can anyone help me...
Ok, so you've got a button and a label within a panel. When you click a button of a panel, you wanna do something to the label in the same panel, right ?
So
private void BtnClick(object sender, EventArgs e) {
var button = (Button)sender;//you've got the button clicked
var panel = button.Parent;//you've got the panel.
//var label = panel.Controls.OfType<Label>().FirstOrDefault();//but don't think you get this in c# 3.0
var label = GetFirstLabel(panel);
if (label != null)
label.Text = "something";
}
private Label GetFirstLabel(Control parent) {
foreach (var control in parent.Controls) {
if (control is Label) return control as Label;
}
return null;
}
Usage
When you add your buttons, you can now do
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
button.Click += BtnClick;
And this will work on all panels.
If you don't store reference to that Label then you can find it in controls of the first Panel by type for example:
panels[0].Controls.OfType<Label>().First().Text = "New Text";
or by name
panels[0].Controls.OfType<Label>().Single(l => l.Name == "lbl1").Text = "New Text";
you can do this with a simple method:
private void button1_Click(object sender, EventArgs e)
{
Label l = panels[0].Controls.Find("lbl1", false).FirstOrDefault() as Label;
l.Text = "TEXT";
}
I have a TextBox, but I can't find any source explaining how to call a function when a button is pressed down.
public Simple()
{
Text = "Server Command Line";
Size = new Size(800, 400);
CenterToScreen();
Button button = new Button();
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.Parent = this;
button.Text = "SEND";
button.Size = new Size (50, 20);
button.Location = new Point(620, Size.Height-70);
button.Parent = this;
button.Click += new EventHandler(Submit);
}
Some sources tell me to use a function, but I don't understand how it is going to get called.
If I understood correctly you want to call a method when users press Enter while typing anything in textbox? If so, you have to use the KeyUp event of TextBox like this:
public Simple()
{
Text = "Server Command Line";
...
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.KeyUp += TextBoxKeyUp; //here we attach the event
txt.Parent = this;
Button button = new Button();
...
}
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Do something
e.Handled = true;
}
}
you already have a button as well like this
button.Click += new EventHandler(Submit);
if you want to call this function you can do this
button.PerformClick(); //this will call Submit you specified in the above statement