Solved

How to store form controls in ... Dictionary(Of String, Object)

Posted on 2014-10-21
5
344 Views
Last Modified: 2014-10-21
I have the following class:

Public Class ControlAccessibility

    Public Controls As New Dictionary(Of String, Object)

End Class

Open in new window


Question: How can I store all controls on the form and enter their names and object type?

("txtFirstName", TextBox)
("cboCustome", ComboBox)
etc.
0
Comment
Question by:Mike Eghtebas
[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
5 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 40395885
I'm not sure if understand your requirements, but try this code to populate a dictionary at run-time and print its content. To test just create any firm and run the code:

namespace ControlsDictionary
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<String, String> controls = new Dictionary<String, String>();
            foreach (Control contrl in this.Controls)
            {
                controls.Add(contrl.Name, contrl.GetType().ToString());
            }

            foreach ( string key in controls.Keys)
            {
                System.Diagnostics.Debug.WriteLine("Name = " + key + "; Type = " + controls[key]);
            }
        }
    }
}

Open in new window


Sample output:

Name = button2; Type = System.Windows.Forms.Button
Name = textBox1; Type = System.Windows.Forms.TextBox
Name = label1; Type = System.Windows.Forms.Label
Name = comboBox1; Type = System.Windows.Forms.ComboBox
Name = button1; Type = System.Windows.Forms.Button
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40395914
Thank you for the good post. Is it possible to give me vb version?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 40395915
Easy, let me 5 mins :)
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 40395924
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
        Dim controlsD As New Dictionary(Of String, String)
        For Each contrl As Control In Me.Controls
            controlsD.Add(contrl.Name, contrl.GetType.ToString)
        Next

        For Each key As String In controlsD.Keys
            Debug.WriteLine("Name = " & key & "; Type = " & controlsD(key))
        Next
    End Sub

Open in new window

0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40395931
Thank you.

Also there is a related question if you have couple of minutes.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_28541932.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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