Linking dynamically created text boxes and labels - c#

I created an array of of TextBoxes and an array of Labels. When the information is updated in the TextBox I want it to change the Labels. How would I be able to do this? Below is piece of my code. I have not created the EvenHandler that I think is the part I need help with. All in C# using windows application form.
textBoxes = new TextBox[value];
labels = new Label[value];
for (int i = 1; i < value; i++)
{
textBoxes[i] = new TextBox();
textBoxes[i].Location = new Point(30, ToBox.Bottom + (i * 43));
labels[i] = new Label();
labels[i].Location = new Point(TopBox3[i].Width + 140, TopBox3[i].Top +3);
textboxes[i].ValueChanged += new EventHandler(this.TextBox_ValueChanged) ;
this.Controls.Add(labels[i]);
this.Controls.Add(textBoxes[i]);
}

You can remember the index of the TextBox in the Tag property
textBoxes[i].Tag = i;
and then use this value in your eventhandler to get the corresponding label (assuming that you hold the labels array as a local variable)
protected void TextBox_ValueChanged(object sender, EventArgs e)
{
TextBox textbox = sender as TextBox;
if(textbox==null)
return;
int index = Convert.ToInt32(textbox.Tag);
if(index >= 0 && index < this.labels.Length)
{
Label label = this.labels[index];
/* ... */
}
}

You should write something like this:
private void textBox1_ValueChanged(object sender, EventArgs e)
{
TextBox changedTxt = sender as TextBox;
for (int i = 1; i < value; i++)
if (textBoxes[i] == changedTxt)
{
Label lblToChange = labeld[i];
lblToChange.Text = changedTxt.Text;
break;
}
}
In the method the TextBox whose text has changed is passed as "sender". You look into your array for it, so you identify the index "i" which can be used to access the corresponding Label and to set its text.
BTW as Tim said, the event is TextChanged, not ValueChanged. Furthermore be aware that the event is triggered for every change in the text, i.e. as soon as you press a key the label will be updated. If you prefer to update your labels only when the user has finished to enter its text Leave is the event you should use.

Related

Windows form receive text input

