Solved

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

Posted on 2008-10-23
7
338 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
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.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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