locked
Calling a form from another form RRS feed

  • Question

  • I am very new to VB and iam learning programming while writing a code... I have many doubts... one of them is: 
    I have one main form(form1) from which i call another form(form2) which is used as a Question box. depending on the answer i have to do different tasks and return to the point where i called the form2. how do i do this or is it possible?
    please help.
    thanks,
    soumysudha
    Thursday, September 24, 2009 5:11 AM

Answers

  • G'day,

    Showing a second form (or any dialog) is not actually as simple as it may seem. Using the VB default copy of a form you can show it in 4 basic ways to give 3 slightly but usefully different behaviours. Give it a try.

    Open a windows Forms app and on the Form1 place 4 buttons with in order (Button1 to Button4) the following captions (text): "Form2.Show()","Form3.Show(Me)", "Form4.ShowDialog()", and "Form5.ShowDialog(Me)".

    On Form1 add 4 more buttons again in order (Button5 to Button8) with captions "Form2.Hide()","Form3.Hide()","Form4.Hide()" and "Form5.Hide()". Double click each of the 8 buttons and enter the text on the button as code for the click event.

    Add to the project Forms 2 to 5. Run the project. Each of the 4 show buttons will display the appropriate form, but whereas the ShowDialog methods prevent access to Form1 (ie display is modal) the Show methods do not.

    The two Show methods behave non-modal but in different ways. One allows Form1 to be clicked to the top and the other does not.

    Because the Show method allows access to the buttons on Form1 an error can arise if you attempt to show a form already displayed.

    I control that by simply adding "Button1.enabled=false" and "Button2.enabled=false", TO BUTTONS 1 AND 2, and adding "Button1.enabled=true" and "Button2.enabled=true"TO BUTTONS 5 AND 6, to the corresponding click events.

    Of these methods "Form2.Show(Me) " and either of the ShowDialog methods are the most useful in everyday coding. If you have other suggestions lets hear about them.

    Regards, Ron
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Monday, September 28, 2009 7:20 AM
  • Hi,

    you have 2 possibilities to achieve this:

    ' either this way
    
    form2.ShowDialog()
    
    ' or this way
    
    Dim myQuestionForm as New form2
    myQuestionForm.ShowDialog()
    The ShowDialog() method shows your form to the user and is waiting with the further execution of the code until the form2 is closed ( returning to the point where you called it)

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Thursday, September 24, 2009 6:14 AM
  • 1. If you want to display a form so that the user must supply an answer to some question or must fill in some information before the program can proceed, then display the form as a dialog. There is an example here:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx

    Notice that the information provided by the user is accessed as a property of the form that was displayed (txtResult.Text = testDialog.TextBox1.Text).

    2. When the other form is closed and the user has indicated OK or Cancel, you would usually carry out one or more actions by calling Functions or Subs, passing the information provided by the user on that form as an argument.

    3. For the structure you have described, you would not usually 'return' to where you displayed that other form.  The procedures in (2) would complete and the program would wait for whatever event it is that triggers the display of Form 2 - that is, whatever event caused step 1 above to happen.  This might be a user action such as clicking a button on form 1.  However if you want to re-display form 2 and ask for another response immediately the tasks in (2) are complete, then you would need to put the above steps into some sort of loop.

    • Proposed as answer by Shmuel Englard Thursday, September 24, 2009 4:04 PM
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Thursday, September 24, 2009 6:24 AM
  • I am very new to VB and iam learning programming while writing a code... I have many doubts... one of them is: 
    I have one main form(form1) from which i call another form(form2) which is used as a Question box. depending on the answer i have to do different tasks and return to the point where i called the form2. how do i do this or is it possible?
    please help.
    thanks,
    soumysudha

    if you are using bottum that verify if the answer is wrong or true you have to call form2 from the bottum like this

    when the person choose the answer than:

    if  radiobutton.checked then
    form2.showDialog()
    end if

    or you can use:

    form2.visible=true


    Don't judge me, just Upgrade me. Thanks!
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Thursday, September 24, 2009 7:46 PM