Hey guys/girls I got myself stuck and I was hoping I could get some help with it. simply said I'm trying to make a soccerpool on windows form. Because the player can put in as many team's as he/she wants I put the code that makes the betting panels in a (for)loop with the text as 0. very handy if I say so myself but now I can't retrieve the correct input from the user or without breaking the loop. any idea's?
for (int i = 0; i < hometable.Rows.Count; i++)
{
DataRow dataRowHome = hometable.Rows[i];
DataRow dataRowAway = awayTable.Rows[i];
Label lblHomeTeam = new Label();
Label lblAwayTeam = new Label();
TextBox txtHomePred = new TextBox();
TextBox txtAwayPred = new TextBox();
lblHomeTeam.TextAlign = ContentAlignment.BottomRight;
lblHomeTeam.Text = dataRowHome["TeamName"].ToString();
lblHomeTeam.Location = new Point(15, txtHomePred.Bottom + (i * 30));
lblHomeTeam.AutoSize = true;
txtHomePred.Text = "0";
txtHomePred.Location = new Point(lblHomeTeam.Width, lblHomeTeam.Top - 3);
txtHomePred.Width = 40;
txtAwayPred.Text = "0";
txtAwayPred.Location = new Point(txtHomePred.Width + lblHomeTeam.Width, txtHomePred.Top);
txtAwayPred.Width = 40;
lblAwayTeam.Text = dataRowAway["TeamName"].ToString();
lblAwayTeam.Location = new Point(txtHomePred.Width + lblHomeTeam.Width + txtAwayPred.Width, txtHomePred.Top + 3);
lblAwayTeam.AutoSize = true;
pnlPredCard.Controls.Add(lblHomeTeam);
pnlPredCard.Controls.Add(txtHomePred);
pnlPredCard.Controls.Add(txtAwayPred);
pnlPredCard.Controls.Add(lblAwayTeam);
So what my end goal is, is recieving the input from the user validating them and then storing them in a database.
Well, depending on how the user activates an event that requires the reading of the TextBox you have a few possible solutions.
Here is one where the TextBox (read all TextBox's) waits for enter:
private void Form_Load(object sender, EventArgs e)
{
while(someLoop)
{
TextBox theTextBox = new TextBox();
theTextBox.Name = "SomeUniqeName";//Maybe team name?
theTextBox.KeyUp += TheTextBox_KeyUp;
}
}
private void TheTextBox_KeyUp(object sender, KeyEventArgs e)
{
if ( e.KeyCode == Keys.Enter )
{
TextBox textbox = (TextBox) sender;//Get the textbox
//Just an example
listOfTeams.First( r => r.TeamName == textbox.Name )
.SomeOtherProperty = textbox.Text;
}
}
The textbox's are now identifiable by their name and all have an event. No matter how many you make.
If you will store the data later with 1 click of a button (and another loop) this solution might be better:
string[] Teams = { "teamA", "teamB", "teamC" };
private void Form1_Load(object sender, EventArgs e)
{
for ( int i = 0; i < Teams.Length; i++ )
{
TextBox theTextBox = new TextBox();
//Prefix the name so we know this is a betting textbox
//Add the 'name' (teams[i] in this case) to find it
theTextBox.Name = "ThePrefix" + Teams[i];
}
}
private void someButton_Click(object sender, EventArgs e)
{
//We want all betting textbox's here but also by team name
for ( int i = 0; i < Teams.Length; i++ )
{
//Because we set the name, we can now find it with linq
TextBox textBox = (TextBox) this.Controls.Cast<Control>()
.FirstOrDefault( row => row.Name == "ThePrefix" + Teams[i] );
}
}
This way each textbox is identifiable and won't conflict with other textbox's (because of 'ThePrefix'). This is essentially the other way around from the first method as it looks for the textbox based on data rather than data based on textbox name.

Get Value Dynamic Textbox in C#

I made dynamic textboxes and keep it List
private void ConvertButton_Click(object sender, EventArgs e)
{
List<TextBox> textBoxes = new List<TextBox>();
foreach (Control item in this.Controls)
{
if (item is TextBox)
{
TextBox txt = item as TextBox;
textBoxes.Add(txt);
}
}
}
I get all of textboxes value but i have a problem. For examples; if user add 3 label like (A,B,C) and add Textboxes for them like (labelA has 2,labelB has 3,labelC has 1) and textboxes get value like array (textboxes[0] has value).The problem is i dont know which label has which value.
I added Textbox just like this:
private void addNewTextbox(object sender, EventArgs e)
{
Button button = (Button)sender;
List<TextBox> textBoxes = button.Tag as List<TextBox>;
if (textBoxes == null)
button.Tag = textBoxes = new List<TextBox>();
TextBox textBox = new TextBox();
textBoxes.Add(textBox);
textBox.Location = new Point(90 * textBoxes.Count, button.Top);
textBox.Size = new Size(50, 50);
this.Controls.Add(textBox);
}
I try to show screen for example;
LabelA-->Textbox1 , Textbox2
</br>
labelB -->Textbox3
</br>
LabelC --> Textbox4 , Textbox5 , TextBox6
Every control that is added has a Name property. Use this property to link the different controls together. For example (and you should come up with your own naming convention), you could do this:
LabelA --> TextboxA1, TextboxA2
LabelB --> TextboxB1
LabelC --> TextboxC1, TextboxC2, TextboxC3
If you wish to have a more complete (i.e. complex) solution, you could:
Create your own control that inherits from TextBox
Add a property for the name of the associated Label control
Set this property when a new control is instantiated
Label[] labelArray = new Label[10];
for (int i = 0; i < labelNumber; i++)
{
labelArray[i] = new Label();
labelArray[i].Text = states[i] + "-->";
this.Controls.Add(labelArray[i]);
labelArray[i].Top = 100 + i * 30;
labelArray[i].Left = 10;
labelArray[i].Width = 30;
}
i did label-created just like this.labelnumber is how many label user wants.#interceptwind
I am trying very hard to understand your question, I guess you are trying to link the user-created textboxes to specific labels? Forgive me if I am completely wrong. How about structure your code to something like this:
Note: Edited with OP's new code
Dictionary<Int, List<TextBox>> label_Textboxes_Dict = new Dictionary<Int, List<TextBox>>();
List<Label> labelArray = new List<Label>(); //I suggest use list as you don't know the array size beforehand
void addLabel(int labelNumber)
{
int currentLabelArrayCount = labelArray.count; //So that users can add multiple times
for (int i = currentLabelArrayCount; i < currentLabelArrayCount +labelNumber; i++)
{
labelArray.Add(new Label());
labelArray[i].Text = states[i] + "-->";
this.Controls.Add(labelArray[i]);
labelArray[i].Top = 100 + i * 30;
labelArray[i].Left = 10;
labelArray[i].Width = 30;
label_Textboxes_Dict.Add(i, new List<TextBox>());
}
}
void addTextBoxForLabel(int labelNum)
{
TextBox t1 = new TextBox();
TextBox t2 = new TextBox();
//etc...
if (label_Textboxes_Dict.ContainsKey(labelNum))
{
label_Textboxes_Dict[labelNum].Add(t1);
label_Textboxes_Dict[labelNum].Add(t2);
}
}
void doSomethingForAllTextboxesOfLabel(int labelNum)
{
List<TextBox> listOfTextBoxes;
if(label_Textboxes_Dict.TryGetValue(labelNum, out listOfTextBoxes))
{
foreach(TextBox tb in listOfTextBoxes)
{
//do your stuff
}
}
}

Getting the value from a dynamic NumericUpDown by Sender c#

I've ran into quite an annoying problem.
I've got these global variables (for keeping the name etc)
List<object> NumeriekVakken = new List<object>();
List<decimal> bedragenLijst = new List<decimal>();
List<string> namenlijstVanNumericFields = new List<string>();
List<string> namenLijst = new List<string>();
Afterwards I have a function that makes NumericUpDowns, depending on the number of records in the db.
The function looks like this:
private void InitializeComponentControlArrayKnoppenTextboxenEnLabels()
{
foreach (DataRow dr in blCategorie.getAlleCategorieenMetLimieten())
{
double limiet = (double) dr.Field<double>("maximumBedrag");
NumericUpDown numeriekVak = new NumericUpDown();
numeriekVak.Name = "numeriekvak" + i;
numeriekVak.Width = 100;
numeriekVak.Maximum = 30000;
numeriekVak.Minimum = 0;
numeriekVak.Increment = 10;
numeriekVak.Value = Convert.ToDecimal(limiet);
numeriekVak.Location = new Point(250, beginhoogte + verhogenMet);
this.Controls.Add(numeriekVak);
NumeriekVakken.Add(numeriekVak);
bedragenLijst.Add(numeriekVak.Value);
namenlijstVanNumericFields.Add(numeriekVak.Name);
namenLijst.Add(categorie);
//to make unique names for my Numerics etc.
i++;
counter++;
//click event aanmaken
button.Click += new EventHandler(buttonWijzig_Click);
}
}
And in the ending I want to update a record whenever the numericUpDown is changed by the user (by clicking on the numericupdown or changing the numbers)
private void buttonWijzig_Click(object sender, EventArgs e)
{
Button knop = (Button)sender;
NumericUpDown numeriekvak = (NumericUpDown)sender;
for (int i = 0; i < counter; i++)
{
if (knop.Name == "knop" + i)
{
int id = i, maximumBedrag = 0;
if (namenlijstVanNumericFields[i] == "numeriekvak" + i)
{
// update limit
DBManager.LimietRow limiet = new DBManager.LimietDataTable().NewLimietRow();
maximumBedrag = Convert.ToInt32(numeriekvak.Value);
blLimiet.updateLimiet(id, maximumBedrag);
}
labelBevestigingLimiet.Text = "Limiet " + namenLijst[i].ToString() + " is succesvol gewijzigd naar " + maximumBedrag + "€";
//stopping of loop if right button is found.
break;
}
}
}
But evertime I run this I get the same problem..
"Can't convert the object from the type System.Windows.Forms.Button to the type
System.Windows.Forms.NumericUpDown"
How can I fix this and update the record depending on the new number thats filled in on the NumericUpDown? I can't get this working, I've got a feeling it has to do with the sender thats not working good..
Thanks for all the help!
Yenthe
You have assigned the event handler buttonWijzig_Click to a control button.
This control is not defined anywhere in your code above.
(By the way, you assign the method at the same control for every loop)
I think you want to assign your event handler to every NumericUpDown created in the loop as
numeriekVak.Click += new EventHandler(buttonWijzig_Click);
Of course the event handler now receives a NumericUpDown control in the sender argument and not a button, so the code of the event handler should be changed accordingly
private void buttonWijzig_Click(object sender, EventArgs e)
{
NumericUpDown numeriekvak = (NumericUpDown)sender;
int id = 0, maximumBedrag = 0;
// The ID could be extracted from the control name starting at 11 position
id = Convert.ToInt32(numeriekvak.Name.Substring(11));
// update limit
DBManager.LimietRow limiet = new DBManager.LimietDataTable().NewLimietRow();
maximumBedrag = Convert.ToInt32(numeriekvak.Value);
blLimiet.updateLimiet(id, maximumBedrag);
// The control name is already available, no need to use the list to retrieve it
labelBevestigingLimiet.Text = "Limiet " + numeriekVak.Name + " is succesvol gewijzigd naar " + maximumBedrag + "€";
}
However, let me say that for your stated purpose:
And in the ending I want to update a record whenever the numericUpDown
is changed by the user (by clicking on the numericupdown or changing
the numbers)
it is better to use the ValueChanged event because this will be fired also when the user changes the value manually and not with up/down buttons. The code above will fit as well for the ValueChanged event
EDIT
Based on your comment below then the assignment of the event handler goes back to the button (there is no code to create the button, so I have assumed that you have followed the same naming convention as for yours NumericUpDown), but insted of using a list to keep track of your NumericUpDown I would use a Dictionary<int, NumericUpDown> where the integer is the id needed to retrieve the corresponding NumericUpDown from the button name.
In declaration change
Dictionary<int, NumericUpDown> NumeriekVakken = new Dictionary<int, NumericUpDown> ();
In Initialization inside InitializeComponentControlArrayKnoppenTextboxenEnLabels change
namenlijstVanNumericFields.Add(i, numeriekVak);
In button click code
private void buttonWijzig_Click(object sender, EventArgs e)
{
Button knop = sender as Button;
int id = 0, maximumBedrag = 0;
// The ID could be extracted from the control name starting at 4th position
id = Convert.ToInt32(knop.Name.Substring(4));
// The ID is the key to find the corresponding NUmericUpDown in the dictionary
NumericUpDown numeriekvak = NumeriekVakken[id];
// update limit
DBManager.LimietRow limiet = new DBManager.LimietDataTable().NewLimietRow();
maximumBedrag = Convert.ToInt32(numeriekvak.Value);
blLimiet.updateLimiet(id, maximumBedrag);
// The control name is already available, no need to use the list to retrieve it
labelBevestigingLimiet.Text = "Limiet " + numeriekVak.Name + " is succesvol gewijzigd naar " + maximumBedrag + "€";
}
The problem is: since you event is fired by a button, you cannot convert the sender to numeric updown.
I see you have only one button and it's not declared in you code.
I assume it's in the form, right???
Did you mean to add that click event many times to a single button???
Or did you mean to add events to each NumericUpDown???
If it's the second option, you should add the click event to each numeric updown.
See this line in your code.
//click event aanmaken
button.Click += new EventHandler(buttonWijzig_Click);
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex >= 0) {
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
iD_supplierNumericUpDown.Value = row.Cells["ID"].Selected(); // this is the problem
nSupplierTextBox.Text = row.Cells["NSupplier"].Value.ToString();
e_mailTextBox.Text = row.Cells["E_mailTextBox"].Value.ToString();
phoneTextBox.Text = row.Cells["Phone"].Value.ToString();
}

How to create many labels and textboxes dynamically depending on the value of an integer variable?

Is there any way to dynamically create and display 'n' Labels with 'n' corresponding Textboxs when we know value of 'n' after for example, clicking "Display" button.
Let me know if anything make you don't understand my question. Thank you!
I am working with VS C# Express 2010 Windows Form.
I would create a user control which holds a Label and a Text Box in it and simply create instances of that user control 'n' times. If you want to know a better way to do it and use properties to get access to the values of Label and Text Box from the user control, please let me know.
Simple way to do it would be:
int n = 4; // Or whatever value - n has to be global so that the event handler can access it
private void btnDisplay_Click(object sender, EventArgs e)
{
TextBox[] textBoxes = new TextBox[n];
Label[] labels = new Label[n];
for (int i = 0; i < n; i++)
{
textBoxes[i] = new TextBox();
// Here you can modify the value of the textbox which is at textBoxes[i]
labels[i] = new Label();
// Here you can modify the value of the label which is at labels[i]
}
// This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBoxes[i]);
this.Controls.Add(labels[i]);
}
}
The code above assumes that you have a button btnDisplay and it has a onClick event assigned to btnDisplay_Click event handler. You also need to know the value of n and need a way of figuring out where to place all controls. Controls should have a width and height specified as well.
To do it using a User Control simply do this.
Okay, first of all go and create a new user control and put a text box and label in it.
Lets say they are called txtSomeTextBox and lblSomeLabel. In the code behind add this code:
public string GetTextBoxValue()
{
return this.txtSomeTextBox.Text;
}
public string GetLabelValue()
{
return this.lblSomeLabel.Text;
}
public void SetTextBoxValue(string newText)
{
this.txtSomeTextBox.Text = newText;
}
public void SetLabelValue(string newText)
{
this.lblSomeLabel.Text = newText;
}
Now the code to generate the user control will look like this (MyUserControl is the name you have give to your user control):
private void btnDisplay_Click(object sender, EventArgs e)
{
MyUserControl[] controls = new MyUserControl[n];
for (int i = 0; i < n; i++)
{
controls[i] = new MyUserControl();
controls[i].setTextBoxValue("some value to display in text");
controls[i].setLabelValue("some value to display in label");
// Now if you write controls[i].getTextBoxValue() it will return "some value to display in text" and controls[i].getLabelValue() will return "some value to display in label". These value will also be displayed in the user control.
}
// This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
for (int i = 0; i < n; i++)
{
this.Controls.Add(controls[i]);
}
}
Of course you can create more methods in the usercontrol to access properties and set them. Or simply if you have to access a lot, just put in these two variables and you can access the textbox and label directly:
public TextBox myTextBox;
public Label myLabel;
In the constructor of the user control do this:
myTextBox = this.txtSomeTextBox;
myLabel = this.lblSomeLabel;
Then in your program if you want to modify the text value of either just do this.
control[i].myTextBox.Text = "some random text"; // Same applies to myLabel
Hope it helped :)
Here is a simple example that should let you keep going add somethink that would act as a placeholder to your winform can be TableLayoutPanel
and then just add controls to it
for ( int i = 0; i < COUNT; i++ ) {
Label lblTitle = new Label();
lblTitle.Text = i+"Your Text";
youlayOut.Controls.Add( lblTitle, 0, i );
TextBox txtValue = new TextBox();
youlayOut.Controls.Add( txtValue, 2, i );
}
Suppose you have a button that when pressed sets n to 5, you could then generate labels and textboxes on your form like so.
var n = 5;
for (int i = 0; i < n; i++)
{
//Create label
Label label = new Label();
label.Text = String.Format("Label {0}", i);
//Position label on screen
label.Left = 10;
label.Top = (i + 1) * 20;
//Create textbox
TextBox textBox = new TextBox();
//Position textbox on screen
textBox.Left = 120;
textBox.Top = (i + 1) * 20;
//Add controls to form
this.Controls.Add(label);
this.Controls.Add(textBox);
}
This will not only add them to the form but position them decently as well.
You can try this:
int cleft = 1;
intaleft = 1;
private void button2_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = cleft * 40;
txt.Size = new Size(200, 16);
txt.Left = 150;
cleft = cleft + 1;
Label lbl = new Label();
this.Controls.Add(lbl);
lbl.Top = aleft * 40;
lbl.Size = new Size(100, 16);
lbl.ForeColor = Color.Blue;
lbl.Text = "BoxNo/CardNo";
lbl.Left = 70;
aleft = aleft + 1;
return;
}
private void btd_Click(object sender, EventArgs e)
{
//Here you Delete Text Box One By One(int ix for Text Box)
for (int ix = this.Controls.Count - 2; ix >= 0; ix--)
//Here you Delete Lable One By One(int ix for Lable)
for (int x = this.Controls.Count - 2; x >= 0; x--)
{
if (this.Controls[ix] is TextBox)
this.Controls[ix].Dispose();
if (this.Controls[x] is Label)
this.Controls[x].Dispose();
return;
}
}

