I have a stackpanel with dynamically created textboxes and buttons in my wpf application.
This works ok. Later in the application I have to use the name of the textboxes and the values. How do I do that.
I have this code:
First the creation of the textboxes i a stackpanel named panelBet.
Second a switch-case where the name and the value is used. Red lines under 'controls'.
First creation:
int f = 1;
foreach (TextBox txt2 in txtBet)
{
string name = "Bet" + f.ToString(); ;
txt2.Name = name;
txt2.Text = name.ToString();
txt2.Width = 100;
txt2.Height = 40;
txt2.Background = Brushes.Lavender;
txt2.Margin = new Thickness(3);
txt2.HorizontalAlignment = HorizontalAlignment.Left;
txt2.VerticalAlignment = VerticalAlignment.Top;
txt2.Visibility = Visibility.Visible;
panelBet.Children.Add(txt2);
f++;
}
Second switch-case:
private void cboRunder_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cboRunder = sender as ComboBox;
string strRunder = cboRunder.SelectedValue.ToString(); // blinds, preflop osv.
switch (strRunder)
{
case "Blinds":
string s = ((TextBox)panelBet.Controls["txtBet"]).Text;
}
}
This should get you a reference to the TextBox named "txtBet" in the panelBet assuming there is one:
TextBox txtBet = panelBet.Children.OfType<TextBox>()
.FirstOrDefault(x => x.Name == "txtBet");
Related
Hi Im trying to assign a label to a FlowLayoutPanel based on the Value of a cell
My Code:
foreach (DataGridViewRow row in datagridview.SelectedRows)
{
var Id = row.Cells["typeID"].Value.ToString();
int typeID;
if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out PlayerID))
{
Label label1 = new Label();
label1.Text = row.Cells["PlayerType"].Value.ToString();
//Does not matter for type of player
//flpGoalie.Controls.Add(label1);
if(label1.Text == "Goalie")
{
flpGoalie.Controls.Add(label1);
}
}
This Line flpGoalie.Controls.Add(label1); , assigns any label to that FlowLayoutPanel.
What im trying to do is Seperate the PlayerType into different FLP based on the value of the cell.
Since there is a pattern between the FLP names and the Text in the label1, you can use Controls.Find to find a particular Control in your Form like this:
foreach (DataGridViewRow row in datagridview.Rows.Cast<DataGridViewRow>())
{
var Id = row.Cells["typeID"].Value.ToString();
int typeID;
if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out PlayerID))
{
Label label1 = new Label();
label1.Text = row.Cells["PlayerType"].Value.ToString();
//Does not matter for type of player
//flpGoalie.Controls.Add(label1);
Control[] ctrls = Controls.Find("flp" + label1.Text, true);
if (ctrls != null && ctrls.Length > 0){
FlowLayoutPanel flp = ctrls[0] as FlowLayoutPanel;
if(flp != null)
flp.Controls.Add(label1);
}
}
Can something like this be done?
var test = 1;
label+test+.Text = "Some text goes here...";
Which would result in:
label1.Text = "Some text goes here...";
I wouldn't mind using switch-case if I had few cases, but I have like 40 labels that I would like dynamically assigned text depending on the variable value.
Use Controls.Find() in your form.
void Button1Click(object sender, EventArgs e)
{
var test = 1;
var labels = Controls.Find("label" + test, true);
if (labels.Length > 0)
{
var label = (Label) labels[0];
label.Text = "Some text goes here...";
}
}
var test = 1;
Control label = this.FindControl("label" + test);
if(label != null)
{
label.Text = "Some text goes here...";
}
More informationon FindControl is available at,
https://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol%28v=VS.100%29.aspx?f=255&MSPPError=-2147217396
I have like 40 labels that I would like dynamically assigned text
depending on the variable value
Here's another example, which is basically the same approach as Handoko's:
for(int i = 1; i <= 40; i++)
{
Label lbl = this.Controls.Find("label" + i.ToString(), true).FirstOrDefault() as Label;
if (lbl != null)
{
lbl.Text = "Hello Label #" + i.ToString();
}
}
I have created a method that allows me to find the next empty Box:
public int CheckBox(int boxNum) {
int BoxNumber = 0;
TextBox[] itemBoxArray = new TextBox[] { itemBox0, itemBox1, itemBox2, itemBox3, itemBox4, itemBox5, itemBox6, itemBox7,
itemBox8, itemBox9,itemBox10,itemBox11,itemBox12,itemBox13,itemBox14,itemBox15,};
for (int i = 0; i < itemBoxArray.Length; i++)
{
if (String.IsNullOrEmpty(itemBoxArray[i].Text))
{
BoxNumber = i;
i = 15;
}
}
return BoxNumber;
}
Next i created a button to check what the empty box is and i would like to input something into this box but i cannot find a way ro convert the string that carries the empty box number to that text box:
private void StandAroundRebar_Click(object sender, EventArgs e)
{
int emptybox = CheckBox(0);
string emptyboxString = emptybox.ToString();
string newbox = "itemBox" + emptyboxString;
MessageBox.Show("TextBox # " + newbox + " is empty ");
var textbox = this.Controls.Find(newbox, true);
}
}
}
Well, I would rather change the CheckBox method
public TextBox CheckBox() {
var itemBoxArray = new TextBox[] { itemBox0, itemBox1, itemBox2, itemBox3, itemBox4, itemBox5, itemBox6, itemBox7,
itemBox8, itemBox9,itemBox10,itemBox11,itemBox12,itemBox13,itemBox14,itemBox15,};
return itemBoxArray.First(m => string.IsNullOrEmpty(m.Text));//or FirstOrDefault
}
now you would get a TextBox returned, and could do whatever you want with it.
You need this function:
http://msdn.microsoft.com/de-de/library/486wc64h(v=vs.110).aspx
This will find it, all you have to do is casting it.
Then:
BoxNumber = i;
i = 15;
What should that do? You set the i to the box number and then you set it again to 15?!
That isn't supposed to work.
Why not just return the TextBox object directly?
public TextBox GetNextEmptyTextBox()
{
return (new[] { textBox1, textBox2, textBox3 })
.FirstOrDefault(tb => string.IsNullOrEmpty(tb.Text));
}
You need Control.ControlCollection.Find and than a cast to TextBox.
TextBox newBox = this.Controls.Find("itemBox1", true).FirstOrDefault() as TextBox;
Once you find your Box you can set the text:
newBox.Text = "my text";
I can't manage to get the values from textboxes that are created at run-time.
I want an user to choose something from a checkedlistbox, and to enter any values he wants in textboxes that are created at every button click.
How can I get the name of those texboxes? They really exist? I am a beginner and I really don't understand how they are created.
This is my code where I create textboxes.
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 466;
int y = 84;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
int i = 0;
TextBox tb = new TextBox();
tb.Location = new Point(x, y);
tb.Name = "txtBox" + i++.ToString();
Controls.Add(tb);
y += 30;
}
just place the i outside the foreach and done.
int i = 0;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
i++;
string textBoxName = "textBox" + i.ToString();
TextBox tb = new TextBox();
tb.Location = new Point(x, y);
//tb.Name = "txtBox" + i++.ToString(); <--Your Version
tb.Name = textBoxName;
//...
//Other stuff or your codes
}
Rather than searching for exact name what you can do is have a string(fixed) which is searched for the control.
so if you find that string ( in your case which is 'textbox' ), what you can do is search for that fixed string in the name of control. if it exists then it's dynamically generated control.
foreach(Control c in parentControlIdOrName.Controls)
{
if(c.GetType()==typeof(TextBox))
{
if(((TextBox)c).Name.indexOf("textbox")!=-1)
{
// do your coding here...what ever you want....
}
}
}
Haven't tested but,Hope for the best. It might work.
HI I have requirement that
1) display given no.of textboxes dynamically and save to DB
2) if I change the number then new textboxes should append to UI
EX: TextBox AddButton
If I give 2 in textbox and click on add
Then 2 textboxes should appear. I filled some data in those textboxes. Now When I change the value 2 to 5 then 3 more textboxes should append(condition:old textboxes data should retain)
If the second value is less than or equal to first value then do nothing.
My code is
void Append()
{
string Data = string.Empty;
TextBox tb;
if (Convert.ToInt32(hdnCnt.Value) < Convert.ToInt32(txtNoofGames.Text))
{
for (int i = 0; i < Convert.ToInt16(txtNoofGames.Text); i++)
{
if (i <= Convert.ToInt32(hdnCnt.Value))
{
tb = (TextBox)Form.FindControl("txtGame1");
Data = tb.Text;
}
TextBox Newtb = new TextBox();
Newtb.ID = "txtGame" + i;
Form.Controls.Add(Newtb);
if (i <= Convert.ToInt32(hdnCnt.Value))
{
Newtb.Text = Data;
}
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (hdnCnt.Value != "")
Append();
hdnCnt.Value = txtNoofGames.Text;
for (int i = 0; i < Convert.ToInt16(txtNoofGames.Text); i++)
{
TextBox tb = new TextBox();
tb.ID = "txtGame" + i;
Form.Controls.Add(tb);
}
}
I am getting exception "object reference not set to an instance of object" at Data = tb.Text; in append method.
You didn't initialize it from the looks of it
TextBox tb = new TextBox();
Hope that helps,
Instead of Form.Controls.Add(tb);
Please try with Page.Form.Controls.Add(tb);