Solved

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

Posted on 2008-10-23
7
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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