473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. ComboBox1.SelectedItem & ".Top"
  2.  
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.Left?
How do I convert the string to a command to the PictureBox to move?

This didn't work:

Expand|Select|Wrap|Line Numbers
  1.          Dim Cntrl As Control = Me.Controls(ComboBox1.Text)
  2.          Cntrl.BringToFront()
  3.          Me.BringToFront()
  4.          If Cntrl IsNot Nothing Then Cntrl.Dock = DockStyle.None
  5.  
I have solved the situation with a workaround cycling through the controls on the form:

Expand|Select|Wrap|Line Numbers
  1.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2.         For Each ctl As Control In ParentForm.Controls
  3.             If ctl.Name = ComboBox1.SelectedItem Then
  4.                 Cntrl = ctl
  5.                 Cntrl.Dock = DockStyle.None
  6.                 Cntrl.BringToFront()
  7.                 Me.BringToFront()
  8.                 Exit Sub
  9.             End If
  10.         Next
  11.  
  12.     End Sub
  13.  
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:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections
  2. Imports System.Windows.Forms
  3. Imports System.Windows.Forms.Control
  4.  
  5. Public Class NvGator
  6.     Private TheX, TheY As New Integer
  7.     Private Cntrl As Control
  8.     Public DX, DY As Integer
  9.  
  10.     Private Sub NvGator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11.         TheX = PictureBox1.Image.Width / 2
  12.         TheY = PictureBox1.Image.Height / 2
  13.         Me.BringToFront()
  14.         Cntrl = Me
  15.         ComboBox1.Items.Clear()
  16.  
  17.         For Each ctl As Control In ParentForm.Controls
  18.             ComboBox1.Items.Add(ctl.Name)
  19.         Next
  20.  
  21.     End Sub
  22.  
  23.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  24.         For Each ctl As Control In ParentForm.Controls
  25.             If ctl.Name = ComboBox1.SelectedItem Then
  26.                 Cntrl = ctl
  27.                 Cntrl.Dock = DockStyle.None
  28.                 Cntrl.BringToFront()
  29.                 Me.BringToFront()
  30.                 Exit Sub
  31.             End If
  32.         Next
  33.  
  34.         ' Dim Cntrl As Control = Me.Controls(ComboBox1.Text)
  35.         'Cntrl.BringToFront()
  36.         'Me.BringToFront()
  37.         '  If Cntrl IsNot Nothing Then Cntrl.Dock = DockStyle.None
  38.  
  39.     End Sub
  40.  
  41.  
  42.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  43.         Cntrl.Top = Cntrl.Top + (e.Y - TheY) / 2
  44.         Cntrl.Left = Cntrl.Left + (e.X - TheX) / 2
  45.         DX = e.X
  46.         DY = e.Y
  47.         Timer1.Enabled = True
  48.     End Sub
  49.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  50.         Cntrl.Top = Cntrl.Top + (DY - TheY) / 2
  51.         Cntrl.Left = Cntrl.Left + (DX - TheX) / 2
  52.  
  53.     End Sub
  54.  
  55.     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  56.         Timer1.Enabled = False
  57.     End Sub
  58. End Class
  59.  
Thank you in advance for any tip or guidance.
ricardosms
Apr 18 '10 #1
2 3224
tlhintoq
3,525 Expert 2GB
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...

Expand|Select|Wrap|Line Numbers
  1. string ControlName = myComboBox.SelectedItem as text;
  2. Control FoundControl = myCustomControl.Controls.Find(ControlName);//Now I have a handle to the requested control
  3. if (FoundControl != null)
  4. {// Never just assume you *actually* found something
  5.    // Move it down 10 pixels, one at a time
  6.    For (int Counter = 0; Counter<10; Counter++)
  7.    {
  8.        FoundControl.Location = New Point(FoundControl.Location.X,  FoundControl.Location.Y++)
  9.    }
  10.  
  11.    // Move it right 10 pixels, one at a time
  12.    For (int Counter = 0; Counter<10; Counter++)
  13.    {
  14.        FoundControl.Location = New Point(FoundControl.Location.X++, FoundControl.Location.Y)
  15.    }
  16. }
Apr 18 '10 #2
Hi, tlhintoq:

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

Expand|Select|Wrap|Line Numbers
  1.         Dim FoundControl() As Control
  2.         Dim ControlName As String
  3.         ControlName = ComboBox1.SelectedItem
  4.         FoundControl = Me.Controls.Find(ComboBox1.SelectedItem, False)
  5.         'Now I have a handle to the requested control 
  6.         If (Not (FoundControl(0)) Is Nothing) Then
  7.             ' Never just assume you *actually* found something 
  8.             ' Move it down 10 pixels, one at a time 
  9.             For Counter = 0 To 10
  10.                 FoundControl(0).Top = FoundControl(0).Top + 1
  11.                 ' Move it right 10 pixels, one at a time
  12.             Next
  13.             For counter = 0 To 10
  14.                 FoundControl(0).Left = FoundControl(0).Left + 1
  15.             Next
  16.         End If
  17.  
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.
Apr 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript...
4
by: D Newsham | last post by:
Hi, This javascript creates a table that has a header and side column that do not move while scrolling through the table. I need to convert this to vb script. Can anybody help, or do you have...
5
by: Carl Gilbert | last post by:
Hi I have some ASP code that I want to run from CD within a VB.NET windows application with a web browser control. However, to get the ASP pages to run without a server is proving quite...
12
by: dixie | last post by:
Can someone familiar with Access 2003 please answer this question? I am asking because I don't have the use of A2003. When Access 2003 finds an Access 2000 database, does it come up with some...
3
by: Thubaiti | last post by:
Hi, I have this code in my ASP.NET and I want to convert it to C# (code behind) <asp:Repeater id="subCategoryRepeater" runat="server"> <ItemTemplate> <ul> <li> <asp:HyperLink...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
2
by: suji | last post by:
Hai, Are there any 3rd party controls who provide free libraries to convert my html form to pdf in c#? I need to have a button on my form, that should convert my page to pdf format. Can any one...
9
by: aaronluna | last post by:
Hi All, I was wondering if it is possible to easily convert an asp.net user control (.ascx) into an equivalent windows app. I plan on simply duplicating the user control in a c# windows app...
6
by: Deckarep | last post by:
I want to be able to pass in a function a string say: "TextBox" Then I need a way to convert that string representation into a Type object so i can search through some controls and check their...
12
by: Miro | last post by:
How can I convert this part of the line: Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName" which is within this line - Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCellName"),...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.