Detect click on textbox in form - vba

I want a certain function to be run every time someone clicks on a textbox in my form. So far I have managed to detect when it gets focus and when it is doubleclicked, using the following two eventhandlers, but I am unsure about how to catch when it is clicked once while it already has focus.
Does anyone here have any experience with catching such an event? I don't seem to find any obvious suspects in the dropdown-menus of the VBA editor.
Private Sub tbxTil_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.tbxTil = format(oppdater_dato(CDate(Me.tbxTil)), "dd.mm.yy", vbMonday, vbFirstFourDays)
End Sub
Private Sub tbxTil_Enter()
Me.tbxTil = format(oppdater_dato(CDate(Me.tbxTil)), "dd.mm.yy", vbMonday, vbFirstFourDays)
End Sub

You can use the Mouse events, something like this:
Private Sub tbxTil_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MsgBox "Click"
End Sub

Related

Empty pre-filled TextBox on MouseDown/MouseUp only FirstTime

I'm newbie in word-vba (just to let you know that my question could be really stupid).
I would like to clear a textbox only when I click in the textbox the first time.
I've tried For... Next but I wasn't able to comfigure it correctly
Private Sub SWName_Field_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
SWName_Field.Text = ""
End Sub
I would like that the code will work exactly the same way it works but when I put some text and for example the user make a mistake or typo error the second click in the textbox shouldn't clear the text inside.
Thank you for the support
There's no inbuilt activity state identifier in any UserForm control. So you need to use meta data to specify and identify whether or not your mousedown is happening for the first time.
Use Tag property of the control for that.
See the code comments for details.
Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'/ Use the Tag field to determine and store the state of Text Box.
If Len(Me.TextBox1.Tag) < 1 Then
'/ If Mousedown for the very first time then TextBox's tag is empty.
'/ Go ahead, clean the textbox.
'/ And set a text in tag.
Me.TextBox1.Text = ""
Me.TextBox1.Tag = "Text Cleared"
End If
End Sub
You can use a Static local variable to "remember" whether the handler was executed at least once or not:
Private Sub SWName_Field_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Static executed As Boolean
If Not executed Then
SWName_Field.Text = ""
executed = True
End If
End Sub
The state of the Static local is tied to your UserForm instance - the value will be "remembered" for as long as the form instance is alive.
This means, if you're showing the form's default instance, the state won't necessarily be reset. You will want to ensure you get a fresh default form state every time the form is shown, not just the first time - to do this you New up the form:
With New UserForm1
.Show
End With
If you just do UserForm1.Show, then you don't control when the form instance gets created - VBA does.
You'll also want to control when the form instance gets destroyed - you can do that by handling the form's QueryClose event:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True ' cancel the destruction of the object
Me.Hide ' hide the instance instead
End If
End Sub
With that, the object will be destroyed when execution reaches End With. Without it, the object will be destroyed if the user clicks the "X" button, and you probably don't want that to happen (especially if you need to access the form's state after it's closed).

Wait for data to be input then run sub.

I need a very simple system that saves a bar code number to a sheet every time it is scanned. It works great but I have to click a cmd button in between each scan or hit enter. Is there any was I could use the execute after the text box changes? But if I use the
Private Sub txtCode_Change()
As a title then it only takes the first number of the code and uses that instead of waiting the .25 seconds for the rest of the bar code to be imputed. Any ideas or help would be greatly appreciated.
You probably can configure your bar-code scanner to send an Enter or a Tab key after the scanned bar-code.
If your scanner sends a Tab key, you can use the TextBox_AfterUpdate event:
Private Sub txtCode_AfterUpdate()
'Do something.
End Sub
If your scanner sends an Enter key, you can use the TextBox_KeyDown event:
Private Sub txtCode_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
'Do something.
End If
End Sub
If your bar-code is a fixed-length string, you can still use the TextBox_Change event like the following:
Private Const BARCODE_LENGTH As Integer = 12
Private Sub txtCode_Change()
If Len(txtCode.Text) = BARCODE_LENGTH Then
'Do something
End If
End Sub
Hope that helps.

How To Make a Table for Controls (Excel VBA)

