Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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