Disable iBeam pointer in TextBox using VB.NET - vb.net

I am currently working on a TextBox using VB.NET 2015 that is read-only and only inserts characters by a button click event. I want to hide or disable the iBeam inside the TextBox to let the user know that it is only accessible by the button click and not by manual typing on the actual keyboard. I have tried changing its ReadOnly property to True and cursor property to cursors other than the iBeam but they don't seem to work.
Is there another way, may it be a code or a property that disables the iBeam in the TextBox when its accessed?
This image is my example of an on-screen keyboard. As you can see, the iBeam on the TextBox is visible as soon as I click on one of the on-screen keys.

Use the HideCaret() API call from the GotFocus() event of your TextBox:
Private Declare Function HideCaret Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
HideCaret(TextBox1.Handle)
End Sub

Related

How do I regaing focus on form?

I have a PC in a corporate production environment and need to make sure the same program loads over and over. I upgraded to Windows 7 and my start-up program keeps losing focus because of MacAfee. I tried to uninstall MacAfee but it's an enterprise version and it won't let me. I tried to do some programming in my vb.net application to regain focus but nothing has worked.
Here's a picture of how the program boots up - notice the form is a lighter color and has lost focus:
http://i.imgur.com/Qwlzuzw.jpg
Here's a picture of how the form SHOULD load up after I click on it with the mouse to fix it - notice the darker color of the form:
http://i.imgur.com/DuLyCsC.jpg
Here's why I think MacAfee is the problem - if I alt tab, the MacAfee updater icon shows up:
http://i.imgur.com/opgOWHW.jpg
Any ideas to solve my problem programmatically or otherwise?
Here's sample code on a timer that gets the foreground window handle, if it's not Me, make it Me
Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim focusedWindow As System.IntPtr
focusedWindow = GetForegroundWindow()
If Not Me.Handle.Equals(focusedWindow) Then
SetForegroundWindow(Me.Handle)
End If
End Sub
In form1.shown sub write this:
For i = 0 To 100
Me.Activate()
Next
This will try 100 times to focus on your form when it shows up.

Detecting whether a mouse button is down in vb.net

I don't want to use the mouseup event because I want to allow the user to drag outside the control. I need to check the state via a timer. I vaguely remember doing this before at some point by using
If MouseButtons = MouseButtons.Left Then...
But now it says that MouseButtons is a type and cannot be used as an expression (which is true, its an enum type).
Maybe they changed things or maybe I just remember wrong.. either way, how would I check if the button is still down?
This is tried and tested. I created a new class: testmouseclass and created a shared function you can use anytime to determine if the LeftMouseButton is down. This is possible with the help of GetAsyncKeyState call.
Imports System.Runtime.InteropServices 'Need to import...
Public Class testmouseclass
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function GetAsyncKeyState(ByVal vkey As Long) As Long
End Function
Public Shared Function LeftMouseIsDown() As Boolean
Return GetAsyncKeyState(Keys.LButton) > 0 And &H8000
End Function
End Class
Example Usage
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
If testmouseclass.LeftMouseIsDown() Then MsgBox("It IS!")
End Sub
Depending on the timer tick's this can be a headache as well. Not sure how you are using this, but in my example I had the timer at 3 seconds and when I held the LeftMouseButton down a message popped up and when I clicked it again it popped up again because of the timer and the left mouse was down. Also this work's even outside of your application...
What you want is to test the Mouse.LeftButton property.
If System.Windows.Input.Mouse.LeftButton = Windows.Input.MouseButtonState.Pressed Then

Sending a message to any windows textbox just by focus

