locked
Msg box (Yes key need double click to close) RRS feed

  • Question

  •  Why my Yes Button need double click to close and No button only require 1 click... I need both 1 click only.. What i am missing here? Please help

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            Try
                Dim result As Integer
                result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
                If (result = DialogResult.No) Then
                    e.Cancel = True
                Else
                    file.WriteLine(Now() & " " & "Succesfully closed   the Application")
                    file.Flush()
                    Application.Exit()
                    Environment.Exit(Environment.ExitCode)

                End If
            Catch ex As Exception
                file.WriteLine(ex.Message)
            End Try
            file.Flush()
        End Sub

    Wednesday, September 2, 2020 8:31 AM

Answers

  • understood my mistake... In the YES click.. i called

    Application.Exit()
     Environment.Exit(Environment.ExitCode) both,... 1 is required.. i removed Apllication.exit() and it worked..

    I am soo happy...Thank you for this forum... as a new VB.net developer you helped me too much to make my first project success...

    If there is only one form you simply call the Close method of the form rather than one of those currently being used. Only use those mentioned in windows forms for a secondary form

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by meeraluv Wednesday, September 2, 2020 12:32 PM
    Wednesday, September 2, 2020 12:14 PM

All replies

  • you should try to do it by creating a new custom form

    SAMPLE;

    create a new project with 2 forms, Form1 will be the main form that will call the custom msgbox and Form2 will be the custom msgbox:

    Form1:

    enter image description here

    Form2:

    enter image description here

    Code for Form1:

    Public Class Form1
        Private Sub btnOpenCustomMsgbox_Click(sender As Object, e As EventArgs) Handles btnOpenCustomMsgbox.Click
            Dim customMsgbox = New Form2("this is my custom msg, if you press yes i will do something if you press no i will do nothing")
            If customMsgbox.ShowDialog() = DialogResult.Yes Then
                ' do something
                MsgBox("I am doing some operation...")
            Else
                ' do nothing (its DialogResult.no)
                MsgBox("I am doing nothing...")
            End If
        End Sub
    End Class

    Code for Form2:

    Public Class Form2
    
        ' field that will contain the messege
        Private PromtMsg As String
        Sub New(ByVal promtmsg As String)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            ' set global field with the argument that was passed to the constructor
            Me.PromtMsg = promtmsg
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' set the msg label
            Me.lblPromtMsg.Text = Me.PromtMsg
        End Sub
    
        Private Sub btnCustomYes_DoubleClick(sender As Object, e As EventArgs) Handles btnCustomYes.DoubleClick
            ' user choosed yes - return DialogResult.Yes
            Me.DialogResult = DialogResult.Yes
            Me.Close()
        End Sub
    
        Private Sub btnCustomNo_DoubleClick(sender As Object, e As EventArgs) Handles btnCustomNo.DoubleClick
            ' user choosed no - DialogResult.no
            Me.DialogResult = DialogResult.No
            Me.Close()
        End Sub
    
    End Class

    Quote from https://stackoverflow.com/questions/47190131/simple-dialog-like-msgbox-with-custom-buttons-vb


    Sorunuzun yanıtı bu ise "Yanıt Olarak Öner" olarak işaretleyin, eğer faydalı bir yorum ise "Oy Ver"erek forumun işleyişine katkıda bulunabilirsiniz

    Wednesday, September 2, 2020 8:48 AM
  • i have my message boxes correctly....This is just asking whether i want to close the windows form or not. Action also working perfectly... Only Prob is YES button need double click to move fwd and NO button is working as expected (ie: 1 click)
    • Marked as answer by meeraluv Wednesday, September 2, 2020 9:36 AM
    • Unmarked as answer by meeraluv Wednesday, September 2, 2020 9:36 AM
    Wednesday, September 2, 2020 8:56 AM
  • understood my mistake... In the YES click.. i called

    Application.Exit()
     Environment.Exit(Environment.ExitCode) both,... 1 is required.. i removed Apllication.exit() and it worked..

    I am soo happy...Thank you for this forum... as a new VB.net developer you helped me too much to make my first project success...

    Wednesday, September 2, 2020 9:38 AM
  • Actually what you perceive as having to press "Yes" twice is because MessageBox.Show is called twice.  Application.Exit causes the FormClosing event handler to be re-entered.  You can see this by putting a breakpoint in the event handler code and examining the call stack in the Visual Studio debugger.

    The documentation for the Application.Exit  method mentions that it raises the FormClosing event in the Remarks section.


    • Edited by RLWA32 Wednesday, September 2, 2020 12:01 PM typo
    Wednesday, September 2, 2020 11:37 AM
  • understood my mistake... In the YES click.. i called

    Application.Exit()
     Environment.Exit(Environment.ExitCode) both,... 1 is required.. i removed Apllication.exit() and it worked..

    I am soo happy...Thank you for this forum... as a new VB.net developer you helped me too much to make my first project success...

    If there is only one form you simply call the Close method of the form rather than one of those currently being used. Only use those mentioned in windows forms for a secondary form

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by meeraluv Wednesday, September 2, 2020 12:32 PM
    Wednesday, September 2, 2020 12:14 PM