Click Event for multiple Textboxes

So I need a way for when a person clicks on a textbox inan 8x8 grid of textboxes, the text in the textbox they have clicked on is changed to something. My grid is set up in a variable called textboxes[,] so if you type textboxes[0,0] you get the first box in the grid. As of now, with my very limited knowledge, I have this.
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
textboxes[i, j].Click += new EventHandler(textboxes_Click);
}
}
Then I can handle whenever one of the boxes is clicked. If you have a better way of doing this, I would love to hear it.I just dont know how to access the box that was clicked, mainly the text. Hope I have explained this well enough. Thanks for all the help!
-Lewis
Your approach is good. You only have to define some additional information to handle it in the event, as follows:
We can define a class to store the textbox position:
public class GridIndex
{
//stores the position of a textbox
public int ipos { get; set; }
public int jpos { get; set; }
}
Your piece of code sightly modified:
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
textboxes[i, j].Click += new System.EventHandler(this.textBox_Click);
textboxes[i, j].Tag = new GridIndex() { ipos = i, jpos = j };
}
And then your handler:
private void textBox_Click(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
//Here your have the text of the clicked textbox
string text = textBox.Text;
//And here the X and Y position of the clicked textbox
int ipos = (textBox.Tag as GridIndex).ipos;
int jpos = (textBox.Tag as GridIndex).jpos;
}
}
Edit: I did some changes to the code, please, review.
Your EventHandler has an object called sender as parameter. You have to cast it to an TextBox, then you can get the text of the textbox.
Your event handler has the signature:
void Handler(object sender, EventArgs args)
Where sender is a reference to the TextBox that was clicked. If you also need to know i * j at this point I'd created a class that derives from TextBox which has those numbers stored within it.
You can get the text box values by writting the following code
TextBox txt= (TextBox)sender;
string text = txt.Text.ToString();
MessageBox.show(text);
Hope this will be help full for u

Categories