-
Jun 8th, 2021, 12:53 AM
#1
Thread Starter
New Member
showing a form based on value of a string containing the form's name
Hello
I have a string like str1 that contain the name of a form like Form33. In fact the str1 determines which form should be loaded and shown. The form may change with its number. How can I show the form through converting the string to a form?
Thank you very much for your help.
str1="Form33"
convert str1 to object obj as Form
obj.show
-
Jun 8th, 2021, 03:55 AM
#2
Re: showing a form based on value of a string containing the form's name
That you are using meaningless names like Form33 is bad to begin with. EVERYTHING, including forms, should have descriptive names.
Secondly, why do you have the name of a form in a String to begin with? There could be a legitimate reason but, in my experience, there is almost always a better way to do what someone wants to do when they think they need to do it like that. You should explain what you're actually trying to achieve, rather than just how you're trying to achieve it, and we can possibly provide a better option.
-
Jun 8th, 2021, 04:32 AM
#3
Thread Starter
New Member
Re: showing a form based on value of a string containing the form's name
Thank you for your response.
In my code, there are many Forms labeled for example as Form33, Form34, Form35, ... Form40,....
An integer variable is set by for example i=33 and then the following code works
dim obj as New Form
if i=33 then
obj=my.Forms.Form33
elseif i=34 then
obj=my.Forms.Form34
elseif i=35 then
obj=my.Forms.Form35
elseif i=36 then
obj=my.Forms.Form36
elseif i=37 then
obj=my.Forms.Form37
elseif i=38 then
obj=my.Forms.Form38
elseif i=39 then
obj=my.Forms.Form39
elseif i=40 then
obj=my.Forms.Form40
else
msgbox("Not invalid form number!")
end if
obj.show
In fact, I get a number in i and I want to show the corresponding Form named by Formi.
I do not want to use if statements because there are many forms, or, in other words, i is a large number or there are many many Forms numbered in order from i=1, to i=100.
Now, I want when i is specified by previous codes, the appropriate form can be shown without using these large IF statements, but only by a few lines of codes like that
obj=Ctype("my.Forms.Form"+str(i)),Form)
obj.show
Of course, this code does not work.
Thank you for your any help.
Dean.
-
Jun 8th, 2021, 04:57 AM
#4
Re: showing a form based on value of a string containing the form's name
Welcome to the forum DeanTop.
1. You could probably store all those form names in an array (or better yet, a list) and use the "i" variable as an index.
2. Use the ampersand character ("&") to build strings instead of the plus character. Or use string interpolation.
3. Use a "Select Case" block instead of all those If/Then/ElseIf lines.
Also kindly enclose code in code tags using the "#" button in the toolbar above a message to be posted on this forum.
-
Jun 8th, 2021, 05:29 AM
#5
Thread Starter
New Member
Re: showing a form based on value of a string containing the form's name
Thank you for your response.
1.Using arrays is not doable, because it needs to allocate one line of code for each form (like as if statements) when there are for example 200 Forms: myForm(33)=My.Forms.Form33,
2. Use of & is ok but strings can not be converted to Forms.
3. Select Case is also like IF statements, it needs two lines for each Form.
I need an implementable solution.
Thank you for your kind reply.
Sincerely,
Dean.
-
Jun 8th, 2021, 05:48 AM
#6
Re: showing a form based on value of a string containing the form's name
Originally Posted by deantop
Thank you for your response.
1.Using arrays is not doable, because it needs to allocate one line of code for each form (like as if statements) when there are for example 200 Forms: myForm(33)=My.Forms.Form33,
2. Use of & is ok but strings can not be converted to Forms.
3. Select Case is also like IF statements, it needs two lines for each Form.
I need an implementable solution.
Thank you for your kind reply.
Sincerely,
Dean.
Is there a reason you need so many forms? Without really knowing exactly what you are trying to do it is really hard to suggest a decent solution.
If you really must use strings like this then you would probably want to look at using Reflection to create an instance of the relevant form.
-
Jun 8th, 2021, 05:49 AM
#7
Re: showing a form based on value of a string containing the form's name
Originally Posted by PlausiblyDamp
Is there a reason you need so many forms? Without really knowing exactly what you are trying to do it is really hard to suggest a decent solution.
Let's see whether this request for relevant information gets ignored too.
-
Jun 8th, 2021, 05:55 AM
#8
Re: showing a form based on value of a string containing the form's name
Originally Posted by jmcilhinney
Let's see whether this request for relevant information gets ignored too.
It is a nice sunny day here, I am feeling optimistic
-
Jun 8th, 2021, 06:35 AM
#9
Re: showing a form based on value of a string containing the form's name
Originally Posted by deantop
Thank you for your response.
1.Using arrays is not doable, because it needs to allocate one line of code for each form (like as if statements) when there are for example 200 Forms: myForm(33)=My.Forms.Form33,
2. Use of & is ok but strings can not be converted to Forms.
3. Select Case is also like IF statements, it needs two lines for each Form.
I need an implementable solution.
Thank you for your kind reply.
Sincerely,
Dean.
1. I have no idea what you mean. - EDIT: you could use array literals: "Dim MyForms() As Form = {Form1, Form2, Form3}. That should work.
2. "obj=Ctype("my.Forms.Form"+str(i)),Form)" - you are building a string here using the plus operator. The fact you're using that string in a conversion is irrelevant.
3. Yes, but using Select Case should result in cleaner code. Observe:
Code:
Select Case i
Case 33
obj = my.Forms.Form33
Case 34
obj = my.Forms.Form34
Case 35
obj = my.Forms.Form35
Case 36
obj = my.Forms.Form36
Case 37
obj = my.Forms.Form37
Case 38
obj = my.Forms.Form38
Case 39
obj = my.Forms.Form39
Case 40
obj = my.Forms.Form40
Case Else
MsgBox("Not invalid form number!")
End Select
And if you're as new to vb.net as I suspect, you would do well to think about what others are telling you, instead of immediately rejecting it out of hand.
Last edited by Peter Swinkels; Jun 8th, 2021 at 06:45 AM.
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
|