Solved

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

Posted on 2014-10-21
5
313 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 33

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 33

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now