Solved

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

Posted on 2008-10-23
7
333 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now