|
-
Mar 21st, 2008, 01:41 PM
#1
Thread Starter
Hyperactive Member
Convert String to Control
Is it possible to convert a string into a control?
Dim Ctrl as string = "DataGridView"
Dim Index as integer = "1"
Dim g as DataGridView = DirectCast(Ctrl & Index, DataGridView)
and then access the properties of the Grid...
g.BackColor = Color.Red
g.Left = 3
etc.
I'm pretty sure it's possible but nothing I've tried works.
-
Mar 21st, 2008, 02:00 PM
#2
Re: Convert String to Control
Not convert it, but you can specify the get the control from the containers collection by its name:
vb Code:
Dim g As DataGridView = DirectCast(Me.Controls("DataGridView1"), DataGridView)
But if you really think you need to use this method, you will need to redesign your code.
-
Mar 21st, 2008, 02:00 PM
#3
Re: Convert String to Control
you can do it by accessing the control by name from the forms controls collection like this:
Code:
Dim Ctrl As String = "DataGridView"
Dim Index As Integer = 1
Dim g As DataGridView = DirectCast(Me.Controls(Ctrl & Index.ToString), DataGridView)
note that the forms controls collection only holds controls directly on the form, so if the DGV is in a panel or another container control like that, you need to use the controls collection of that container, and not the form.
-
Mar 21st, 2008, 03:07 PM
#4
Thread Starter
Hyperactive Member
Re: Convert String to Control
I suppose that if the control was inside a container on the form (like in a TabControl) that would affect the code wouldn't it?
-
Mar 21st, 2008, 03:09 PM
#5
Re: Convert String to Control
yes instead of the keyword Me (which indicates the form) you would use the variable name for the TabPage (not the TabControl) that the given datagridview is on.
Code:
Dim g As DataGridView = DirectCast(TabPage1.Controls(Ctrl & Index.ToString), DataGridView)
-
May 19th, 2011, 10:51 PM
#6
New Member
Re: Convert String to Control
thanks you so much, this info. is good for me.
-
Jun 3rd, 2012, 06:06 PM
#7
New Member
Re: Convert String to Control
I found a shortcut! and its superly duperly dynamic!
Dim s As String = "textbox"
Dim c As Object
If s = "textbox" Then
c = New TextBox
c.Text = "This is a textbox"
c.Location = New System.Drawing.Point(200, 50)
c.Size() = New System.Drawing.Size(70, 20)
Me.Controls.Add(c)
End If
-
Jun 3rd, 2012, 06:51 PM
#8
Re: Convert String to Control
 Originally Posted by iexpress
