Solved

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

Posted on 2008-10-23
7
347 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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 is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

630 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