Solved

forms visual basic .net -500

Posted on 2004-04-08
6
200 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:mayankeagle
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

15 Experts available now in Live!

Get 1:1 Help Now