I found a shortcut! and its superly duperly dynamic!
Dim s As String = "textbox"
Dim c As Object
If s = "textbox" Then
c = New TextBox
c.Text = "This is a textbox"
c.Location = New System.Drawing.Point(200, 50)
c.Size() = New System.Drawing.Size(70, 20)
Me.Controls.Add(c)
End If
you misunderstood this thread
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 3rd, 2012, 09:09 PM
#9
Re: Convert String to Control
If you want to create a class via a string that represents its type name you can use this:-
vbnet Code:
'
Public Function CreateClass(ByVal className As String) As Object
Dim asms() As Assembly = AppDomain.CurrentDomain.GetAssemblies
For Each Asm As Assembly In asms
Dim types = Asm.GetTypes
For Each T As Type In types
If T.Name.Equals(className, StringComparison.OrdinalIgnoreCase) Then
Return Activator.CreateInstance(T)
End If
Next
Next
Throw New Exception("Type not found")
End Function
Here is a version using Linq:-
vbnet Code:
'
Public Function CreateClass(ByVal className As String) As Object
Dim asms() As Assembly = AppDomain.CurrentDomain.GetAssemblies
Dim typeSought() As Type = (From Asm In asms _
Let types As Type() = Asm.GetTypes _
Let found As Type = Array.Find(Of Type)(types, Function(t) t.Name.Equals(className, StringComparison.OrdinalIgnoreCase)) _
Where found IsNot Nothing Select found).ToArray
If typeSought.Length > 0 Then
Return Activator.CreateInstance(typeSought(0))
End If
Throw New Exception("Type not found")
End Function
Use it like:-
vbnet Code:
'
Dim tb As TextBox = CreateClass("textbox")
Dim dg As DataGridView = CreateClass("datagridview")
Me.Controls.Add(tb)
Me.Controls.Add(dg)
-
Jul 18th, 2012, 10:26 AM
#10
Junior Member
Re: Convert String to Control
I've read this with interest but still can't get what I want to do to work.
My requirement is...
Get a string from a database say dbstr which = "cmd2"
Create a new instance of a form and then Performclick on the control button named btn2 on that form.
I'm struggling with the Directcast statement to do this. I've tried
Code:
Dim dbstr As String
Dim btn As New Button
Dim ssga As New sggAcs //(sggAcs is a project form)
dbstr = "cmd2"
btn = DirectCast(ssga.Controls(dbstr), Button)
If I then try
Code:
ssga.btn.PerformClick()
vb tells me that btn is not a member of sggAcs
I use this method a fair bit to change the screen colours etc. depending on what the user is doing so any pointers to my errors would be much appreciated.
-
Jul 18th, 2012, 03:30 PM
#11
Re: Convert String to Control
is the button in any sort of container control, like a panel or groupbox?
-
Jul 18th, 2012, 04:47 PM
#12
Junior Member
Re: Convert String to Control
Yes it's in a panel.
does that mean I should have put
ssga.panelname.btn.PerformClick()
I'll go and try it.
Thanks.
-
Jul 18th, 2012, 05:02 PM
#13
Junior Member
Re: Convert String to Control
Panel is called pnl2 so I tried this
Code:
Dim dbstr As String
Dim btn As New Button
Dim ssga As New sggAcs
dbstr = "cmd2"
btn = DirectCast(ssga.Controls(dbstr), Button)
MsgBox(ssga.pnl2.btn.text)
blue squiggly line under ssga.pnl2.btn - Error List gives "btn is not a member of System.Windows.Forms.Panel"
Bother!
i've now tried this - code is in a button_click event on main form.
Code:
Dim dbstr As String
Dim ssga As New sggAcs
dbstr = "cmd2"
Me.Text = ssga.pnl2.Controls(dbstr).Text
when I click the button in main form the title bar changes to the text of the button in the form sggAcs, so I can access the text property of a control on another form without using directcast BUT when I try this which is what I really want to do...
Code:
ssga.pnl2.Controls(dbstr).performclick()
I get an error message - performclick is not a member of 'System.Windows.Forms.Control'
I'm obviously missing something important here but can't figure out what.
Any pointers much appreciated.
Last edited by timjohn; Jul 18th, 2012 at 05:39 PM.
-
Jul 18th, 2012, 06:20 PM
#14
Re: Convert String to Control
vb Code:
directcast(ssga.pnl2.Controls(dbstr), Button).performclick()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2012, 07:12 PM
#15
Junior Member
Re: Convert String to Control
Hi,
Tried that and as soon as I clicked the button on the main form I got this in the immediate window..
A first chance exception of type 'System.NullReferenceException' occurred in Gtacs.exe
Odd as your line of code didn't produce any errors and I don't understand the .exe bit as I'm running in vb2010 debug mode. However Gtacs is the project name, just not compiled yet.
-
Jul 18th, 2012, 07:46 PM
#16
Re: Convert String to Control
at the time you got that error, had you created the dbstr button?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2012, 07:54 PM
#17
Re: Convert String to Control
i need to see all of your relevant code.
i'm not sure if you're adding a button in code or it already exists, + you're trying to refer to it by a string
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2012, 08:20 PM
#18
Junior Member
Re: Convert String to Control
Hi Paul,
Was in middle of writing to you when you asked for code, so scrapped last post and send this in it's place. Hope it's what you need.
Here is the code in the main form.
Code:
Bit at top of form is this.
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Then the relevant button is this.
Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
Dim dbstr As String
Dim ssga As New sggAcs
dbstr = "cmd3"
DirectCast(ssga.Controls(dbstr), Button).PerformClick()
End Sub
This is the code for the other form where the buton to be clicked is..
Code:
Friend Class sggAcs
Inherits System.Windows.Forms.Form
Private Sub cmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click, cmd2.Click, cmd3.Click
If sender.Tag <> gtType Then
gtType = sender.tag
gtCode = Trim(gtID) & sender.tag
If sender.tag = "g" Then
gtHead = "General"
ElseIf sender.tag = "c" Then
gtHead = gtCamp
Else
gtHead = "Fundraising"
End If
MainGtacs.Text = "Gtracs - " & gtHead & " Accounts - " & gtIdent
Me.Dispose()
Else
Me.Dispose()
End If
End Sub
End Class.
There is more to go in but it's not there yet.
BTW, I'm trying to convert(re-writing) a system from VB6. Then I just used the index of an array, not in vb2010 though!
-
Jul 18th, 2012, 08:29 PM
#19
Re: Convert String to Control
ok. it seems you're creating + referring to a new instance of your form, instead of the open instance you should be referring to. here's an example using a form (Form2) + a button (Button1):
vb Code:
DirectCast(My.Application.OpenForms("Form2").Controls("Button1"), Button).PerformClick()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2012, 08:32 PM
#20
Re: Convert String to Control
BTW... you can use an array of buttons in vb.net, but in a different way to the vb6 control array:
vb Code:
Public Class Form1
Dim btns() As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btns = New Button() {Button1, Button2, Button3}
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2012, 08:36 PM
#21
Re: Convert String to Control
are you intending to open a new form + click cmd3?
why not just use [formName].[buttonName].PerformClick() ?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 19th, 2012, 03:33 AM
#22
Junior Member
Re: Convert String to Control
Hi Paul,
Working backwards
(21) I only have the button name in a string variable so [formName].[buttonName].PerformClick() doesn't work when I replace [buttonName] with a variable, get the error 'dbstr' is not a member of 'Gtacs.sggAcs'.
(20) Thanks for that I'll have a play and see.
(19) I get an error 'OpenForms' is not a member of 'Gtacs.My.MyApplication'
Sorry to be causing all this, I really thought I was asking something simple (well it was in VB6)- click a control button on a second form & carry out the code on the button click, with the user never seeing the second form. All it took was 3 lines
Code:
Load sggAcs
sggAcs.Command3(gtAcNo).Value = True
Unload sggAcs
where gtAcNo was an integer.
-
Jul 19th, 2012, 09:24 AM
#23
Junior Member
Re: Convert String to Control
Think I've got it - not sure if it's the correct way/good coding but it seems to work.
Add
Code:
Public Shared btns() As Button
to the Target Form below Inherits statement.
Then Add
Code:
btns = New Button() {cmd1, cmd2, cmd3}
to the Target Forms Load event.
Then use
Code:
Dim dbstr As Integer
sggAcs.Show()
dbstr = 2 // dbstr will be filled from the database(-1) & it becomes index of btns.
DirectCast(sggAcs.btns(dbstr), Button).PerformClick()
in the calling form.
If you know a better way I'd appreciate it.
Thanks are due for all your help in pointing me in the right direction.
-
Jul 19th, 2012, 10:57 AM
#24
Re: Convert String to Control
no need to cast:
vb Code:
sggAcs.btns(dbstr).PerformClick()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 19th, 2012, 11:05 AM
#25
Junior Member
Re: Convert String to Control
Got it, thanks Paul, won't have such a late night tonight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|