I have a form with 10 TextBoxes and OK button.
When the OK button was clicked. I need to store the values from the textboxes to a string of array.
Can someone help me please?
I need to store the values from the textboxes to a string of array.
string[] array = this.Controls.OfType<TextBox>()
.Select(r=> r.Text)
.ToArray();
The above expects the TextBoxes to be on the Form directly, not inside a container, if they are inside multiple containers then you should get all the controls recursively.
Make sure you include using System.Linq;.
If you are using lower frameworks than .Net Framework 3.5. Then you can use a simple foreach loop like:
List<string> list = new List<string>();
foreach(Control c in this.Controls)
{
if(c is TextBox)
list.Add((c as TextBox).Text);
}
(this would work with .Net framework 2.0 onward)
To get all textboxes not only the direct childs of the form (this)
Func<Control, IEnumerable<Control>> allControls = null;
allControls = c => new Control[] { c }.Concat(c.Controls.Cast<Control>().SelectMany(x => allControls(x)));
var all = allControls(this).OfType<TextBox>()
.Select(t => t.Text)
.ToList();
Related
i made this code for get all the groupboxes from a winform and then take only the ones with a determinated name.
Control.ControlCollection controles = this.Controls;
GroupBox gBoxAux = new GroupBox();
List<GroupBox> gBoxes = new List<GroupBox>();
foreach (Control c in controles)
{
if (c.GetType() == typeof(GroupBox))
{
gBoxAux = (GroupBox)c;
gBoxes.Add(gBoxAux);
}
}
I don't know if there's a better way to do it instead of iterate over all the controls.
Thank you very much!
You can query that using Linq:
this.Controls.OfType<GroupbBox>().Where(x=> x.Name == "SomeName").ToList();
Well to find all groupboxes there is no better way than to iterate over all of them. But (for me) the code would look better with this:
List<GroupBox> gBoxes = this.Controls.OfType<GroupbBox>().ToList();
OfType<T> selects all elements of a sequence that are of that type.
Note that this only finds all groupboxes directly contained in this ControlCollection but not in sub-containers. You may want to collect the groupboxes recursively:
public IEnumerable<GroupBoxes> GetAllGroupBoxes(Control c)
{
return c.Controls.OfType<GroupBox>()
.Concat(c.Controls.OfType<Control>().SelectMany(GetAllGroupBoxes));
}
List<GroupBox> gBoxes = GetAllGroupBoxes(this).ToList();
To filter for a specific name you can use Where:
Controls.OfType<GroupBox>().Where(gb => gb.Name == "whatever")...
I have a tab control that has listboxes on it some of which are created and named dynamically so I can't statically program their name. Is there a way to create an array of all the list box names on a give tabPage? I have been going nuts trying to figure out a way to do it.
it would look something like this (based on a winforms example)
List<string> listBoxNames = new List<string>();
foreach (Control control in tabPage1.Controls)
{
if (control.GetType() == typeof(ListBox))
{
listBoxNames.Add(control.Name);
}
}
Or the same thing in linq syntax
List<string> listBoxNames = (from Control control in tabPage1.Controls
where control.GetType() == typeof (ListBox)
select control.Name).ToList();
if you want to find all the listbox's in the tabpage again then see below
foreach (var listBoxName in listBoxNames)
{
ListBox listBox = (ListBox) tabPage1.Controls.Find(listBoxName, true)[0];
}
I want to capture all the textboxes values on button click in string without explicitly writing each of this line for each textbox in c# like
string atv1 = TextBox1.Text;
string atv2 = TextBox2.Text;
It should find all textboxes and make a string of it (join) .
can anyone help out !!
(window form when making some asp.net website (c#))
concise way :
String.Join(",",Form.Controls.OfType<TextBox>().Select(c => c.Text))
You can use a StringBuilder to "join" (most software types use the term concatenate) the strings. To get all the TextBoxes you can select any control on the form that is a text box. Simple solution is
var s = new StringBuilder();
foreach (var textbox in this.Controls.OfType<TextBox>())
{
s.AppendLine(textbox.Text)
}
Console.WriteLine(s.ToString());
However, a TextBox can be inside of a Control on the Form. So to handle this case you need recursion. I'll leave you to search StackOverflow to figure out how to do this.
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
var txt = (TextBox)c;
//Do Something
}
}
More elegant way:
var objTextboxValues = this.Controls.OfType<TextBox>().Select(obj => obj.Text).ToList();
var varJoinedText = String.Join(", ", objTextboxValues);
Original Answer:
var varAllTextBoxValues = "";
foreach (Control objControl in this.Controls)
if (objControl is TextBox)
varAllTextBoxValues += ((TextBox)objControl).Text;
}
MessageBox.Show(varAllTextBoxValues);
I have a List which has 16 Dictionary items, I want to assign the values of this 16 dictionaries into 16 different text fields. What I am doing now is this
txtAccountType.Text = SheetData[0]["KeyName"].ToString();
txtAccountName.Text = SheetData[1]["KeyName"].ToString();
txtAccountAddress.Text = SheetData[2]["KeyName"].ToString();
txtAccountActivationDate.Text = SheetData[3]["KeyName"].ToString();
txtAccountExpiry.Text = SheetData[4]["KeyName"].ToString();
SheetData is a instance of List class containing multiple dictionaries.
I thought of using the for loop as well but the problem is that it did not work because every time I used to see the last dictionaries value in all the text fields.
The above solution works fine for me but what if I get 15 dictionaries or 10 dictionaries in future, the solution I am using is not dynamic here so could you please suggest me on how can I improve this.
Its not possible to decide which data to assign for particular textbox. If any data can be assigned to any of the text box below code will work for you. It works like first element will be assigned to first textbox.
int i = 0;
foreach (Control ctl in controls)
{
if (ctl is TextBox)
{
TextBox txt = (TextBox)ctl;
txt.Text = SheetData[i]["KeyName"].ToString();
i++;
}
}
Here controls are the collection of ControlCollection object. For example you can collect it from form like this ControlCollection controls = this.form1.Controls;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I make a Control Array in C# 2010.NET?
i have 10 textbox in my window form can i write a source code in C# as in VB6 to access all the textbox with index value where all the textbox having the same name?
VB6 style Control arrays are not supported, but you can easily accomplish this by addiing each one the controls to a seperately-declared array or list.
private List<Textbox> txtSameName = new List<Textbox>();
in constructor, after InitializeComponent:
txtSameName.Add(txtOne);
txtSameName.Add(txtTwo);
txtSameName.Add(txtThree);
txtSameName.Add(txtFour);
then you can iterate by index or via foreach:
for (int 1 = 0; i < txtSameName.Length; i++)
{
txtSameName[i].Text = string.empty;
}
to wire up a common handler:
foreach (Textbox tb in txtSameName)
{
tb.TextChanged += new EventHandler(txtSameName_TextChanged);
}
and then a single handler as follows:
private void txtSameName_TextChanged(object sender, EventArgs e)
{
Textbox tb = sender as Textbox;
tb.BackColor = Colors.Yellow;
}
Name is irrelevant really in Winforms. You can just add the textboxes to an array and index them that way.
IF you only need to access the controls I think you could do something like this:
public TextBox[] TextBoxesArray
{
get
{
return Controls.OfType<TextBox>().Select(control => control).ToArray();
}
}
I'm not sure how to extend this to allow adding/removing TextBoxes from the array and update the Controls collection at the same time.
There is no "built-in" way like in VB6. However, assuming your text boxes are named txtBox0, txtBox1, etc., and there are fewer than 10...
If you use the method shown in this answer, then you could write something like:
var myTextBoxes =
this.FilterControls(c => c is TextBox)
.Where(c => c.Name != null && c.Name.StartsWith("txtBox"))
.OrderBy(c => c.Name)
.ToArray();
Now myTextBoxes should contain your array.