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
Expand|Select|Wrap|Line Numbers
- ComboBox1.SelectedItem & ".Top"
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.Left?
How do I convert the string to a command to the PictureBox to move?
This didn't work:
Expand|Select|Wrap|Line Numbers
- Dim Cntrl As Control = Me.Controls(ComboBox1.Text)
- Cntrl.BringToFront()
- Me.BringToFront()
- If Cntrl IsNot Nothing Then Cntrl.Dock = DockStyle.None
Expand|Select|Wrap|Line Numbers
- 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
My control has a PictureBox with an Image, a ComboBox and a Timer.
Here Is the Complete code:
Expand|Select|Wrap|Line Numbers
- 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
ricardosms