I need to know how I can send a message to any text box of windows.
If a focus the google chrome url textbox, then I will "auto paste" the message, or if I focus a Word Document string, or notepad, or anything!
I got a code ho sends by setting the iHwnd, findwindow and findwindowex, but I need to set any time I want to change the final program, and thats why I need an automatic program "focus based".
Here is what I have so far...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iHwnd As IntPtr = FindWindow("notepad", vbNullString)
Dim iHwndChild As IntPtr = FindWindowEx(iHwnd, IntPtr.Zero, "Edit", vbNullString)
SendMessage(iHwndChild, WM_SETTEXT, 0, "Hello World!")
End Sub
Sorry for my bad english!
SendMessage is always going to require a specific window handle, or broadcast to all top level windows. To continue with your current code, you could first try to retrieve the active window's handle with GetActiveWindow or similar function.
Alternately, you could experiment with the SendKeys class to send your text. SendKeys always targets the currently active control (as if the user were typing directly on the keyboard), so you don't need to concern yourself with finding window handles or titles.

VB Disabling textboxes after button is pressed.

is there a way to disable a textbox in code only when an even is triggered?
for example, a textbox wants user to enter an initial amount of money in a textbox. After the calculate button is clicked, make the textbox not editable.
im a beginner and my textbook doesnt mention it.
To disable textbox on button click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Enabled = False
End Sub
To make it Read Only
TextBox1.ReadOnly = True
The difference between Enabled and ReadOnly is :
Readonly allows the user to set focus to and select and copy the text but not modify it.
A disabled TextBox does not allow any interaction whatsoever.
Use ReadOnly when you have data that you want the user to see and copy, but not modify. Use a disabled textbox, when the data you are displaying is not applicable in for the current state of a dialog or window.

VB 2012 TextBox.Clear() not working

I'm working on an encryption program and it uses a "PIN" to calculate some stuff for the encryption. I have a textbox where the user can insert the "PIN". I'd like to prevent people from entering anything but numbers. I added this on the KeyPress event:
If Not Char.IsControl(e.KeyChar) Then
If Not Char.IsNumber(e.KeyChar) Then
MsgBox("Invalid character", , "WARNING!")
TextBox3.Clear()
End If
End If
It shows the msgbox and it doesn't write to the textbox until i close th emsgbox. The typed character appears in the textbox. When I write another one it works the same as before, but it only replaces the last character instead of writing another one. Is there something I'm missing because that looks like a bug to me?
Set the ES_NUMBER windows style for your TextBox:
Public Class Form1
Public Const GWL_STYLE As Integer = (-16)
Public Const ES_NUMBER As Integer = &H2000
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal handle As IntPtr, ByVal nIndex As Integer) As Integer
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal handle As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Public Sub SetNumbersOnlyTextBox(ByVal TB As TextBox)
SetWindowLong(TB.Handle, GWL_STYLE, GetWindowLong(TB.Handle, GWL_STYLE) Or ES_NUMBER)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SetNumbersOnlyTextBox(TextBox3)
End Sub
End Class
It shows the msgbox and it doesn't write to the textbox until i close th emsgbox.
Yes, that's what modal dialogs do. They block the caller from updates until closed. That's the point; the user cannot interact with the parent until they clear the modal child.
Why not simply clear the textbox first? Better yet; don't show an annoying dialog at all. Simply disallow the user from entering invalid characters by setting e.Handled to true. However, it's a bit trickier than it sounds as you need to allow for the backspace and delete keys, disable pasting, etc.
Here's an example of a NumericTextbox: http://msdn.microsoft.com/en-us/library/ms229644(v=vs.80).aspx
You just need to set the Handled property to true instead of clear:
e.Handled = True
As MarkPM notes above, if its a key you don't want you can set e.handle=true (as you intercept the key on the keypress event) to have the system eat it.
Along with this, in stead of a pop-up, you can have a label on the form that says "Only numbers can be entered here" or something like that. Set it up so that the color of the text is red. Also set it up so the label is not normally visible.
Finally, also in the keypress event, beyond setting e.handle=true for unwanted keys, when an unwanted key comes along make the label that says "Only numbers can be entered here" visible - you can also set up a timed event to turn the label's visibility off after a few seconds. You can also throw a Beep() into the mix if you like :-)
This is less invasive then a pop-up and moves things along nicely for the user.