Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create from from variable

Status
Not open for further replies.

compdrmt

Programmer
Sep 22, 2002
60
0
0
US
I have need to create of more correctly load a form dependent on which item is chosen in a listbox.

This can be done easily enough in vb6 using the forms.add method. In VB.net however this method does not exist.

Does any one know how to do this in .net or if it even possible






Debugging is the process of removing bugs. Programming is the process of putting them in.
 
So you have a list of forms is a list box?
Are these forms already in existance?

In order to load a form you have to create an instance of it.
'open the report form
Dim frm As New Report 'Report is the Exact name of your form
frm.ShowDialog 'if you are calling the form load from a "Sub Main" you must use ShowDialog, or your form will show for a split second and then the program will end. if you are calling it from another form you can just use .Show
'frm.Dispose() 'get rid of it later

you can use a select case in the List box SelectedIndexChanged event (which is the default event when you double click the List box)
Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
[b]
        Select Case Me.ListBox1.SelectedItem
            Case "Report"
                Dim frm As New Report
                frm.Show()
            Case "Customer"
                Dim frm As New Customer
                frm.Show()
            Case "Other"
                Dim frm As New Other
                frm.Show()
        End Select
[/b]
    End Sub

note if you move the frm.Show below the End Select because it is redundant you will get a Blue line
VB.NET Only knows a DIM where it is and in a Select statment it must be used there.
Maybe there is a better way, but this works

DougP, MCP, A+
 
yes the form will exist of course

What I am trying to accomplish is ...

In a database I have the name of the form which is used for a given record. for example I have a details form for type one input and another detail form for type 2 input etc.

In the list box I want to list

type one
type two
etc.

When the user chooses type one I want a form of type one to open. the easiest way (and the way I did this in VB6 ) is to use something like

'create name of form from name stored in dataset

private formvar as string

------
in listbox_click event

formvar = value_derived_from_the_list_box_choice

forms.add(formvar)
forms(formvar).show

This way I code for all forms easily in a few lines of code and add from later without changing or adding a bunch of code simply by creating the form and adding a record to the dataset. Rather than creating a select ... end select structure that requires maintenance.


There are several other places I used such structures in VB 6 and was just curious if anyone had found a way to do this in VB.net


Debugging is the process of removing bugs. Programming is the process of putting them in.
 
ooh, I just had an idea!

Code:
dim frmCastMe as form

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  frmCastMe = ctype(frmCastMe,Me.ListBox1.SelectedItem)
  frmCastMe.Show
End Sub

----------------------
 


I will try this.

It looks like it might just work



Debugging is the process of removing bugs. Programming is the process of putting them in.
 
to compdrmt:

have you made the code of thatrickguy work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top