C# Get text from a textbox that is made at runtime - c#

Hello I am making a program that has 2 textboxes and 2 buttons
When I press the add button then it will make 2 new textboxes using this code :
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
TextBox NEW_TEXTBOX_1 = new TextBox();
NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
//Set NEW_TEXTBOX_1 font
NEW_TEXTBOX_1.Font = new Font("Segoe Print", 9);
NEW_TEXTBOX_1.Font = new Font(NEW_TEXTBOX_1.Font, FontStyle.Bold);
//Set pos and size and then create it.
NEW_TEXTBOX_1.Location = new System.Drawing.Point(16, 71 + (35 * HOW_FAR));
NEW_TEXTBOX_1.Size = new System.Drawing.Size(178, 29);
this.Controls.Add(NEW_TEXTBOX_1);
//Make the PRICE_TEXTBOX_
TextBox NEW_TEXTBOX_2 = new TextBox();
NEW_TEXTBOX_2.Name = "PRICE_TEXTBOX_" + HOW_FAR.ToString();
//Set NEW_TEXTBOX font
NEW_TEXTBOX_2.Font = new Font("Segoe Print", 9);
NEW_TEXTBOX_2.Font = new Font(NEW_TEXTBOX_2.Font, FontStyle.Bold);
//Set pos and size and then create it.
NEW_TEXTBOX_2.Location = new System.Drawing.Point(200, 71 + (35 * HOW_FAR));
NEW_TEXTBOX_2.Size = new System.Drawing.Size(89, 29);
this.Controls.Add(NEW_TEXTBOX_2);
//Change pos of the add button
ADD_ROW.Location = new System.Drawing.Point(295, 71 + (35 * HOW_FAR));
this.Height = 349 + (35 * HOW_FAR);
this.Width = 352;
}
This works very well but now I want to get the text from a newly made textbox back how do I do this?
This doesn't work because it says : NAME_TEXTBOX_1 Does not exist in the current context.
private void button2_Click(object sender, EventArgs e)
{
string tmpStr = NAME_TEXTBOX_1.Text;
}

You need to move the variable declaration outside of the ADD_ROW_Click event handler so that it's accessible outside that block;
TextBox NEW_TEXTBOX_1;
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
NEW_TEXTBOX_1 = new TextBox(); //remove "TextBox" since we declared it above
NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
//...
The alternative, and possibly better depending on the number of textboxes, is to add each TextBox you create into a List. You can then iterate that List from and find the TextBox you want. For example
List<TextBox> allTextBoxes = new List<TextBox>();
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
TextBox NEW_TEXTBOX_1 = new TextBox();
//...fill out the properties
//add an identifier
NEW_TEXTBOX_1.Tag = 1;
allTextBoxes.Add(NEW_TEXTBOX_1);
}
Then when you want a particular TextBox
private void button2_Click(object sender, EventArgs e)
{
TextBox textBox1 = allTextBoxes.Where(x => x.Tag == 1).FirstOrDefault();
string tmpStr = "";
if(textBox1 != null)
tmpStr = textBox1.Text;
}
Alternatively, and especially if you're going to have a lot of TextBoxes, you could store them in a Dictionary as Corak suggested in the comments.

you're declaring NAME_TEXTBOX_1 within the ADD_ROW_Click method, which is why it isn't available within the button2_Cick method.
You can declare the textbox at the class level to access it in both places.
(You should work on renaming your variables too - e.g. TextBoxPrice)

One simple solution:
Make a private field called "NEW_TB" for example.
In your button2_Click(..) { string tmpStr = NEW_TB.Text; }
Add in your ADD_ROW_Click(..) method NEW_TB = NAME_TEXTBOX_1;
If I understood your question right, this should work.

Make global your textboxes:
TextBox NEW_TEXTBOX_1;
then initiate them in your method:
NEW_TEXTBOX_1 = new TextBox();

OMG Never mind sorry guys I found a good way :D
var text = (TextBox)this.Controls.Find("PRICE_TEXTBOX_1", true)[0];
text.Text = "PRO!";
This works pretty well :)

Related

C# delete dynamically added textbox and move all other textboxes down