All replies

  • Hi,

    you have 2 possibilities to achieve this:

    ' either this way
    
    form2.ShowDialog()
    
    ' or this way
    
    Dim myQuestionForm as New form2
    myQuestionForm.ShowDialog()
    The ShowDialog() method shows your form to the user and is waiting with the further execution of the code until the form2 is closed ( returning to the point where you called it)

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Thursday, September 24, 2009 6:14 AM
  • 1. If you want to display a form so that the user must supply an answer to some question or must fill in some information before the program can proceed, then display the form as a dialog. There is an example here:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx

    Notice that the information provided by the user is accessed as a property of the form that was displayed (txtResult.Text = testDialog.TextBox1.Text).

    2. When the other form is closed and the user has indicated OK or Cancel, you would usually carry out one or more actions by calling Functions or Subs, passing the information provided by the user on that form as an argument.

    3. For the structure you have described, you would not usually 'return' to where you displayed that other form.  The procedures in (2) would complete and the program would wait for whatever event it is that triggers the display of Form 2 - that is, whatever event caused step 1 above to happen.  This might be a user action such as clicking a button on form 1.  However if you want to re-display form 2 and ask for another response immediately the tasks in (2) are complete, then you would need to put the above steps into some sort of loop.

    • Proposed as answer by Shmuel Englard Thursday, September 24, 2009 4:04 PM
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Thursday, September 24, 2009 6:24 AM
  • I am very new to VB and iam learning programming while writing a code... I have many doubts... one of them is: 
    I have one main form(form1) from which i call another form(form2) which is used as a Question box. depending on the answer i have to do different tasks and return to the point where i called the form2. how do i do this or is it possible?
    please help.
    thanks,
    soumysudha

    if you are using bottum that verify if the answer is wrong or true you have to call form2 from the bottum like this

    when the person choose the answer than:

    if  radiobutton.checked then
    form2.showDialog()
    end if

    or you can use:

    form2.visible=true


    Don't judge me, just Upgrade me. Thanks!
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Thursday, September 24, 2009 7:46 PM
  • G'day,

    Showing a second form (or any dialog) is not actually as simple as it may seem. Using the VB default copy of a form you can show it in 4 basic ways to give 3 slightly but usefully different behaviours. Give it a try.

    Open a windows Forms app and on the Form1 place 4 buttons with in order (Button1 to Button4) the following captions (text): "Form2.Show()","Form3.Show(Me)", "Form4.ShowDialog()", and "Form5.ShowDialog(Me)".

    On Form1 add 4 more buttons again in order (Button5 to Button8) with captions "Form2.Hide()","Form3.Hide()","Form4.Hide()" and "Form5.Hide()". Double click each of the 8 buttons and enter the text on the button as code for the click event.

    Add to the project Forms 2 to 5. Run the project. Each of the 4 show buttons will display the appropriate form, but whereas the ShowDialog methods prevent access to Form1 (ie display is modal) the Show methods do not.

    The two Show methods behave non-modal but in different ways. One allows Form1 to be clicked to the top and the other does not.

    Because the Show method allows access to the buttons on Form1 an error can arise if you attempt to show a form already displayed.

    I control that by simply adding "Button1.enabled=false" and "Button2.enabled=false", TO BUTTONS 1 AND 2, and adding "Button1.enabled=true" and "Button2.enabled=true"TO BUTTONS 5 AND 6, to the corresponding click events.

    Of these methods "Form2.Show(Me) " and either of the ShowDialog methods are the most useful in everyday coding. If you have other suggestions lets hear about them.

    Regards, Ron
    • Marked as answer by Jeff Shan Tuesday, September 29, 2009 2:21 AM
    Monday, September 28, 2009 7:20 AM
  • Thanks Regnwald,
    Your post was interesting and helpful

    Tuesday, September 29, 2009 5:47 AM