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
Solved

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

Posted on 2014-10-21
5
327 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
  • 3
  • 2
5 Comments
 
LVL 29

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 29

Expert Comment

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

790 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