Issues with using a cancel button and leaving a textbox.

si2030
si2030 used Ask the Experts™
on
Hi Experts

I have a form with among other things a cancel button. However, I also have a combobox which the form sets a the start point when the form is opened.

The thing is, when I open the form its starting in the combobox (as part of the user control) and I have the combobox set so if you leave it without selecting an option it throws up a messageBox. The sub is presented below..

My Problem: If I open the form and then click on the cancel button (to immediately close the form) it leaves the combobox first and since there is nothing selected it throws up the messageBox. It should instead close the form.

I have tried to set the button click event but it doesnt fire before the leave for the combobox event.


How do I detect the user is clicking on the cancel button and in this instance, ignore the fact there is nothing in the combobox and go straight on and close the form??

Kind Regards

Simon
Private Sub USCClientList_LEAVE() Implements creditorInvoiceINTERFACE.IfrmCreditorInvoice.USCClientList_LEAVE
 
        Try
 
            If thisUserControl.btnSearch.Focused Then
 
                Return
 
            End If
 
            If thisUserControl.cmbxClientName.SelectedIndex = -1 Then
 
                Dim reply = MessageBoxEx.Show("You must select a valid client from the dropdown list...", "Selection Error", _
                                                                MessageBoxButtons.OK, MessageBoxIcon.Error, _
                                                                MessageBoxDefaultButton.Button1)
 
                thisUserControl.cmbxClientName.Focus()
 
            Else
 
                lblInvoiceDate.Enabled = True
                dtInvoiceDate.Enabled = True
                dtInvoiceDate.Focus()
 
            End If
 
        Catch ex As Exception
 
            manageWindowsViewError("creditorTASK", "frmCreditorInvoice", "USCClientList_LEAVE", ex)
 
        End Try
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
The problem is, the Leave event of the combobox would execute before any other code such as Cancel_click or mousedown etc. I would suggest you change the strategy here and validate the combobox when some button (such as save) is clicked rather than validating on leave event.

Commented:
hi,

i will suggest that you create an event that can be raised in the control and called in your form.
eg
Friend Event BeforeLeave(ByRef Response As Boolean)

from your code---

Private Sub USCClientList_LEAVE() Implements creditorInvoiceINTERFACE.IfrmCreditorInvoice.USCClientList_LEAVE
 dim bolResponse as boolean =false

        Try
            RaiseEvent BeforeLeave(bolResponse)
                 if bolResponse
                       exit sub
                 end
            If thisUserControl.btnSearch.Focused Then
 
                Return
 
            End If
........

the following will be raised in your form

    Private Sub UserControl11_BeforeLeave(ByRef Response As Boolean) Handles UserControl11.BeforeLeave
        'determine which control was clicked, if button tell the response to be False and use this as a switch in your control to execute the preceeding code or terminate it
        If TypeOf Me.ActiveControl Is Button Then
            Response = True
        End If
    End Sub

Author

Commented:
Hi miketayo,

Am having a little trouble implementing your solution. In particular I am having an issue with the following line:

Private Sub UserControl11_BeforeLeave(ByRef Response As Boolean) Handles UserControl11.BeforeLeave

its throwing an error on  "UserControl11.BeforeLeave"? not sure what should go there as I have a usercontrol called thisUserControl and the control in it I am testing is "thisUserControl.cmbxClientName" as in "cmbxClientName"
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Commented:
hi

you need to include this code in the control before using this in your form section
1. Friend Event BeforeLeave(ByRef Response As Boolean)
2. in your leave event in the control insert  (the bold text)

Private Sub USCClientList_LEAVE() Implements creditorInvoiceINTERFACE.IfrmCreditorInvoice.USCClientList_LEAVE
dim bolResponse as boolean =false

Try
RaiseEvent BeforeLeave(bolResponse)
        if bolResponse 'the bolresponse is required to be a reference sent to the control from the form area.  test it and decide on what to do if true, it exits the function else it continue
                exit sub
         end
(insert the rest of the codes here)
End sub
 3.Private Sub thisUserControl1_BeforeLeave(ByRef Response As Boolean) Handles thisUserControl1.BeforeLeave
        'determine which control was clicked, if button tell the response to be False and use this as a switch in your control to execute the preceeding code or terminate it
        If TypeOf Me.ActiveControl Is Button Then
            Response = True
        End If
    End Sub
No. 3 is in the form area where the control is being used,
The logic of this is that when the button is click on the client window, it will trigger the leave event in the control because that is the control with focus.
When this is triggered, when this event is fired, another event is fired with in, causing it to return to the window area to call the Beforeleave event of the control in the window area.
If i understood your issue well, this will work, because i have tested it and it working. so i will appreciate if you can re explain the issue.

Author

Commented:
I have included a screen print of the error I am getting... Sytax error.
Handles.jpg

Author

Commented:
Sorry, Just a quick note to say I have removed the "cmbxClientName" and I get the same error...

Commented:
Did you include this in the control class
Friend Event BeforeLeave(ByRef Response As Boolean)

Author

Commented:
Yes third variable in...

I looked up the error and got this...

http://msdn.microsoft.com/en-us/library/32787dt6(VS.80).aspx

Not sure how this fits in...
Public Class frmCreditorInvoice
    Implements IfrmCreditorInvoice
 
#Region "VARIABLES"
    
    Private Presenter As New frmCreditorInvoicePRESENTER
 
    Private thisUserControl As New uscClientSelect()
 
    Friend Event BeforeLeave(ByRef Response As Boolean)
 
    Private accountComboBox As New DataGridViewComboBoxColumn
    Private assetComboBox As New DataGridViewComboBoxColumn
 
    Private clearComboboxFLAG As Boolean = False
 
    Private newColumn As New DataGridViewMultiColumnComboColumn()
 
 
    Private accountList As Object = Nothing
    Private assetList As Object = Nothing
 
    Private clientNo As Integer = -1
    Private postingId As Integer = -1
 
#End Region

Open in new window

Commented:
hi

the event should be in this class uscClientSelect

Commented:
Also the call to this control must have withebemts

Private thisUserControl As New uscClientSelect()
Private withevents thisUserControl As New uscClientSelect()

Author

Commented:
Hi miketayo,

I believe the logic is sound and that this would work but my execution isnt working. I have never used this code before and am still learning...

I am not sure what should be in the user control and what should be in the class containing the leave event.

I have tried a few things but am getting stuck. Wondering if you could spell this out for me.

This is where I am at:

1. I have the following in the form code:

  Private thisUserControl As New uscClientSelect()
    Private WithEvents thisUserControl1 As New uscClientSelect()

2. Also in the form code I have the sub:

Private Sub UserControl11_BeforeLeave(ByRef Response As Boolean) Handles thisUserControl1.BeforeLeave

        'determine which control was clicked, if button tell the response to be False and use this as a switch in your control to execute the preceeding code or terminate it
        If TypeOf Me.ActiveControl Is Button Then
            Response = True
        End If

    End Sub

No errors  for 1. and 2.


3. in the usercontrol I have:

Friend Event BeforeLeave(ByRef Response As Boolean)



I am now getting an error in the leave event. (see the jpg below).

Thanks for your time in this..

Simon






error.jpg
Commented:
How about

If Me.ActiveControl.Name = "btnCancel" Then

                Exit Sub

            End If

Simple and it identifies that the cancel button has been pressed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial