Convert Control.Name to Control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ricardosms
    New Member
    • Apr 2010
    • 2

    Convert Control.Name to Control

    Hello:
    I have a custom control with a Combobox that at form1_Load gets filled with the names of the controls with visual interface. From this ComboBox the user selects a control name and that control, if is docked, gets undocked and is moved in increments depending on where you click on a PictureBox, changing the control's ".Top" and ".Left" properties.
    Here is my question:
    How do I convert the string

    Code:
    ComboBox1.SelectedItem & ".Top"
    to a reference of the control whose name is contained in SelectedItem?

    Let's say that the user selects on the ComboBox the 4th item: IconPicture that is a PictureBox;
    How do I refer to IconPicture.Top or IconPicture.Lef t?
    How do I convert the string to a command to the PictureBox to move?

    This didn't work:

    Code:
             Dim Cntrl As Control = Me.Controls(ComboBox1.Text)
             Cntrl.BringToFront()
             Me.BringToFront()
             If Cntrl IsNot Nothing Then Cntrl.Dock = DockStyle.None
    I have solved the situation with a workaround cycling through the controls on the form:

    Code:
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            For Each ctl As Control In ParentForm.Controls
                If ctl.Name = ComboBox1.SelectedItem Then
                    Cntrl = ctl
                    Cntrl.Dock = DockStyle.None
                    Cntrl.BringToFront()
                    Me.BringToFront()
                    Exit Sub
                End If
            Next
    
        End Sub
    But, is there a way of doing it directly?

    My control has a PictureBox with an Image, a ComboBox and a Timer.

    Here Is the Complete code:

    Code:
    Imports System.Collections
    Imports System.Windows.Forms
    Imports System.Windows.Forms.Control
    
    Public Class NvGator
        Private TheX, TheY As New Integer
        Private Cntrl As Control
        Public DX, DY As Integer
    
        Private Sub NvGator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TheX = PictureBox1.Image.Width / 2
            TheY = PictureBox1.Image.Height / 2
            Me.BringToFront()
            Cntrl = Me
            ComboBox1.Items.Clear()
    
            For Each ctl As Control In ParentForm.Controls
                ComboBox1.Items.Add(ctl.Name)
            Next
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            For Each ctl As Control In ParentForm.Controls
                If ctl.Name = ComboBox1.SelectedItem Then
                    Cntrl = ctl
                    Cntrl.Dock = DockStyle.None
                    Cntrl.BringToFront()
                    Me.BringToFront()
                    Exit Sub
                End If
            Next
    
            ' Dim Cntrl As Control = Me.Controls(ComboBox1.Text)
            'Cntrl.BringToFront()
            'Me.BringToFront()
            '  If Cntrl IsNot Nothing Then Cntrl.Dock = DockStyle.None
    
        End Sub
    
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            Cntrl.Top = Cntrl.Top + (e.Y - TheY) / 2
            Cntrl.Left = Cntrl.Left + (e.X - TheX) / 2
            DX = e.X
            DY = e.Y
            Timer1.Enabled = True
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Cntrl.Top = Cntrl.Top + (DY - TheY) / 2
            Cntrl.Left = Cntrl.Left + (DX - TheX) / 2
    
        End Sub
    
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
            Timer1.Enabled = False
        End Sub
    End Class
    Thank you in advance for any tip or guidance.
    ricardosms
    Last edited by ricardosms; Apr 18 '10, 08:15 PM. Reason: Width and Height mix-up
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You do a .Find in the custom control's .Controls collection, to locate the specific control selected in the Combo box.
    Then you change it's .Location property.

    In C# it would go something like this...

    Code:
    string ControlName = myComboBox.SelectedItem as text;
    Control FoundControl = myCustomControl.Controls.Find(ControlName);//Now I have a handle to the requested control
    if (FoundControl != null)
    {// Never just assume you *actually* found something
       // Move it down 10 pixels, one at a time
       For (int Counter = 0; Counter<10; Counter++)
       {
           FoundControl.Location = New Point(FoundControl.Location.X,  FoundControl.Location.Y++)
       }
    
       // Move it right 10 pixels, one at a time
       For (int Counter = 0; Counter<10; Counter++)
       {
           FoundControl.Location = New Point(FoundControl.Location.X++, FoundControl.Location.Y)
       }
    }

    Comment

    • ricardosms
      New Member
      • Apr 2010
      • 2

      #3
      Hi, tlhintoq:

      Thank you for your reply, I translated it to VB like this:

      Code:
              Dim FoundControl() As Control
              Dim ControlName As String
              ControlName = ComboBox1.SelectedItem
              FoundControl = Me.Controls.Find(ComboBox1.SelectedItem, False)
              'Now I have a handle to the requested control 
              If (Not (FoundControl(0)) Is Nothing) Then
                  ' Never just assume you *actually* found something 
                  ' Move it down 10 pixels, one at a time 
                  For Counter = 0 To 10
                      FoundControl(0).Top = FoundControl(0).Top + 1
                      ' Move it right 10 pixels, one at a time
                  Next
                  For counter = 0 To 10
                      FoundControl(0).Left = FoundControl(0).Left + 1
                  Next
              End If
      And due to the situation that the answer is an array I used the first element; there is only one control with that full name, so has to be the first one.
      It works similarly than my loop. I guess there is not that better way that I was hoping to find. I don't see why is there a need to do a search or a loop if I already have the name of the control and there is only one control with that name.
      Isn't there a way to get a handle to the control just by the name?
      Thank you.
      Last edited by ricardosms; Apr 18 '10, 08:07 PM. Reason: grammar

      Comment

      Working...