Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execution of string variable containing form name

Status
Not open for further replies.

Bobalooey

IS-IT--Management
Feb 10, 2002
20
US
This should be the easiest thing to do. In a SQL Server table, I store menu information that is later shown in a tree-view control on a winform. The menu shows the names of forms that can be invoked. One of the columns contains the names of the various forms that can be executed (shown).

These would be sample rows from the menu table:

Menu Caption Form Name
------------------- -------------
Accounts Receivable frmAR
Accounts Payable frmAP
Order Entry frmOrderEntry

All of the forms have been defined in the solution.
When the user clicks on the tree-view node, I want the application to invoke the appropriate form, but I want to execute the form as a macro substitution. In Visual FoxPro (God how I miss it!) I could do the following:

command = "do form " + cFormName
&command

where cFormName is the name of the form that was brought in as data in the table row (ex: frmOrderEntry) and lo and behold the form would execute (if it could be found in the project path). Please spare me the verbage as to why this is a security issue and the history of compiled versus runtime languages. Can VB.NET do this...if so...how. Specific (and simple) code examples would be immensely appreciated. I'm at wit's end if that's not obvious from my demeanor.

I cannot find how to do this in vb.net winforms (even the latest Visual Studio .Net 2003 version). Any help would be greatly appreciated.
 
I'm pressed for time, so I can't give you a complete answer right now - but I sugest you look up "reflection" and "callbyname".

Chip H.
 
Chip,

If you have some more time ;-) do you think you could give that complete answer? The documentation for "reflection" and "callbyname" read like instructions to make plutonium.

Thanks,

Bobalooey
 


anyone have a solution to this problem?

thought I'd raise it rather than start a new thread.


Thanks
ITflash

 
You can use the CreateInstance of System.Reflection.Assembly namespace to load a form using the name of the form (but you must include the namespace):
Code:
Dim f As Form = CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("Namespace.FormName"), Form)

f.Show()
 

Thanks for the reply.

Is there any way I can do the following?

Code:
Dim f As TexBox = CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("Namespace.FormName.TextBoxName"), TextBox)
f.Focus()

It gives me an error. And what is the namespace? The application name?

Thanks
ITflash

 
Not exactly what you were asking for but..
you could start all the forms hidden and when one is
selected you could unhide the selected item.


if it is to be it's up to me
 


Thanks for the reply

Persume you are referring to the first post in this thread

Is there any way I can do this with text boxes?

I have the name of a textbox in a string variable and want to set focus to that text box.

Thanks
ITflash

 
If you are trying to set the focus to a text box on the form, then clearly you will need to create an instance of the form first (see the code in my previous post). You can then use the following code to find the text box and set the focus:
Code:
Dim ct As Control

For Each ct In f.Controls

    If TypeOf ct Is TextBox AndAlso ct.Name = "TextBoxName" Then

        ct.Focus()

        Exit For

    End If

Next
 

Thanks for the reply.

Know what you are saying.

However, I can do this anyway. My scenario is that I have a form and when the user clicks a specific button, I want to manipluate a specific textbox & its behaviour (tab, etc).

Therefore, I could do what you suggested and just loop through the controls as such:

Code:
Dim ct As Control
For Each ct In me.controls
    If TypeOf ct Is TextBox AndAlso ct.Name = "TextBoxName" Then
        ct.Focus()
        Exit For
    End If
Next

I would prefer to create the command in a variable for ease of use and to save looping through all the controls on a form.

i.e.

Code:
dim mytextboxname as String
mytextboxname="TexBox1"
Dim f As TexBox = CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("Namespace.FormName." & mytextboxname), TextBox)
f.Focus()

Is this possible?


Thanks
ITflash

 
No.

In order to gain access to a control collection on a form, you must first have an instance of the form. You then need a reference to the required control before you can call the Focus() method of the control, hence the loop to locate and reference the required text box.

The other way is to cast the form instance to the required form and directly reference the textbox:
Code:
Dim f As frmFormName = CType(CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("Namespace.FormName"), Form), frmFormName)

f.Show()

f.txtTextBox.Focus()
For example, to set the focus to TextBox1 in a form called frmAbout in a project called TestProject, you would use the following code:
Code:
Dim f As frmAbout = CType(CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("TestProject.frmAbout"), Form), frmAbout)

f.Show()

f.TextBox1.Focus()
I hope this helps...
 

Thanks for the reply.

The trouble is, I only have the textbox name inside a string variable.

Is there no way I can create an instance of the textbox like you do with the form by using my variable containing its name?


Thanks
ITflash

 
The only way I know of referring to a control by it's name is to loop the collection and check each name.

Sorry.
 
as a footnote i do the same thing and setup controls and properties for those controls via an ini file.
you will need a recursive loop if you have groupboxes on your form.

if you only want to loop once then build an additional hash table when your form is loaded. it sounds crazy but there you go
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top