Solved

forms visual basic .net -500

Posted on 2004-04-08
6
228 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

Technology Partners: 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

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

730 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