Hi guys,

I have a form that could be called from 3 tool strip menu items at different times.And from that form you can select an item and proceed to another form based on the selection.Since they share the items this form is holding.
Now i want this form to determine which tool strip menu item called it,and then to display the relevant forms.Is that possible,rather than creating identical form for the same purpose?

Hmmm... I think I understand what you are trying to do

Why not have the menu items show only the appropriate options on the called form? e.g.

sub MenuItem1_OnClick()
    dim MyForm as new form1

    with MyForm
        .Item1.visible = true
        .Item2.visible = false
        .item3.visible = false 
        .item4.visible = true
        'and so on
        .showdialog
    end with
end sub

 sub MenuItem2_OnClick()
    dim MyForm as new form1

    with MyForm
        .Item1.visible = false
        .Item2.visible = true
        .item3.visible = false 
        .item4.visible = true
        'and so on
        .showdialog
    end with
end sub

'etc

Another possibility would be to have a public property declared in the called form and have it set to a different value from each menu handler. Then the called form could use the value of that variable to do the appropriate initialization. That puts more of the logic in the called form rather than in the calling form.

A third possibility would be to override the New method in the called form and pass it a different value depending on which menu handler instantiated the form. Then the called form would use that value rather than exposing a public property.

Option one would be better if you want to create the form only once.

@ G Waddell,the items available in the called form may be common which are in a list box by the way.Plus what my idea is like of Reverend Jim mentioned first.

Edited 2 Years Ago by Nebil

@ Reverend Jim,Can you exemplify your first idea? Since it was what i was looking for.

Create a project with two forms named Form1 and Form2. Each form has three buttons named Button1, Button2 and Button3. Add the following code.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Form2.Mode = "mode 1"
        Form2.ShowDialog()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Form2.Mode = "mode 2"
        Form2.ShowDialog()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Form2.Mode = "mode 3"
        Form2.ShowDialog()
    End Sub

End Class

Public Class Form2

    Public Mode As String = ""

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Select Case Mode
            Case "mode 1"
                Button1.Enabled = True
                Button2.Enabled = False
                Button3.Enabled = False
            Case "mode 2"
                Button1.Enabled = False
                Button2.Enabled = True
                Button3.Enabled = False
            Case "mode 3"
                Button1.Enabled = False
                Button2.Enabled = False
                Button3.Enabled = True
            Case Else
                Button1.Enabled = False
                Button2.Enabled = False
                Button3.Enabled = False
        End Select

    End Sub

End Class

Edited 2 Years Ago by Reverend Jim

@ Reverend Jim,'Mode' property doesn't exist for a form.And if it is modal if you're referring to,the property is read-only.

Yes it does. Line 22

Public Mode As String = ""

defines it as a public property. I tested the code before I posted it. VB 2010. The Form2 code could also be written as

Public Class Form2

    Public Mode As String = ""

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Button1.Enabled = Mode = "mode 1"
        Button2.Enabled = Mode = "mode 2"
        Button3.Enabled = Mode = "mode 3"

    End Sub

End Class

Edited 2 Years Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.