Solved

Parse my winForm to store all textbox values into an array.

Posted on 2008-10-23
7
341 Views
Last Modified: 2013-11-26
Hi, I have created a form with textbox and droplist. I have named them by group. Eg :

private System.Windows.Forms.TextBox p1_ctl00_CPPC_ctrlShipToAddress_txtName;
private System.Windows.Forms.TextBox p1_ctl00_CPPC_ctrlShipToAddress_txtDepartment;
private System.Windows.Forms.ComboBox p1_ctl00_CPPC_ctrlShipToAddress_ddlCountry;
...

Have you may notice, they all start by "p1_" because I have an other group that I have named "p2_" and so one, so I can individualy manage group fields.

What I need to do is to parse the whole form and store their name excluding the "p1_" in a Array[] because de name of the item refers to a specific name on a webApplication.

I am pretty new to C# and need assistance to figure this out.

Thanks,
0
Comment
Question by:maxleb
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 22787922
Use the controls collection

Can you translate from VB.NET?

For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                    'parse ctrl.name and add to array
            endif
Next
0
 

Author Comment

by:maxleb
ID: 22788228
public void storeArray(string prefix){
           
            ArrayList p1 = new ArrayList();

            foreach (Control ctrl in Form1){
                if (ctrl.Name.Substring(0, 3) == "p1_")
                {
                    p1.Add(ctrl.Name.Replace("p1_",""));
                }
            }


But in "Control ctrl in Form1" I don't have the .Controls and gives me an error.

Error    1    'PuroWS.Form1' is a 'type' but is used like a 'variable'  

Any ideas?


0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 250 total points
ID: 22788322
Do you need this.Controls instead of Form1?

public void storeArray(string prefix){
           
           ArrayList p1 = new ArrayList();

           foreach (Control ctrl in this.Controls){
               if (ctrl.Name.Substring(0, 3) == "p1_")
               {
                   p1.Add(ctrl.Name.Replace("p1_",""));
               }
           }
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22788550
Hi maxleb;

Here is a code snippet that should do what you want.

Fernando

private void button1_Click(object sender, EventArgs e)
{
    StoreControls("textBox");
}
 
private List<Control> controlList = new List<Control>();
 
public void StoreControls(string prefix)
{
    foreach (Control ctl in this.Controls)
    {
        if (ctl.Name.StartsWith(prefix))
        {
            controlList.Add(ctl);
        }
    }
}

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 22788650
Hi maxleb;

Seeming you only want the name of the control without the prefix this should work.

        private List<String> controlList = new List<String>();

        public void StoreControls(string prefix)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl.Name.StartsWith(prefix))
                {
                    controlList.Add(ctl.Name.Substring(prefix.Length));
                }
            }
        }

Fernando
0
 

Author Comment

by:maxleb
ID: 22788754
Ok, getting close,

Just a little issues.

I have added MessageBox.Show(ctl.Name); before the condition and it shows everything except TextBox and Droplist.. Shows only Button controls names and GroupBox controls name.

I have tried to change <Control> by <TextBox> and it is not working still

Sorry about my ignorance, is there a reason why?



        private List<Control> p1 = new List<Control>();
 
        public void StoreControls(string prefix)
        {
            foreach (Control ctl in this.Controls)
            {
                MessageBox.Show(ctl.Name);
                if (ctl.Name.StartsWith(prefix))
                {
                    p1.Add(ctl);
                }
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            StoreControls("p1_");
        }

Open in new window

0
 

Author Comment

by:maxleb
ID: 22788850
Hi,

I have found the problem. Since all my fields where in a groupBox, it was excluded from the form itself.

I have added the group name and everything when great.
Thanks

this.grShipTo.Controlsthis.grShipTo.Controls
        private List<Control> p1 = new List<Control>();
 
        public void StoreControls(string prefix)
        {
            foreach (Control ctl in this.grShipTo.Controls)
            {
                MessageBox.Show(ctl.Name);
                if (ctl.Name.StartsWith(prefix))
                {
                    p1.Add(ctl);
                }
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            StoreControls("p1_");
        }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 45
defining NULL or 0 10 44
transition to visual .net from vb6 5 41
Code enhancement 4 22
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question