?
Solved

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

Posted on 2014-10-21
5
Medium Priority
?
351 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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