weird behaviour of button
weird behaviour of button
weird behaviour of button
weird behaviour of button
weird behaviour of button
weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button
weird behaviour of button weird behaviour of button
weird behaviour of button
Go Back  Xtreme Visual Basic Talk > > > weird behaviour of button


Reply
 
Thread Tools Display Modes
  #1  
Old 08-13-2017, 03:33 PM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 287
Default weird behaviour of button


I am just wondering if anyone has come across this before...
Here is the code attached to the enter button on my single form:
Code:
Private Sub cmdEnter_Click()
Call writetofile
End Sub
Can't get much simpler than that, right? Problem is, when the button is clicked, nothing happens. Click it a second time and everything works absolutely perfectly as it is supposed to. I set a watch on the first watchable line of the 'writetofile' sub and that too was fine, but of course this only happened on the second click, so it's not possible to see why the sub isn't called on the first click!
Any suggestions?
Reply With Quote
  #2  
Old 08-13-2017, 08:37 PM
Kluz's Avatar
Kluzweird behaviour of button Kluz is offline
Sapience.Aquire

Super Moderator
* Expert *
 
Join Date: Oct 2003
Location: Lake Bluff, Ill., U.S.
Posts: 3,445
Default

It may be that the first click sends the focus to that button. Try playing with the TakeFocusOnClick Property and the TabStop Property.
__________________
No the other right mouse click
Reply With Quote
  #3  
Old 08-14-2017, 10:06 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 287
Default

There doesn't appear to be a takefocusonclick property in VB6 at all (which is probably why I've never heard of it!) though there is in VBA - so do I need to reference VBA from my project? Would that work?

OK - update.
This only started happening after I'd introduced a couple of routines for two number-only textboxes, and I have just noticed what might be causing the problem.
Code:
Private Sub nbComp1_LostFocus()
If InStr(1, nbComp1.Text, ".") Then
    nbComp1.Text = Replace(nbComp1.Text, ".", "")
Else
nbComp2.SetFocus
End If
End Sub
Basically, if the user enters a decimal point in the box, this routine removes it (the second text box contains the same routine), but could the 'resetting' of focus be the bug?
I have removed the routines and everything is working properly again, though I won't be able to trap the errors. Ho hum, never mind!

Last edited by starmanMike; 08-14-2017 at 11:28 AM. Reason: additional info
Reply With Quote
  #4  
Old 08-14-2017, 04:34 PM
Cerian Knight's Avatar
Cerian Knightweird behaviour of button Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,193
Default

You might be able to conditionally prevent the focus from changing to nbComp2 when the button is clicked. Here is a simple example:
Code:
Dim flgMouseDown As Long

Private Sub cmdEnter_Click()
Debug.Print "hello"
End Sub

Private Sub cmdEnter_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
flgMouseDown = 1
End Sub

Private Sub cmdEnter_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
flgMouseDown = 0
End Sub

Private Sub nbComp1_LostFocus()
If flgMouseDown = 0 Then nbComp2.SetFocus
End Sub
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #5  
Old 08-15-2017, 06:24 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 59
Default

Rather than use the LostFocus event, perhaps you could ensure a numeric entry using the KeyPress event instead?

Add code to assess entry in the textbox
Code:
Public Sub OnlyNumeric(sText As String, iKeyAscii As Integer, Optional blnAllowDecimal As Boolean = False)
If Chr(iKeyAscii) = "." Then
    'decimal point entered. disallow if blnAllowDecimal false or text already contains a decimal
    If blnAllowDecimal = False Or InStr(1, sText, ".") > 0 Then
        iKeyAscii = 0
    End If
ElseIf InStr(1, "0123456789", Chr(iKeyAscii)) < 1 And Not (iKeyAscii = 8 Or iKeyAscii = 13 Or iKeyAscii = 27) Then
    'only allow a number. ignore 8, 13, 27 which are backspace, enter, escape respectively
    iKeyAscii = 0
End If
End Sub
Call the code in the textbox's keypress event
Code:
Private Sub nbComp1_KeyPress(KeyAscii as integer)
call OnlyNumeric(nbComp1.text,KeyAscii)
end sub
Reply With Quote
Reply

Tags
button, click, writetofile, set, watch, perfectly, time, absolutely, watchable, supposed, called, fine, line, happened, suggestions, code, attached, enter, behaviour, wondering, single, form, weird, clicked, simpler


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
weird behaviour of button
weird behaviour of button
weird behaviour of button weird behaviour of button
weird behaviour of button
weird behaviour of button
weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button weird behaviour of button
weird behaviour of button
weird behaviour of button
 
weird behaviour of button
weird behaviour of button
 
-->