[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

How to get Control Reference


Hello!

How to get Control Reference, if path to control looks like:

Me
    TabControl
             SplitContainer.Panel1
                        SplitContainer.Panel1
                                             GroupBox
                                                     SplitContainer

All those controls creates at the run time, the last SplitContainer has name
Group_100, I’m trying to add new control into last SplitContainer, but getting error control doesn’t   exist.
How to get that control?

Thanks a lot.
0
bogdem
Asked:
bogdem
  • 4
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
Like this:

Imports System.Reflection
Imports System.Windows.Forms

Public Class ApplicationControls

  Public Shared Function FindControl(ByVal owner As Form, ByVal name As String) As Control

    Dim propInfo As PropertyInfo = _
     owner.GetType().GetProperty(name, BindingFlags.IgnoreCase Or _
     BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public)

    If Not propInfo Is Nothing Then
      Dim value As Object = propInfo.GetValue(owner, Nothing)
      If TypeOf value Is Control Then
        Return value
      End If
    End If

    Return Nothing

  End Function  

End Class

Example:
Dim control As Control = ApplicationControls.FindControl(Me, "Group_100")

Bob
0
 
bogdemAuthor Commented:
Sorry, didn't work for me :(
0
 
Bob LearnedCommented:
Did you get past this line?

    If Not propInfo Is Nothing Then

Bob
0
Technology Partners: 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!

 
bogdemAuthor Commented:
No I din't pass that line
0
 
bogdemAuthor Commented:
I have on form1
SplitContainer1
Button1

your class

and this code
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim but As New Button
        but.Text = "Hi"
        but.Name = "but_name"
        but.Enabled = True
        but.Visible = True
        but.Location = New System.Drawing.Point(10, 20)
        but.Size = New System.Drawing.Point(40, 65)

        SplitContainer1.Panel1.Controls.Add(but)
        but.BringToFront()
        SplitContainer1.Panel1.Refresh()

        'CheckControls(Me)
        Dim control As Control = ApplicationControls.FindControl(Me, "but_name")
 End Sub

Control = Nothing


0
 
Bob LearnedCommented:
Confused :(

What are you trying to do?  Why would you want to find a control where you already have a reference?

Bob
0
 
bogdemAuthor Commented:
this is my test code, in real life, I have to find my control.
For some reason in my test code I'm not able to find control with name = but_name  
:(
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Silly question, sorry!

I see now that you have SplitContainers so you must be using VB.Net 2005...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim but As New Button
        but.Text = "Hi"
        but.Name = "but_name"
        but.Enabled = True
        but.Visible = True
        but.Location = New System.Drawing.Point(10, 20)
        but.Size = New System.Drawing.Point(40, 65)

        SplitContainer1.Panel1.Controls.Add(but)
        but.BringToFront()
        SplitContainer1.Panel1.Refresh()

        Dim ctrlName As String = "but_name"
        Dim ctrl As Control = GetControl(Me, ctrlName)
        If Not IsNothing(ctrl) Then
            MessageBox.Show(ctrl.Name, "Control Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(ctrlName, "Control Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Public Function GetControl(ByVal frm As Form, ByVal ctrlName As String) As Control
        Dim match As Control = Nothing
        Dim matches() As Control = frm.Controls.Find(ctrlName, True)
        If matches.Length > 0 Then
            match = matches(0)
        End If
        Return match
    End Function

End Class
0

Featured Post

Technology Partners: 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!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now