I am creating an application where a user will input grades and the program will output the weighted average. On load, it will ask for the number of categories for the assignments. The program will then dynamically create textboxes for the user to input information. The problem is that I can not figure out how to read the text that is inputed after I create the textboxes. Here is my code:
TextBox txtbx = new TextBox();
txtbx.Text = "";
txtbx.Name = "txtbx1";
txtbx.Location = new Point(10, 10);
txtbx.Height = 20;
txtbx.Width = 50;
Controls.Add(txtbx);
How can I change this code so I can find the current text in the box when the user submits?
If you are dynamically generating controls then obviously you won't be able to have a field for each one. But if you are trying to access the Controls collection for a named control, the ControlCollection can be indexed by name. After adding the text box with the specified name, you can simply do:
TextBox txtbx = (TextBox)Controls["txtbx1"];
You could use the FindControl method of the Page class.
This method takes a parameter which is the TextBox's ID, which you have to set upon creation:
txtbx.ID = "txtbx1";
Then you can select it:
TextBox txtbx1 = (TextBox)FindControl("txtbx1");
and use it.
Edit: Since the initial question added that he is refering to Windows Forms, my reply above is off-topic.
In Windows Forms, you should simply use a class member variable instead of a local variable. E.g.:
public partial class MyForm
{
...
private TextBox txtbx;
...
private void createControls()
{
txtbx = new TextBox();
txtbx.Text = "";
txtbx.Name = "txtbx1";
txtbx.Location = new Point(10, 10);
txtbx.Height = 20;
txtbx.Width = 50;
Controls.Add(txtbx);
}
private void someOtherFunction()
{
// Do something other with the created text box.
txtbx.Text = "abc";
}
}
This code for the Dynamically Add Textbox On Button Click
int count = 1;
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = count * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.count.ToString();
count = count + 1;
return txt;
}
private void Onbutton_Click(object sender, EventArgs e)
{
//Call the method AddNewTextBox that uses for Dynamically create Textbox
AddNewTextBox();
}
I hope this code will help you .
Thank You
Happy Coding:)
Keep a list of references of all text boxes on the form. Add the textBox reference to the list when you create them dynamically.
Then you can simply iterate through all text boxes in the list when you want to read their text.
Make sure that you name the text boxes as per their related category names. Then you can also Find the control in the list by their names.
class MyForm : Form
{
IList<TextBox> _textBoxes = new List<TextBox>();
private void AddTextBox(string categoryName){
var myTextBox = new TextBox();
myTextBox .Name = categoryName + "txtbx";
// set other properties and add to Form.Controls collection
_textBoxes.Add(myTextBox);
}
private TextBox FindTextBox(string categoryName)
{
return _textBoxes.Where( t => t.Name.StartsWith(categoryName)).FirstOrDefault();
}
}
All you need to do is set up an OnClick listener for your submit button and have it do something like this
private void OnSubmit(object sender, EventArgs args)
{
string yourText = txtbx.Text;
}
You'll have to keep a reference to the text box after you create it. yourText will contain the value you need. Hope this helps
Related
I have created a label called txt1 programmatically inside a private void when the app is running ( in runtime ), I want to change the text of this label inside another private void, but I can't get access to txt1 from another void!
script for creating the label dynamically:
private void labelCreate()
{
Label txt1 = new Label();
}
script for changing the text of txt1 which has been created in labelCreatevoid ( & this script doesn't work because txt1 has not been declared as a control ):
private void labelTextChange()
{
txt1.Text = "Hello World!";
}
Update 1: I need to create 100 labels which have different names then I will use a for statement for creating 100 labels. I can't declare 100 global variables. So I need to pass the variables instead of declaring them as global.
Update 2: Is it possible to declare 100 labels inside a for statement as global?
Update 3: Suppose that I want to fetch some data from a DB, I want to show them separately in unique labels. So I name each label & I change the texts of them according to different data that I get from DB! So I need 2 voids: one for creating the labels according to the number of rows that I get from DB & the other void for changing the texts of labels which I have created before!
Question: How can I have access on a control which has been created in different void? If there is an answer please share the link :)
Thanks
Declare Label Globally in your class
Label txt1;
private void labelCreate()
{
txt1 = new Label();
}
Than access in another method
private void labelTextChange()
{
txt1.Text = "Hello World!";
}
Edit 1
If you have number of Labels, While creating Labels you can store those objects in an array and when you need to change those text do it like
Label[] labelsArray //declare globally
private void labelTextChange()
{
// Get Label objects from array
labelsArray = { label1, label2, label3, label4, label5 };
for (int i = 0; i < labelsArray.Count(); i++)
{
labelsArray[i].Text = "Hello.. " + (i + 1).ToString();
}
}
If you have a dynamic number of labels you could use a list.
List<Label> labels;
private void labelCreate()
{
labels = new List<Label>();
for(int i = 0; i < 100; i++)
{
labels.Add(new Label());
}
}
private void labelTextChange()
{
// use the index or search for the name of the label
labels[42].Text = "Hello World!";
}
Here is some information about the lifetime of variables and their accessibility (where the variable can be read from and/or written to) you could find helpflul.
In your code Label1 is a private variable which is not accessible in
labelTextChange method.
You need to declare txt1 as class variable.
Label txt1;
private void labelCreate()
{
txt1 = new Label();
}
private void labelTextChange()
{
txt1.Text = "Hello World!";
}
The problem that you are facing has to do with the following:
When you create an object in a function, you can only use it in the function itself.
To be able to use a object (Label in this case), you have to declare it outside of the function, usually above all functions. (Label txt1;)
After having declared a Label outside of the function, you can use one function to instantiate the object (txt1 = new Label();)
In the other function you can set the .Text property of the label
EDIT 1:
What you're looking for is a field that can store multiple labels like a List, or an array. Considering the usage of this field, i'd say a List is a good option.
If you would want to make your life alot easier, i'd use a ListBox or a DataGridView instead of alot of labels though. As far as I know, labels aren't meant to be used in the way that you describe, there are other controls as i described above that are more suitable for this kind of application.
EDIT 2:
int amountOfRows;
Label[] labels;
public void setLabels(List<string> inputData)
{
//allocate memory for the array
amountOfRows = inputData.Count;
labels = new Label[amountOfRows];
for(int i=0; i<amountOfRows; i++)
{
labels[i] = new Label();
//set properties like location and size of labels here first
labels[i].Text = inputData[i];
}
}
I am trying to open a file inside a rich text box that is dynamically created in a tabpage on click. But it is inside a split container along with another element. For some reason when I try to access it, I get a Object reference not set to an instance of an object error.
Here's the code:
Dynamic creation of said tab page:
public class Texttab : TabPage
{
readonly RichTextBox _text = new RichTextBox();
ConsoleControl.ConsoleControl ca = new ConsoleControl.ConsoleControl();
private SplitContainer split = new SplitContainer();
public Texttab()
{
split.Dock = DockStyle.Fill;
split.Orientation = Orientation.Horizontal;
split.Name = "split";
_text.Dock = DockStyle.Fill;
_text.Name = "textbox";
_text.Font = fontx;
_text.BackColor = Color.FromName(back);
_text.ForeColor = Color.FromName(front);
ca.Dock = DockStyle.Fill;
ca.Name = "cmdbox";
ca.StartProcess("cmd", null);
ca.Font = fonty;
Controls.Add(split);
split.Panel1.Controls.Add(_text);
split.Panel2.Controls.Add(ca);
}
}
Code accessing the TEXTBOX and opening the text file:
private void OpenToolStripMenuItemClick(object sender, EventArgs e)
{
var dx = new OpenFileDialog();
dx.ShowDialog();
dx.Filter = Resources.Form1_openToolStripMenuItem_Click_Text_Files___txt____txt_Python_Files___py____py_Javascript_Files___js____js_C_Files___c____c_CPP_Files___cpp____cpp_Shell_Files___sh__bat____sh___bat_All_Files__________;
RichTextBox selectedRtb = (RichTextBox)tabControl1.SelectedTab.Controls["split"].Controls["textbox"];
selectedRtb.LoadFile(dx.FileName, RichTextBoxStreamType.PlainText);
}
Thanks, any help on the issue is appreciated. I am almost positive the issue has something to do with the split control. Thanks again!
You will have to access the RichTextBox like this instead since the Panels in the SplitContainer are not named items.
(RichTextBox)((SplitContainer )tabControl1.SelectedTab.Controls["split"]).Panel1.Controls["textbox"]
I have a problem I've been looking into for a few days now, I just can't think of a logical way of doing what I want.
I have an app which has a task list. It starts of with 3 controls: a textbox, datetimepicker and a PictureBox which changes image on click. The user can then press an image which will add another row of controls below (It gets the properties of the dynamic controls from the controls already created):
https://www.dropbox.com/s/o2pub6orww24w25/tasklist.png (This is a screenshot to make it clearer)
Now what I want to do is save the values from each of the rows (A row being defined as: Textbox, Date, Status) into an SQLite DB.
For the first row it is easy, because that has a unique design name (and is a 'static' control).
However, the problem hits when I attempt to save values from the dynamic controls:
Problem a) I cannot reference the dynamic control because 'It does not Exist in the current Context'. -The function for creating the controls has a public access modifier so I thought that should do the trick? -It didn't. I've also tried:Panel1.pb.blah but it still didn't recognize the control?
Problem b) How can I tell my program that each row is a new set of data? In other words, how can I run a new insert command for each row? -I thought of doing this as a for-each-textbox loop, however would that not just pick up the first dynamic date everytime?
I've also thought of using the tag property and setting it to the counter variable, to group the controls in the row. (The counter being an integer which increments every time a new row is added.) However I cannot do that because the picture box uses the tag property as part of its function to change image on click (Changes multiple times).
Code:
Adding the Controls:
public void pictureBox1_Click(object sender, EventArgs e)
{
//TextBox Control
int tbh = tasktb.Location.Y + (counter*25);
int tbsh = tasktb.Size.Height;
int tbsw = tasktb.Size.Width;
TextBox tb = new TextBox();
tb.Location = new Point(9, tbh);
tb.Size = new System.Drawing.Size(tbsw, tbsh);
tb.Tag = counter.ToString();
//Date Time Control
int dth = duedatedb.Location.Y + (counter * 25);
int dtsh = duedatedb.Size.Height;
int dtsw = duedatedb.Size.Width;
DateTimePicker dtp = new DateTimePicker();
dtp.Location = new Point(300, dth);
dtp.Size = new Size(dtsw, dtsh);
dtp.Format = System.Windows.Forms.DateTimePickerFormat.Short;
//Picture Box Control
int stsh = status.Location.Y + (counter * 25);
int stssh = status.Size.Height;
int stssw = status.Size.Width;
PictureBox pb = new PictureBox();
pb.Location = new Point(429, stsh);
pb.Size = new Size(stssw, stssh);
pb.Image = Red;
pb.Click += new System.EventHandler(pb_Click);
panel1.Controls.Add(tb);
panel1.Controls.Add(dtp);
panel1.Controls.Add(pb);
++counter;
}
Trying to Reference the control: (For purposes of changing the image on click) [Found the Control.Find function from researching this in the MSDN Website]
public void pb_Click(object sender, EventArgs e)
{
PictureBox pb = (panel1.Controls.Find("pb",false));
if (pb.Image == Red) { pb.Image = Orange; status.Tag = "Orange"; }
else if (pb.Image == Orange) { pb.Image = green; status.Tag = "Green"; }
else if (pb.Image == green) { pb.Image = Red; status.Tag = "Red"; }
}
The essential problem here is Problem a, if you guys could see where I have gone wrong with that, I'd be able to go away and attempt to write some code to get around problem b.
(I have included Problem b in this for your suggestions on the best way to do this. -At the moment I have no clue!)
Thank you for any help received! It really is appreciated!
ControlCollection.Find looks for a control with the specified name, and you haven't set any. The variable names in your code aren't related. So, either:
pb.Name = "pb";
But that would mean you'd eventually have several items with the same name. So, seeing how you want to change the picture of the clicked PictureBox, just do this:
public void pb_Click(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
if (pb.Image == Red) { pb.Image = Orange; status.Tag = "Orange"; }
else if (pb.Image == Orange) { pb.Image = green; status.Tag = "Green"; }
else if (pb.Image == green) { pb.Image = Red; status.Tag = "Red"; }
}
The sender argument always contains a reference to whichever control raised the event, in this case whichever picturebox was clicked!
Edit: As for your other question, I assume you'll need to do stuff to the controls later on, so I suggest you store a reference to all of them (or at least the ones you need), something like this:
// helper class
private class Entry
{
public TextBox TextBox { get; private set; }
public DateTimePicker DateTimePicker { get; private set; }
public PictureBox PictureBox { get; private set; }
public Entry( TextBox tb, DateTimePicker dtp, PictureBox pb )
{
this.TextBox = tb;
this.DateTimePicker = dtp;
this.PictureBox = pb;
}
}
// member field
private List<Entry> m_Entries = new List<Entry>();
// at the end of pictureBox1_Click
public void pictureBox1_Click(object sender, EventArgs e)
{
....
m_Entries.Add( new Entry( tb, dtp, pb ) );
}
Then you can use the items in that list to interact with your rows. You might also want to add an index, or a reference to whatever the original data structure is. Also, you might want to think about if you really should be creating the controls yourself like that or actually use some kind of table/grid control to host them!
Or perhaps just wrap up all those controls in a single UserControl, with logic included and all!
I don't know whether it is clear. I mean a form has an input textbox and a button. If I input 5 in the textbox and click on the button, the form will add 5 labels...
The question is I don't know it is 5 or 4 or 3……before the code is running and the input.
I don't know how to add the labels and how to define or get their names in order to use them later in the code.
I am just learning windows applications development with VS using C#....
And also this is my first ask in stackoverflow please forgive me if it is not clear. Is there anybody can help me?
let's split your entire problem into few steps of understanding:
What basically down the line, you are asking, is to how to add controls dynamically in a winform, in your case the control is label, so wrap your label creating logic in a function like below:
protected Label CreateLabel(string Id, string text)
{
Label lbl = new Label();
lbl.Name = Id;
lbl.Text = text;
return lbl;
}
Now you need to add as many labels as the number entered in a given textBox and upon a button click, so possibly something like below in button's click event:
protected void button_Clicked(object sender, EventArgs e)
{
//make sure nothing invalid string comes here
int counter = Convert.ToInt32(txtCount.text);
for(int i=0;i<counter;i++)
{
var lbl = CreateLabel("rand"+i, "Label" +i);
container.Controls.Add(lbl);//container can be your form
}
}
Now the basic problem in winforms you will face, will be about the positioning of these dynamically added labels. The most simple way to go about it is to add your labels to winforms FlowLayoutPanel. It automatically aligns the controls. There are other layout controls available aswell. so do this :
drag and drop a FlowLayoutPanel on your form and give it the name "container", rest assured
For example:
for(var i=0; i<N; i++ ) {
var l= new Label();
l.Text = "some name #" + i.ToString();
l.Width = 200;
l.Location = new Point(30, 20);
parent.Controls.Add(l);
}
You can use this as:
Label[] arrLabel;
int num = 0;
int.TryParse(textBox1.Text, out num);
arrLabel = new Label[num];
for (int i = 0; i < num; i++)
{
arrLabel[i] = new Label();
arrLabel[i].Text = "Label #" + (i+1);
arrLabel[i].Width = 20;
arrLabel[i].Location = new Point(30+10*(i+1), 20);
this.Controls.Add(arrLabel[i]);
}
I have an input field on my page where the user will type in the number of text inputs they want to create. The action for the button is:
int num_flds = int.Parse(a_fld.Text);
for (int i = 0; i < num_flds; i++)
{
TextBox tmp = new TextBox();
tmp.ID = "answer_box" + i;
tmp.Width = Unit.Pixel(300);
answer_inputs.Controls.Add(tmp);
}
Now, I have another button that the user would click after they have filled in all their dynamically-created text boxes. Questions, first of all, am I creating the text boxes dynamically in the correct place? How would I get the values out of the dynamically-created text boxes? (The dynamically-created text boxes are being added to the Panel "answer_inputs".
I recommend reading this and a few other articles about the topic of dynamically created controls. It is not quite as straightforward as you might think. There are some important page lifecycle issues to consider.
When creating web controls dynamically, I find it best to have the controls themselves report in the answers. You can achieve it like this:
Create something in your Page class to store the values:
private readonly Dictionary<TextBox, string> values=new Dictionary<TextBox, string>();
Make a method to act as a callback for the textboxes when their value changes:
void tmp_TextChanged(object sender, EventArgs e)
{
TextBox txt = sender as TextBox;
if(txt!=null)
{
values.Add(txt,txt.Text);
}
}
And then add this method to each textbox as they are added:
int num_flds;
if(!int.TryParse(a_fld.Text,out num_flds))
{
num_flds = 0;
}
for (int i = 0; i < num_flds; i++)
{
TextBox tmp = new TextBox();
tmp.ID = "answer_box" + i;
tmp.Width = Unit.Pixel(300);
answer_inputs.Controls.Add(tmp);
tmp.TextChanged += tmp_TextChanged;
}
Finally, you iterate through the dictionary on callback to see if it holds any values. Do this in the OnPreRender method for instance.
Edit: There is a problem with this, if the number of text fields are decreased on postback. Some safe way to recreate the previous textfields on postback should be employed.