none
programming x button to close RRS feed

  • Question

  • hello i want to know how i can programming x button with a message box to confirm close

    Wednesday, October 21, 2015 1:00 PM

Answers

  • Hi Dereklouis,

    Are you trying to get a messagebox to confirm to exit from WinForm? If so please write the below code into your Form's FormClosing event. Hope this helps you.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Exit or no?",
                    "Confirm Exit",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Information) == DialogResult.No)
        {
            e.Cancel = true;
        }
    }

    Thanks,

    Sabah Shariq


    Please remember to mark the replies as answers if they help. If this post is helpful please vote

    Thursday, October 22, 2015 11:07 AM
  • This is the Small Basic forum, so I think he meant to program it in Small basic.

    Unfortunately you cannot have a confirm box unless you have an extension.

    Here is how you could do it without one.

    GraphicsWindow.Show()
    
    
    While 1 = 1
      Program.Delay(10)
      If GraphicsWindow.LastKey = "Escape" Or GraphicsWindow.LastKey = "X" Then
        Program.End()
      EndIf
      EndWhile

    And if you use litdev's extension:

    GraphicsWindow.Show()
    GraphicsWindow.KeyDown = KeyDownSub
    
    
    
    Sub KeyDownSub
      If GraphicsWindow.LastKey = "Escape" Or GraphicsWindow.LastKey = "X" Then
        Confirm = LDDialogs.Confirm("Do you wish to exit?","SmallBasic")
        If Confirm = "Yes" Then
          Program.End()
        EndIf
      EndIf
    EndSub

    Hope this helps!!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Thursday, October 22, 2015 2:59 PM
    Answerer
  • Oops. My bad.

    Here is how you would do that (With the LD extension):

    GraphicsWindow.Show()
    LDGraphicsWindow.CancelClose = "True"
    LDGraphicsWindow.ExitOnClose = "False"
    LDGraphicsWindow.Closing = OnClosing
    
    
    Sub OnClosing
      Ans = LDDialogs.Confirm("Do you wish to exit?","Small basic")
      If Ans = "Yes" Then
        Program.End()
        EndIf
      EndSub


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Thursday, October 22, 2015 6:17 PM
    Answerer
  • I know that it is kind of long, but here is a fully working one with a confirm box. You can move stuff around of course. Also you can change what it closes. Like if you want to close the entire program just write:

    Program.End
    where the "Yes"part is.

    XButton = Controls.AddButton("X", 50, 50)
    Controls.ButtonClicked = Close
    
    Sub Close 
      LastClicked = Controls.LastClickedButton 
      If (LastClicked = XButton) Then
        GraphicsWindow.BrushColor = "LemonChiffon"
        Box = Shapes.AddRectangle(200, 100)
        Shapes.Move(Box, 250, 300)
        GraphicsWindow.BrushColor = "Black"
        Stuff = Shapes.AddText("Are you sure you want to exit?")
        Shapes.Move(Stuff, 260, 300)
        Yes = Controls.AddButton("Yes", 350, 350)
        No = Controls.AddButton("No", 300, 350)
        
      ElseIf (LastClicked = Yes) Then
        Shapes.Remove(Box)
        Controls.Remove(Yes) 'Remove whatever you want
        Controls.Remove(No)
        Shapes.Remove(Stuff)
        
      ElseIf (LastClicked = No) Then
        Controls.Remove(Yes)
        Controls.Remove(No) 'Remove just the confirm box
        Shapes.Remove(Box)
        Shapes.Remove(Stuff)
      EndIf
    EndSub

     

    Friday, October 23, 2015 9:47 PM

All replies

  • Hi Dereklouis,

    Are you trying to get a messagebox to confirm to exit from WinForm? If so please write the below code into your Form's FormClosing event. Hope this helps you.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Exit or no?",
                    "Confirm Exit",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Information) == DialogResult.No)
        {
            e.Cancel = true;
        }
    }

    Thanks,

    Sabah Shariq


    Please remember to mark the replies as answers if they help. If this post is helpful please vote

    Thursday, October 22, 2015 11:07 AM
  • This is the Small Basic forum, so I think he meant to program it in Small basic.

    Unfortunately you cannot have a confirm box unless you have an extension.

    Here is how you could do it without one.

    GraphicsWindow.Show()
    
    
    While 1 = 1
      Program.Delay(10)
      If GraphicsWindow.LastKey = "Escape" Or GraphicsWindow.LastKey = "X" Then
        Program.End()
      EndIf
      EndWhile

    And if you use litdev's extension:

    GraphicsWindow.Show()
    GraphicsWindow.KeyDown = KeyDownSub
    
    
    
    Sub KeyDownSub
      If GraphicsWindow.LastKey = "Escape" Or GraphicsWindow.LastKey = "X" Then
        Confirm = LDDialogs.Confirm("Do you wish to exit?","SmallBasic")
        If Confirm = "Yes" Then
          Program.End()
        EndIf
      EndIf
    EndSub

    Hope this helps!!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Thursday, October 22, 2015 2:59 PM
    Answerer
  • Zock, I think he didn't ment to close by the character "X" , but through the button with X in the top of Windows programs. This isn't caught by the LastKey statement.

    Jan [ WhTurner ] The Netherlands

    Thursday, October 22, 2015 5:09 PM
    Answerer
  • Oops. My bad.

    Here is how you would do that (With the LD extension):

    GraphicsWindow.Show()
    LDGraphicsWindow.CancelClose = "True"
    LDGraphicsWindow.ExitOnClose = "False"
    LDGraphicsWindow.Closing = OnClosing
    
    
    Sub OnClosing
      Ans = LDDialogs.Confirm("Do you wish to exit?","Small basic")
      If Ans = "Yes" Then
        Program.End()
        EndIf
      EndSub


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Thursday, October 22, 2015 6:17 PM
    Answerer
  • I know that it is kind of long, but here is a fully working one with a confirm box. You can move stuff around of course. Also you can change what it closes. Like if you want to close the entire program just write:

    Program.End
    where the "Yes"part is.

    XButton = Controls.AddButton("X", 50, 50)
    Controls.ButtonClicked = Close
    
    Sub Close 
      LastClicked = Controls.LastClickedButton 
      If (LastClicked = XButton) Then
        GraphicsWindow.BrushColor = "LemonChiffon"
        Box = Shapes.AddRectangle(200, 100)
        Shapes.Move(Box, 250, 300)
        GraphicsWindow.BrushColor = "Black"
        Stuff = Shapes.AddText("Are you sure you want to exit?")
        Shapes.Move(Stuff, 260, 300)
        Yes = Controls.AddButton("Yes", 350, 350)
        No = Controls.AddButton("No", 300, 350)
        
      ElseIf (LastClicked = Yes) Then
        Shapes.Remove(Box)
        Controls.Remove(Yes) 'Remove whatever you want
        Controls.Remove(No)
        Shapes.Remove(Stuff)
        
      ElseIf (LastClicked = No) Then
        Controls.Remove(Yes)
        Controls.Remove(No) 'Remove just the confirm box
        Shapes.Remove(Box)
        Shapes.Remove(Stuff)
      EndIf
    EndSub

     

    Friday, October 23, 2015 9:47 PM