0

I am developing an application using Windows Form and C#. How can I add the Text of all textboxes on the current tabpage to a List<string> Mydata. Here is my code below:

foreach(Control c in Controls){
    if((c is TextBox) && (c.Text !="")){
         if(c.Tag.ToString() == "500"){
             StaticClass.Mydata.Items.Add(c.Text);
         }
    }
}

NB: Mydata (List) is public on a static class. I have a form with a tabcontrol which contains about 6 tabpages. The other tabpages have textboxes, too, but I want to retrieve only those in my example on tabindex 5.

Thanks.

Max Play
  • 3,717
  • 1
  • 22
  • 39
  • 1
    Get the tabpage and call .Controls on it. Instead of this.Controls. – Derek Jul 13 '17 at 12:17
  • You can give a name to the textbox and then you can find it very easily https://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls. Because the chances are the textbox may not be the control of tabpage i.e. may control of any other control which is inside the tabpage. – lkdhruw Jul 13 '17 at 12:27
  • Texboxes has they names and the thing which is common to them is tag number. – Joel Mayer Mukanya Jul 13 '17 at 12:43
  • All textboxes has it own names – Joel Mayer Mukanya Jul 13 '17 at 12:43
  • Ikdhruw: The above question it not the same with the one on the link. I think I can try dereck answer and see if I can solve my logic – Joel Mayer Mukanya Jul 13 '17 at 12:48

2 Answers2

1

If I understand you correctly you need to get the text from all TextBoxes within a TabPage, but only if the Tag property is equal to the string "500".

Your code is basically right, but I've modified it slightly (removed some redundant parenthesis etc.). I'm assuming that your TabPage is named tabPage1, indexOfTabPage is the index of the TabPage and the results are placed in a local List.

You can just add the strings to a static list instead if you like.

int indexOfTabPage = 5;
List<string> myData = new List<string>();

foreach(Control c in this.tabControl1.TabPages[indexOfTabPage].Controls)
{
   if(c is TextBox && !string.IsNullOrEmpty(c.Text) && c.Tag != null && c.Tag.ToString() == "500")
   {
      myData.Add(c.Text);
   }
}
kkirk
  • 352
  • 1
  • 7
  • Thanks, you did answered my question. Let me try to implement it – Joel Mayer Mukanya Jul 13 '17 at 13:07
  • I have a static class ; where I define lstData as List object. Inside the form's construction just under InitializeComponent(); ClMyData.lstData = new List(); under the button on tabpage1 then I include the same code but It doesn't display those value on tabpage2 listbox – Joel Mayer Mukanya Jul 13 '17 at 13:21
  • @JoelMayerMukanya ok, you should make it List unless you want to store something other than type string in it. Otherwise, simply use StaticClass.Mydata.Add(c.Text) rather than myData.Add(c.Text). In your original example you had StaticClass.Mydata.Items.Add(c.Text); but List doesn't have an Items property. – kkirk Jul 13 '17 at 13:25
  • Am using List to store values. It was a mistake – Joel Mayer Mukanya Jul 13 '17 at 13:28
  • @JoelMayerMukanya If that's a question, you mentioned in your original question that you wanted "retrieve only those on example on tabindex 5", so that's pointing to tab index 5. – kkirk Jul 13 '17 at 13:34
  • In a class: namespace ExampleOnList { public static class ClMyData { public static List lstData; } } – Joel Mayer Mukanya Jul 13 '17 at 13:39
  • public frmMain() { InitializeComponent(); ClMyData.lstData = new List(); } – Joel Mayer Mukanya Jul 13 '17 at 13:41
  • Under Button submit on tabpage1: private void btnSubmit_Click(object sender, EventArgs e) { int indexOfTabpage = this.tabControl1.TabPages.Count-1; foreach (Control c in this.tabControl1.TabPages[indexOfTabpage].Controls) { if ( (c is TextBox) && (!string.IsNullOrEmpty(c.Text) ) && (c.Tag.ToString()=="500")) { if(c.Text != "") { ClMyData.lstData.Add(c.Text); } } } } – Joel Mayer Mukanya Jul 13 '17 at 13:42
  • @JoelMayerMukanya Yes, that should work. Are you seeing some unexpected results? – kkirk Jul 13 '17 at 13:47
  • I try to work it on the same example for textboxes on the form it didn't work. Foreach(Control c in this.Controls){ if(c is TextBox) { clData.lstData.Add(c.Test);}} do u have an idea why isn't working Thanks – Joel Mayer Mukanya Jul 13 '17 at 15:19
  • @JoelMayerMukanya Yes, it's because the textboxes are not children of the Form, but of the TabControl. See e.g. https://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – kkirk Jul 13 '17 at 17:01
0
        private void GetTextBoxValueFromCurrentTabPage()
        {
            List<string> lstTextBoxData = new List<string>();
            foreach (var textBox in tabControl1.TabPages[tabControl1.SelectedIndex].Controls.OfType<TextBox>())
            {
                lstTextBoxData.Add(textBox.Text.Trim());
            }
        }

        private void GetTextBoxValueFromAllTabPages()
        {
            List<string> lstTextBoxData = new List<string>();
            foreach (TabPage tabPage in tabControl1.TabPages)
            {
                foreach (var textBox in tabPage.Controls.OfType<TextBox>())
                {
                    lstTextBoxData.Add(textBox.Text.Trim());
                }
            }
        }