Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chicken and Egg problem

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
Hello All,

We have a Windows form with a couple dozen comboboxes and textboxes on it. Included in these controls are a handful of comboboxes which display static data. The underlying tables behind the Sql Server stored procedures include an nvarchar(4000) column named Details. The users want a Details button which when clicked will pop up the Details data for the currently selected item in the currently selected combobox.

To simplify things, lets say that they are Combo1, Combo2, Combo3 and Combo4 and the associated button is cmdDetails. There are a couple dozen controls on the form in total. I don't want to have to do a bunch of event coding so here is what I have done so far:

A. Set up GotFocus event for Combo1, ..., Combo4 to enable cmdDetails
Code:
Private Sub cmbStructureClassification_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStructureClassification.GotFocus, cmbExposureCategory.GotFocus, cmbTopographicCategory.GotFocus, cmbSeismicZone.GotFocus
    'Save currently active control in module level variable
    mDetailsControl = CType(sender, ComboBox)
    [b]Me.cmdDetails.Enabled = True[/b]
End Sub
B. Set up LostFocus event for Combo1, .l.., Combo4 to disable cmdDetails
Code:
Private Sub cmbStructureClassification_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStructureClassification.LostFocus, cmbExposureCategory.LostFocus, cmbTopographicCategory.LostFocus, cmbSeismicZone.LostFocus
    [b]Me.cmdDetails.Enabled = False[/b]
End Sub
The problem is that when I click cmdDetails the LostFocus in B above disables it so its Click Event is cancelled by the system. Is there any way to determine whether the mouse was clicked within the control rectangle for cmdDetails so that I cannot disable the button in that case? Maybe something like this:
Code:
Private Sub cmbStructureClassification_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStructureClassification.LostFocus, cmbExposureCategory.LostFocus, cmbTopographicCategory.LostFocus, cmbSeismicZone.LostFocus
    'Following is PseudoCode
    MouseClickedInCommandButton = IsMouseOverCommandButton
    If Not MouseClickedInCommandButton Then 
        Me.cmdDetails.Enabled = False
    End If
End Sub
Thanks in advance for any ideas, suggestions, etc!

Have a great day!

j2consulting@yahoo.com
 
loose hte lost focus of the combo boxes and code another Got Focus routine which looks at WHAT control is getting the focus and what previously had the focus?



MichaelRed


 
The solution turned out to be fairly simple after I thought about it for a while. First I defined a module level variable to keep track of the active control and then used AddHandler to set GotFocus handling for all of the controls in the form except for the ones I wanted to handle separately.
Code:
Private mActiveControl As Object

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim exclControls As String = ",ComboBox1,ComboBox2,ComboBox3,Button1,"
   For Each ctl As Control In Me.Controls
      If exclControls.IndexOf(ctl.Name) = -1 Then
         AddHandler ctl.GotFocus, AddressOf DisableCommandButton
      End If
   Next
End Sub
Then I created normal event code for the specific controls as well as the private sub DisableCommandButton.
Code:
Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus, ComboBox2.GotFocus, ComboBox3.GotFocus
   Me.Button1.Enabled = True
   mActiveControl = sender
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   MsgBox(CType(mActiveControl, Control).Name)
End Sub

Private Sub DisableCommandButton(ByVal sender As Object, ByVal e As System.EventArgs)
   Me.Button1.Enabled = False
End Sub



Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top