how to close a form in VB.Net (VS 2005)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Q2hyaXM=?=

    how to close a form in VB.Net (VS 2005)

    I am showing a form by ShowModal(). I put a button on that form. When a user
    clicks the button a MsgBox will show with question "do you want to close?"
    yes/no.
    How to handle the situation:
    - user clicks "yes" - the form closes
    - user clicks "no" - nothing happens

    I have problem with that. It seems that assigning property of
    button.DialogRe sult wokrs ok only for the first time (in designer generated
    code and any longer)
  • Gillard

    #2
    Re: how to close a form in VB.Net (VS 2005)

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    If MessageBox.Show ("do you want to close", Me.Text,
    MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
    Windows.Forms.D ialogResult.OK Then
    'TODO : place your close code here
    End If
    End Sub

    "Chris" <Chris@discussi ons.microsoft.c omwrote in message
    news:86DC89F4-D785-43E7-9E9F-241B25578717@mi crosoft.com...
    I am showing a form by ShowModal(). I put a button on that form. When a
    user
    clicks the button a MsgBox will show with question "do you want to close?"
    yes/no.
    How to handle the situation:
    - user clicks "yes" - the form closes
    - user clicks "no" - nothing happens
    >
    I have problem with that. It seems that assigning property of
    button.DialogRe sult wokrs ok only for the first time (in designer
    generated
    code and any longer)

    Comment

    • =?Utf-8?B?Q2hyaXM=?=

      #3
      Re: how to close a form in VB.Net (VS 2005)

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      If MessageBox.Show ("do you want to close", Me.Text,
      MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
      Windows.Forms.D ialogResult.OK Then
      'TODO : place your close code here
      End If
      End Sub



      'TODO : place your close code here - that uis EXACTLY my question. What
      shoulb be here to close the form???????
      Me.Close() does NOT work - nothing happens

      The form closes only when the DialogResult property of the clicked button is
      set to DialogResult.OK

      The problem is that even if I have written the code below:

      'designer generated code
      button1.DialogR esult=DialogRes ult.Ok;
      'end of designer generated code

      and then:

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      If MessageBox.Show ("do you want to close", Me.Text,
      MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
      Windows.Forms.D ialogResult.OK Then
      button1.DialogR esult=DialogRes ult.Ok;
      Else
      button1.DialogR esult=DialogRes ult.No;
      End If
      End Sub

      It always closes the form (even the user clicks "No")

      That IS the problem

      Comment

      • Teemu

        #4
        Re: how to close a form in VB.Net (VS 2005)


        "Chris" <Chris@discussi ons.microsoft.c omkirjoitti viestissä
        news:F40B2FBA-7018-4CE2-A65D-D5C4AED3AAE7@mi crosoft.com...
        >
        The form closes only when the DialogResult property of the clicked button
        is
        set to DialogResult.OK
        >
        It always closes the form (even the user clicks "No")
        >
        That IS the problem
        I'm not sure if I understood your problem correctly.

        I tried this kind of scenario:

        In Form1 I have a button and this code:
        MsgBox(Form2.Sh owDialog.ToStri ng)

        And in Form2 I have another button and this code:
        If MsgBox("Close this form?", MsgBoxStyle.Yes No Or MsgBoxStyle.Que stion) =
        MsgBoxResult.Ye s Then
        Me.DialogResult = Windows.Forms.D ialogResult.Yes
        End If

        My button in Form2 doesn't have any DialogResult set in design mode. Now it
        works so that if I click yes Form2 returns Yes. If I click no Form2 stays
        open and I can click the button again.

        Is this what you wanted to achieve? If it is, just remove the DialogResult
        property in design mode and assign it in code.

        Hopefully you find this helpful.

        -Teemu

        Comment

        • Gillard

          #5
          Re: how to close a form in VB.Net (VS 2005)

          Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          System.EventArg s) Handles Button1.Click
          If MessageBox.Show ("do you want to close", Me.Text,
          MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
          Windows.Forms.D ialogResult.OK Then
          Me.Dispose(True ) 'this close the form
          Else
          ' the user click no
          'so i suppose he do not want to close the prog
          End If
          End Sub


          "Teemu" <tsirkia@hotmai l.comwrote in message
          news:iJmlk.4427 9$_03.36565@rea der1.news.sauna lahti.fi...
          >
          "Chris" <Chris@discussi ons.microsoft.c omkirjoitti viestissä
          news:F40B2FBA-7018-4CE2-A65D-D5C4AED3AAE7@mi crosoft.com...
          >>
          >The form closes only when the DialogResult property of the clicked button
          >is
          >set to DialogResult.OK
          >>
          >It always closes the form (even the user clicks "No")
          >>
          >That IS the problem
          >
          I'm not sure if I understood your problem correctly.
          >
          I tried this kind of scenario:
          >
          In Form1 I have a button and this code:
          MsgBox(Form2.Sh owDialog.ToStri ng)
          >
          And in Form2 I have another button and this code:
          If MsgBox("Close this form?", MsgBoxStyle.Yes No Or MsgBoxStyle.Que stion) =
          MsgBoxResult.Ye s Then
          Me.DialogResult = Windows.Forms.D ialogResult.Yes
          End If
          >
          My button in Form2 doesn't have any DialogResult set in design mode. Now
          it works so that if I click yes Form2 returns Yes. If I click no Form2
          stays open and I can click the button again.
          >
          Is this what you wanted to achieve? If it is, just remove the DialogResult
          property in design mode and assign it in code.
          >
          Hopefully you find this helpful.
          >
          -Teemu

          Comment

          • Mr. Arnold

            #6
            Re: how to close a form in VB.Net (VS 2005)


            "Chris" <Chris@discussi ons.microsoft.c omwrote in message
            news:86DC89F4-D785-43E7-9E9F-241B25578717@mi crosoft.com...
            >I am showing a form by ShowModal(). I put a button on that form. When a
            >user
            clicks the button a MsgBox will show with question "do you want to close?"
            yes/no.
            How to handle the situation:
            - user clicks "yes" - the form closes
            - user clicks "no" - nothing happens
            >
            I have problem with that. It seems that assigning property of
            button.DialogRe sult wokrs ok only for the first time (in designer
            generated
            code and any longer)
            The form itself has a FromClosing event an event just like a Button-click
            event for a Button, but its for the form, which is the (lighting bolt) on
            the form's Property page that shows all the events for the form. You find
            the FormClosing event and you double click it to get the event established
            in the code.

            When the user clicks the X icon to close the form, it's going to firer the
            FromClosing event where you ask the question. If the response is yes, the
            closing of the form is done. If the response is no, the form closing is
            cancelled and the form will not close.


            Private Sub FormClosing(ByV al sender As System.Object, ByVal e As
            System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing

            If MessageBox.Show ("Are you sure to exit?", "Exit",
            MessageBoxButto ns.YesNo, MessageBoxIcon. Question) = DialogResult.Ye s Then
            e.Cancel = False
            Else
            e.Cancel = True
            End If

            End Sub

            Comment

            Working...