Id like it so that when you click the 'x' button to close the program it doesnt just end.

How do I apply code to the 'x' button?

All help is appreciated, thanks in advance

-Russell aka Rabbit

what you mean? "it doesnt just end."
You want it will hide? then write code in form_laod
"me.hide" or "form1.hide"

Id like a message box prompt to come up to ask the user if he or she is sure about their decision.

Id like it so that when you click the 'x' button to close the program it doesnt just end.

Use the Form_Unload() event

Private Sub cmdX_Click()
     unload Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim intReturn As Integer
    
    intReturn = MsgBox("Ready to Exit", vbOKCancel, "Exit?")
    If intReturn = vbOK Then
    Else
        Cancel = True
    End If
End Sub

Hi rabbithaveit

I would suggest the API way as it gives you more flexibility... See picture attached...

Please place a Command Button on the form so that you can exit.

Paste this code in the general declaration of the form...

Option Explicit

Private Declare Function DeleteMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long

Private Const MF_BYPOSITION = &H400&

Private ReadyToClose As Boolean

Private Sub RemoveMenus(frm As Form, remove_restore As Boolean, _
remove_move As Boolean, remove_size As Boolean, remove_minimize As Boolean, _
remove_maximize As Boolean, remove_seperator As Boolean, remove_close As Boolean)
    
    Dim hMenu As Long
    
    '~~> Get the form's system menu handle.
    hMenu = GetSystemMenu(hwnd, False)
    
    If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
    If remove_seperator Then DeleteMenu hMenu, 5, MF_BYPOSITION
    If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
    If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
    If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
    If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
    If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
End Sub

Private Sub cmdClose_Click()
    ReadyToClose = True
    Unload Me
End Sub

Private Sub Form_Load()
    '~~> Remove the Close system menu item and the menu separator.
    RemoveMenus Me, False, False, False, False, False, True, True
End Sub

'~~> Cancel if ReadyToClose is false.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Cancel = Not ReadyToClose
End Sub

Thanks for replying guys.

hkdani thanks again. I will be sure to try that solution

Koolsid I like the concept but it seems a bit complicated for mke at the moment....
thanks for the reply. But I dont want answer per say...I prefer solutions. So I can learn from them. I'll be sure to look up the API and put your coding to use soon. So thank you

Will be back with a post if it works hkdani

Thanks again guys

-Russell aka Rabbit

Nah, unfortunately that didnt work hkdani. But thanks for the post

koolsid I'll do some research on API and use your code in the meanwhile.

Will post back with results

-Russell aka Rabbit

In the code editor click (i think its the right) combo box at the top and choose where it says terminate with a lightning bolt next to it

@koolsid

Does this code just disable the 'x' button?

Thats the conclusion im drawing from your image there.

@jbennet

In the code editor click (i think its the right) combo box at the top and choose where it says terminate with a lightning bolt next to it

Can you supply an image to support that?

Thanks in advance

-Russell aka Rabbit

http://www.vb6.us/files/Image/Hello-World/Visual-Basic-Tutorial-screen5.jpg

Look at this image

You see where the two drop down boxes say Command1 and Click?

Change that to say form1, and the second one to say (i think its either Terminate or onClose, will have a lightning bolt symbol next to it)

That will create a code block. In there you can type whatever you want to happen when they try to close the form

@koolsid

Does this code just disable the 'x' button?

Thats the conclusion im drawing from your image there.

It not only disable's the close button but also removes the 'close' option from the form's shortcut menu ;)

The API is very simple... All you need to do is copy and paste the code in the general declaration of the area....

Edit: Attaching a project sample for you :)

Nah, unfortunately that didnt work hkdani. But thanks for the post

That's funny: it works on my vb6 program. Actually, that's the method that I learned from a Microsoft CD on VB6 programming fundamentals. I've been using that method for years. It's odd that it doesn't work for you.

What I like about the method that Microsoft recommends is it's simplicity. That's why they made Visual Basic, because they knew that programs could be written faster and programmers could be more productive, if they were provided a simple way to perform a task such as the task you describe.

From what I've read and studied from the established authors and programmers, they always recommend the KISS method: Keep it Simple Stupid.

Yes, it's true that you can use API's. And it looks quite impressive. But in the end, in the real world it's all about achieving a desired end within a certain time frame.

If you can't achieve that end with the tools that VB6 provides, then VB6 has the flexibility of being able to call the Windows APIs from the Platform Software Developer Kit (PSDK). But as a general rule, use the tools that VB6 provides and only venture into APIs if VB6 doesn't provide the means to accomplish your end.

When it comes right down to where the rubber meets the road the end user will never see your code. He only sees your program. The programmer should not try to impress someone with his knowledge of Windows APIs, the end user probably doesn't have a clue as to what an API is.

Knowledge tends to puff people up. But a truly great programmer uses his knowledge to meet the needs of his client not to enhance his ego.

@Koolsid

I actually want the 'x' enabled. But I want to apply code to it.

@Jbennet

Thanks, that worked. Just one problem. Whether Yes or NO is chosen. The program still closes. Any reason why?

@Koolsid

I actually want the 'x' enabled. But I want to apply code to it.

@Jbennet

Thanks, that worked. Just one problem. Whether Yes or NO is chosen. The program still closes. Any reason why?

Oh Ok I understand :D

Is this what you want?

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Ret = MsgBox("Do You want to Exit?", vbOKCancel)
        '~~> If user decides against it...
        If Ret = vbCancel Then Cancel = 1
    End If
End Sub
commented: Very helpful person. Persistent with help lol +1

Check the attachement file..
I hope that it will help you...

Gentlemen / Women...People

Thank you so very much for your suggestions and help, I really do appreciate it.

I have not yet gotten to try the zip files. But I am going to test them now. Again, I do appreciate it.

-Russell aka Rabbit

KSS and Sidz

You have both my thanks!

@Sidz, the zip file you gave me actually helped with another situation I had. lol One less thread to start up. Because I want my icons to show. So thank you.

@KSS, the zip you gave was EXACTLY what I was looking for. Thank again.

Now that I think of it...That final snippet you provided Sidz was the same thing in the zip KSS gave. Thanks again.

I do appreciate it.

Thread Solved!

-Russell aka Rabbit

No problem. Glad you got it solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.