Solved

forms visual basic .net -500

Posted on 2004-04-08
6
234 Views
Last Modified: 2013-12-04
i am writing a program in visual studio .net
i have five forms made (form1,form2,form3,form4,form5)

i want the code to open a given form by name.
for example to give the "form2" in a textbox
and then create an instanse of the form "form2" as x and then open the x  on the program .
thanx

0
Comment
Question by:voulgaris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:Leroyski
ID: 10781936
what language are you using, Visual Basic?

If so, all you need to do is:

formname.show

So, if you want form1 to be opened:

form1.show
0
 
LVL 19

Accepted Solution

by:
Desp earned 250 total points
ID: 10782013
Unfortunately there is no direct way to do this in VB.NET you have to perform checks on the contents of the textbox
like this:

'**********************
Select Case TextBox1.Text
            Case "form1"
                Dim frm1 As New Form1()
                frm1.Show()

            Case "form2"
                Dim frm2 As New Form2()
                frm2.Show()

                .........

End Select

'**********************
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
ID: 10782263
This is result of my test. Create new VB.NET Windows application. Add Button1 and TextBox1 to Form1. Add new Form2 and Form3 to the project. This is Form1 code:

Imports System.Reflection
Imports System.Text
...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As [Assembly]
        Dim f As Form
        Dim s As StringBuilder

        Try
            a = [Assembly].GetExecutingAssembly()

            s = New StringBuilder
            s.Append(a.GetName().Name)
            s.Append(".")
            s.Append(TextBox1.Text)

            f = CType(a.CreateInstance(s.ToString()), Form)

            If f Is Nothing Then
                MessageBox.Show(Me, "Type not found")
            Else
                f.ShowDialog(Me)
            End If

        Catch ex As Exception
            MessageBox.Show(Me, ex.Message)
        End Try
    End Sub

Type Form2 or Form3 in the textbox and press the button.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10782969
I would vote for reflection. Takes care of all forms, so tomorrow if you add a new form, then you don't need to code another case in the switch construct. Handling exceptions could also be easy.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question