I have really been struggling with this question for a long time.
I add a textbox and a button on tab press . I put text inside the textboxes:
Now, my question, how do I remove the textbox next to the button I click and move all the textboxes down, so I won't get any open space. If I press on the button next to the 7th textbox, I want it to look like this:
here's my code:
private void Form1_Load(object sender, EventArgs e)
{
//creates a textbox(t0) and a button(b0) on load
TextBox t0 = new TextBox();
t0.Name = "t0";
t0.Location = new Point(16, 12);
t0.Width = 200;
t0.PreviewKeyDown += new PreviewKeyDownEventHandler(PreviewKeyDown);
Button b0 = new Button();
b0.TabStop = false;
b0.Text = "x";
b0.Location = new Point(216, 11);
b0.Size = new System.Drawing.Size(20, 22);
b0.Click += new EventHandler(buttonclicked);
panel1.Controls.Add(t0);
panel1.Controls.Add(b0);
}
private new void PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//if I press tab in the last textbox it creates a new textbox(t + amount of textboxes) and button(b + amount of textboxes)
if (e.KeyData == Keys.Tab)
{
int counter2 = 0;
foreach (TextBox box in panel1.Controls.OfType<TextBox>())
{
counter2++;
}
counter2 = counter2 - 1;
string Name = "t" + Convert.ToString(counter2);
counter2++;
foreach (TextBox box in panel1.Controls.OfType<TextBox>())
{
if (Name == box.Name && box.Focused)
{
TextBox t0 = new TextBox();
Button b0 = new Button();
t0.Location = new Point(16, 12 + counter - panel1.VerticalScroll.Value);
t0.Width = 200;
t0.Name = "t" + Convert.ToString(counter2);
t0.PreviewKeyDown += new PreviewKeyDownEventHandler(PreviewKeyDown);
b0.TabStop = false;
b0.Text = "x";
b0.Name = "b" + Convert.ToString(counter2);
b0.Location = new Point(216, 11 + counter - panel1.VerticalScroll.Value);
b0.Size = new System.Drawing.Size(20, 22);
b0.Click += new EventHandler(buttonclicked);
panel1.Controls.Add(t0);
panel1.Controls.Add(b0);
counter = counter + 25;
}
}
}
}
private void buttonclicked(object sender, EventArgs e)
{
//Remove the textbox next to it.
}
Any help is appreciated
I suggest you use a TableLayoutPanel. Set the GrowStyle on the TableLayoutPanel to AddRows or AddColumns and then you can add/remove controls to it and it will resize automatically. Set the height of each row to a number a little bigger than the height of the textbox. Set the column widths to a value so the textbox and the button can fit in them. You need 2 columns in it.
You do not need to set the location of the controls to a static location. They will be handled by the TableLayoutPanel for you.
Here is how you can add new controls to it.
yourTableLayoutPanel.Controls.Add(yourTextbox1, 0 /* Column Index */, 0 /* Row index */);

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.

Removing textbox/label on button click C#

I am trying to remove textboxes and labels one by one by pressing a button.
I have a list of textboxes called inputTextBoxes.
Here is the code for adding :
private void onClickAdd(object sender, EventArgs e)
{
inputTextBoxes = new List<TextBox>();
Label label1 = new Label();
label1.Name = "label1";
label1.Text = "w" + i;
label1.Location = new System.Drawing.Point(5, 10 + (20 * i));
label1.Size = new System.Drawing.Size(30, 20);
this.Controls.Add(label1);
TextBox text1 = new TextBox();
text1.Name = "text1";
text1.Location = new System.Drawing.Point(35, 10 + (20 * i));
text1.Size = new System.Drawing.Size(25, 20);
inputTextBoxes.Add(text1);
this.Controls.Add(text1);
i++;
}
For removing I am trying this :
private void onClickRemove(object sender, EventArgs e)
{
foreach(TextBox text1 in inputTextBoxes)
{
this.Controls.Remove(text1);
}
}
But it removes only the last textbox added,clicking againg on the button doesn't do anything.
You are constantly creating a new list in your OnClickAdd() method:
inputTextBoxes = new List<TextBox>();
Try to check if the inputTextBoxes is null and only then do this line of code. Otherwise, just let the rest of the code run.
Also, remember about clearing the inputTextBoxes list after the onClickRemove() method finishes removing textboxes/labels.
You want to remove only one TextBox at a time, why do you need a foreach loop? just grab the last or first TextBox and if it is not null remove it from the Controls:
private void onClickRemove(object sender, EventArgs e)
{
var textBoxToRemove = inputTextBoxes.LastOrDefault();
// or
// var textBoxToRemove = inputTextBoxes.FirstOrDefault();
if (textBoxToRemove != null)
{
this.Controls.Remove(textBoxToRemove);
inputTextBoxes.Remove(textBoxToRemove);
}
}
Make sure you remove it from inputTextBoxes also so the next time you will ask to remove a TextBox it will not try to remove it again and go on to the next one.
Edit
#Piotr Nowak has pointed one more problem you have, you allocate a new list for inputTextBox every time you add a new TextBox, you should allocate the list only once when you create your class.
Remove this from onClickAdd method:
inputTextBoxes = new List<TextBox>();
And use this when you declare the list as a field it your class:
private readonly inputTextBoxes = new List<TextBox>();

