Solved

forms visual basic .net -500

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now