I hope you are enjoying your weekend!
I am here to ask if there is a way to initialize a table or list layout inside of the frame of a userform. And, if this is possible, whether or not I can place controls (text boxes and labels) into the cells instead of strings of text.
Thank you in advance for your answers/comments.
*Edit: Follow up question. Assuming the above is possible in Excel VBA, can I also set the table (only one column) or list to have its dimensions based on the control's width and height?
You can embed a Microsoft Office Spreadsheet. Go to tool box additional controls.
You can access ot just like you do an Excel WorkSheet.
Spreadsheet1.Range("A1")
You can also use Private WithEvents to hook it's events. Here I did it in the Userform, however, I could have do it from a class module if I wanted.
Private WithEvents MySpreadsheet1 As OWC11.Spreadsheet
Option Explicit
Private WithEvents MySpreadsheet1 As OWC11.Spreadsheet
Private Sub UserForm_Initialize()
Set MySpreadsheet1 = Spreadsheet1
End Sub
Private Sub MySpreadsheet1_BeforeContextMenu(ByVal x As Long, ByVal y As Long, ByVal Menu As OWC11.ByRef, ByVal Cancel As OWC11.ByRef)
End Sub
Private Sub Spreadsheet1_BeforeContextMenu(ByVal x As Long, ByVal y As Long, ByVal Menu As OWC11.ByRef, ByVal Cancel As OWC11.ByRef)
End Sub
It's not fully featured but you can do alot with it.
How To Use the Spreadsheet Web Component with Visual Basic
You can hide the columns and rows, but it's too slow to be practical.
Spreadsheet1.Columns("E:ZZZ").EntireColumn.Hidden = True
Spreadsheet1.Rows("10:262144").EntireRow.Hidden = True

How would I go about creating a textbox help label (cuebanner/cuetext)?

I wanted to create a textbox which has a help label inside the box which then disappears when the box has characters entered into it. I found one way of doing it which involves loading the form with text inside the textbox in the colour grey and then removing it when the user clicks on the box... The problem with this is i wanted to use a string.IsNullOrEmpty(textboxIP) but when the user hasn't typed anything into the box, the program sees the box as not empty as it has the pre-loaded writing in it. This is the code I used to remove the text on user click...
Dim WatermarkIP As String = "Yes"
Dim WatermarkPing As String = "Yes"
Private Sub textboxIP_Enter(sender As Object, e As EventArgs) Handles textboxIP.Enter
If WatermarkIP = "Yes" Then
textboxIP.Clear()
textboxIP.ForeColor = Color.Black
WatermarkIP = "No"
End If
End Sub
Private Sub textboxPing_Enter(sender As Object, e As EventArgs) Handles textboxPing.Enter
If WatermarkPing = "Yes" Then
textboxPing.Clear()
textboxPing.ForeColor = Color.Black
WatermarkPing = "No"
End If
End Sub
Does anyone know of a better way of creating a greyed out help/hint label inside the textbox which IS NOT counted as text inside the box, does not have to be deleted by the user before they can type in the box and is maybe a bit simpler?
I've found it finally!
This will give you that watermark text on your textbox controls:
Imports:
Imports System.Runtime.InteropServices
Global Declarations in your main class:
Private Const EM_SETCUEBANNER As Integer = &H1501
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
Functions in your main class:
Private Sub SetCueText(ByVal control As Control, ByVal text As String)
SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
End Sub
Usage (usually in form_load event):
SetCueText(TextBox1, "blah")
SetCueText(TextBox2, "blahblah")
Hope this helps :)
Credit: http://www.vbforums.com/showthread.php?638105-CueBanner-Watermark-text-for-Textboxes

Mouse hovering event

I want to do an mouse hovering event, when the mouse is over an button I want to change button text color and font size, I have try this code but doesn't work:
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.MediumBlue()
Command1.FontSize = 10
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.White()
Command1.FontSize = 8
End Sub
Can anyone give me a suggestion i have search on Google and try different ways with mouse event handler but didn't work.
First, instead of tracking every mouse move, you can rely on MouseEnter and MouseLeave events of the button.
Second, do not forget to add Handles <Control>.<Event> clause at the declaration of your event-handling procedures.
Result:
Private Sub Command1_MouseEnter(sender As Object, e As EventArgs) _
Handles Command1.MouseEnter
Command1.FontSize = 10
End Sub
Private Sub Command1_MouseLeave(sender As Object, e As EventArgs) _
Handles Command1.MouseLeave
Command1.FontSize = 8
End Sub
Also please do not forget that some users are preferring keyboard control.
This means that
You might want to equip the button with an accelerator.
Command1.Text = "&Launch" (now Alt+L activates the button)
Note: accelerator character for winforms is &, for wpf is _.
You might want to make your entry/leave effect also when the button receives/looses keyboard focus (focus is moved using Tab and Shift+Tab key).
You can try making your changes into MouseEnter and MouseLeave
Private Sub RightButton_MouseEnter(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseEnter
RightButton.ForeColor = Color.AliceBlue
RightButton.Font = New Font(RightButton.Font, 12)
End Sub
Private Sub RightButton_MouseLeave(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseLeave
RightButton.ForeColor = Color.White
RightButton.Font = New Font(RightButton.Font, 10)
End Sub