c# link two dynamically added controls. (Windows form application)

This i a pretty basic question, but i can't find the solution anywere.
I have this code, that dynamically creates a combobox and a label when the user presses a button. My qustion is now, how can I link the combobox and the label so that the label shows what is selected in the combobox?
// Tilføjer combobox
ComboBox cboRun = new ComboBox();
cboRun.Name = "cboDynamic1" + c++;
cboRun.Location = new System.Drawing.Point(10, 10 + (20 * c));
cboRun.Size = new System.Drawing.Size(200, 25);
cboRun.BringToFront();
cboRun.Enter += CBox_Enter;
grb_MealOne.Controls.Add(cboRun);
// Tilføjer label
Label labRun = new Label();
labRun.Name = "labDynamic1" + c;
labRun.Location = new System.Drawing.Point(270, 10 + (20 * c));
labRun.Size = new System.Drawing.Size(25, 25);
labRun.BringToFront();
labRun.Text = "Neee";
grb_MealOne.Controls.Add(labRun);
i really dont have any idea on how to do this part. Have tried many different things!
Label L;
public void YourMethod()
{
//Create the `ComboBox1` and set the event `comboBox1_SelectedIndexChanged` to it
Label Y = new Label();
// ...
L = Y;
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
L.Text = "Something else";
}
this field (L) is your Label and you can use it anywhere.

c# use object before it's created

I dynamic create and trackbar on an event,
now i want a textbox tto be filled with the value of the trackbar.
but how am i possible to do that? since i'll get an error saying the dynamic created trackbar does not exist. which is logic
this is what i have so far.
TrackBar trackBar = new TrackBar();
trackBar.Name = "TrackbarWidth" + trackbarName++;
trackBar.Tag = "dispose";
trackBar.Maximum = 85;
trackBar.Minimum = 65;
trackBar.SmallChange = 5;
trackBar.TickFrequency = 5;
trackBar.Value = WidthValue;
trackBar.Location = new Point(175, 440 + (50 * trackbarName));
trackBar.Size = new System.Drawing.Size(100, 25);
this.Controls.Add(trackBar);
TextBox textBox = new TextBox();
textBox.Name = "TrackbarWidth" + TextboxName++;
textBox.Text = trackBar.Value.ToString();
textBox.Tag = "dispose";
textBox.Location = new Point(300, 440 + (50 * TextboxName));
textBox.Size = new System.Drawing.Size(30, 25);
this.Controls.Add(textBox);
lineWidth += 4;
}
#endregion
}
private void trackBar1_Scroll(object sender, EventArgs e){
textBox1.Text = trackBar1.Value.ToString();
}
The problem with this solution is that I cannot access the textbox or the trackbar in the trackBar1_Scroll method.
The easiest solution here to use use an anonymous event handler that is capable of closing over the two variables that you need. Include this just after you finish constructing the textbox:
this.Controls.Add(textBox);
trackBar.Scroll += (s, args) => {
textbox.Text = trackbar.Value.ToString();
};
The sender argument is always the control which triggered the event:
private void trackBar_Scroll(object sender, System.EventArgs e)
{
// TextBox also dynamic? One way is using ControlCollection.Find
textBox1 = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
if(textBox1 != null)
textBox1.Text = trackBar1.Value.ToString();
}
However, if you create it dynamically you also have to create the event handler:
TrackBar trackBar = new TrackBar();
trackBar.Scroll += this.trackBar_Scroll;

Categories