Eliminate user input of single quote for every field in every form in a project - vb.net

using VB.net and Visual studio is there a way to globally (across all fields in all forms in a project) prevent a user from entering a single quote character into a field. I'm thinking some sort of modification of the keypress event, but I'm not sure how to go about it. Currently I am currently using the ADDHANDLER, ADDRESSOF technique to assign this special code to a particular field, and I guess I could do a loop over all my contained controls on a particular form and assign the keypress code, but if there is a way to do it once without repeating the code on each form that would be great. Any hints graciously accepted. Thanks.

What about a regex? Stripping or converting to escape character on submit?
Would it be possible to pass an array where TextBox1 is TextBox1,Textbox2, etc. Obviously you would need to edit the expression if you only need to strip the single quote. This for all non alphabet characters. I don't do much with vb.net but thought this might be helpful.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
StripNonAlphabetCharacters(TextBox1)
End Sub
Public Sub StripNonAlphabetCharacters(ByVal input As TextBox)
' pattern matches any character that is NOT A-Z (allows upper and lower case alphabets)
Dim rx As New Regex("[^a-zA-Z]")
If (rx.IsMatch(input.Text)) Then
Dim startPosition As Integer = input.SelectionStart - 1
input.Text = rx.Replace(input.Text, "")
input.SelectionStart = startPosition
End If
End Sub

The easiest solution is to create a derived TextBox and use that instead of the standard textbox.
Public Class NoQuoteTextBox
Inherits TextBox
Public Sub New()
AddHandler MyBase.KeyPress, AddressOf Handler
End Sub
Public Sub Handler(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If e.KeyChar = "'" Then
e.Handled = True
End If
End Sub
End Class
Build your project then the NoQuoteTextBox (or whatever you decide to call it) will appear in the Toolbox in the Designer:
Use this new control instead of the standard textbox and it will handle the single quote for you.

Related

User enters letter then button computes position from a string using VB

I am using indexOf but I cannot figure out where I made a mistake as the output gives a -1.
I realise I can copy the whole statement paragraph into the last line of the output but I was hoping it could pull it straight from the label.
Public Class Form1
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim statement As String
Dim letter As String
statement = CStr(lblStatement.Text)
letter = CStr(txtLetter.Text)
txtOutput.Text = CStr(lblStatement.Text).IndexOf("letter")
'txtOutput.Text = letter.ToUpper & " first occurs in position " & statement.IndexOf(statement) & "."
End Sub
End Class
Here is a picture of the form:
Update: Thanks to #ADyson and #Slugsie for taking the time to respond to my call for help. As #Slugsie noted it was indeed down to the lower case in my screenshot. I am now researching how to make it work without being case-sensitive.
Final code
Public Class Form1
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
txtOutput.Text = lblStatement.Text.IndexOf((txtLetter.Text).ToUpper)
End Sub
End Class
.IndexOf("letter")
is looking for the literal text letter within the data the user enters in the text. in VB.NET (and most other programming languages), anything enclosed within quote marks is treated as a fixed string of text, to be interpreted as-is rather than processed or treated as program code.
To make it look for the contents of the letter variable instead (which looks like what you were intending) simply remove the quote marks:
.IndexOf(letter)
As an aside, your whole code could be much reduced - primarily the use of CStr is unnecessary, because the Text properties of the textbox and label already return a string - meaning you don't need to use CStr convert it - and also because you're not making use of all the variables you declared either.
You could re-write your whole sample much more succinctly as:
Public Class Form1
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
txtOutput.Text = lblStatement.Text.IndexOf(txtLetter.Text)
End Sub
End Class

autocomplete combobox for vb.net word by word or with delimiters

I am trying to add word by word (and/or with delimiter support) autocomplete functionality to a combobox (or textbox) in a vb.net application. This is the desired behavior, stripped down to one delimiter for this question:
When the user types 'invoke XX ' in the box, a list of suggested words should pop up. The user should still be able to type through, continually filtering those words, until he/she selects one. For example:
'invoke 121 ' (should prompt a list of words such as Display, DVD, CD, still with typethrough)
I can handle much of the actual logic, but I'm pretty new to vb.net, and I'm unsure of the best way to trigger an autocomplete popup on a space (or period), word by word. (Regular combobox autocomplete doesn't support word by word.) This post looks helpful but I don't know how to convert the accepted answer's developer-express tools into regular .net components: autocomplete text box for .net with support for delimiter. Ideally it'd work a lot like intellisense.
Here is my attempt so far (just a form with a textbox), stripped down. I need help figuring out how to allow typing to filter through the contextmenustrip. I also wonder if I'd be better off with something other than a contextmenustrip. Thanks
Public Class Form1
Private WithEvents firstContextMenu As ContextMenuStrip
' Populate items for the contextmenustrip
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
firstContextMenu = New ContextMenuStrip()
firstContextMenu.Items.Add("Display")
firstContextMenu.Items.Add("DVD")
firstContextMenu.Items.Add("CD")
End Sub
' Bring up the contextmenustrip if typing a space, if after 'invoke XX '
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = " " And TextBox1.Text.StartsWith("invoke ", StringComparison.CurrentCultureIgnoreCase) And TextBox1.Text.Split().Length = 2 Then
TextBox1.Text = TextBox1.Text + " " ' Add a space
TextBox1.Select(TextBox1.Text.Length, 0) ' To put the cursor at the end of the textbox
Dim point As New System.Drawing.Point(TextBox1.GetPositionFromCharIndex(DirectCast(sender, TextBox).SelectionStart - 1).X, TextBox1.Height - 5)
firstContextMenu.Show(TextBox1, point, ToolStripDropDownDirection.BelowRight)
End If
End Sub
' Adds the clicked item to the textbox
Private Sub firstContextMenu_ItemClicked1(sender As Object, e As ToolStripItemClickedEventArgs) Handles firstContextMenu.ItemClicked
TextBox1.Text = TextBox1.Text + e.ClickedItem.ToString()
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
' Moves cursor to end of textbox if you 'escape' out of the contextmenustrip
Private Sub firstContextMenu_Closed(sender As Object, e As ToolStripDropDownClosedEventArgs) Handles firstContextMenu.Closed
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
End Class

How to Dynamically Update Label with TextBox As Input Changes

I have a spelling application that I am building in VB.Net, where I have a textbox receiving simple input (spelling words), and a label which will show the output. What I want to accomplish is when I enter something in the textbox, I can see it in my label - as I am typing into the textbox.
I will admit that I don't know what I'm doing, as I've never tried this before, so I don't know to begin in terms of setting up what I need to do. I know that I'll need some variable to hold my String input, and will probably need some type of loop, but beyond that, I am lost. The only other example is in C#, and doesn't help me any.
Can anyone give me a simple model to work off of, so I can put the approach into memory? For now, all I have is code stub from my TextChanged event handler:
Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
'Set variables to hold values.
Dim someText As String
'Connect the label and textbox.
lblShowInput.Text = txtWordInput.Text
'Process loop to populate the label from textbox input.
for '(This is where I am lost on the approach)
End Sub
I know that I'll need some variable to hold my String input, and will
probably need some type of loop
I don't think you'll need a loop, or the variable to hold the value. You almost have it:
Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
'Connect the label and textbox.
lblShowInput.Text = txtSpell.Text
End Sub
In the code you provided, you are referencing an object named txtWordInput inside your txtSpell text changed event handler. If you are entering the text in the txtWordInput input, you'll want to handle this in the txtWordInput textChanged event handler:
Private Sub txtWordInput_TextChanged(sender As Object, e As EventArgs) Handles txtWordInput.TextChanged
'Connect the label and textbox.
lblShowInput.Text = txtWordInput.Text
End Sub
Follow-up:
The TextChanged event is the correct event for this.
In your code, you are assigning lblShowInput.Text to txtWordInput.Text, but in the txtSpell TextChanged event handler.
You want to be in the TextChanged event handler for whatever TextBox you would like to use to update the label, as the text is changing.
To give a better example, I have created a simple Winforms VB application that has only a textbox named InputTextBox and a label named Output Label.
The Form:
The Code:
Public Class Form1
Private Sub InputTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles InputTextBox.TextChanged
OutputLabel.Text = InputTextBox.Text
End Sub
End Class
Explanation:
InputTextBox_TextChanged is the method name generated by Visual Studio for our event handler
Handles InputTextBox.TextChanged ties the method to an actual event it is handling.
When the InputTextBox text property is changed (typically by user input), whatever we have in our InputTextBox_TextChanged Sub will execute. In this case, I am assigning the Text of OutputLabel to the Text of the InputTextBox
Output:
Resources:
I've uploaded this simple demo to GitHub if you'd like a closer look.
Take a look at the TextChanged documentation

Toggle the masking and unmasking of a TextBox using a CheckBox

I have a TextBox, which has a CheckBox operation to mask the containing text. This works with the following code:
Private Sub CheckBox2_Checked(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox2.CheckedChanged
TextBox14.PasswordChar = "*"
End Sub
It works well, but I want to also be able to uncheck theCheckBox and then have the recognizable text return. How can I achieve this?
The docos actually state:
The character used to mask characters entered in a single-line TextBox
control. Set the value of this property to 0 (character value) if you
do not want the control to mask characters as they are typed. Equals 0
(character value) by default.
Found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.passwordchar(v=vs.110).aspx
In VB.NET, that would be easiest done by setting PasswordChar to vbNullChar.
You can do so by simply setting the PasswordChar property back to a null character, like this:
Private Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
TextBox14.PasswordChar = "*"c
Else
TextBox14.PasswordChar = ControlChars.NullChar
End If
End Sub
The CheckedChanged event occurs every time the Checked property changes. So, when the user unchecks the CheckBox, it will raise that event too, so you need to check to see whether or not the control is currently checked.
I found just toggling the password character wasn't enough. In my case I was masking a connection string. With the lack of spaces in my text I had an issue going back and forth. My text would be cut off and wasn't wrapping properly.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
Dim beforeText As String = TextBox1.Text
TextBox1.Text = ""
TextBox1.PasswordChar = IIf(CheckBox1.Checked, Convert.ToChar(0), "*"c)
TextBox1.Text = beforeText
End Sub
I imagine if you used a font like Console this would not be a problem as all character widths are constant.

Capture keypress / let user pick their own start/stop key

Currently I have the start-key for my vb.net application hardcoded like this:
GetAsyncKeyState(Keys.F2)
Where vb.net sais "F2 As System.Windows.Forms.Keys = 113" on mouse-over
But I want my users to be able to pick their own Key. If I make a drop-down box (combobox) and pre-define some choices in there (Like ESC or F3), all those choices are strings. How can I convert those strings to a System.Windows.Forms.Keys integer?
Also, I'd like it to also be possible to "capture" a single keypress. So they'd click the "capture" button, and the next key they hit will be saved as the start/stop button. But I wouldn't even know where to begin looking for that one.
If txtKeys.Text=="F3" Then
GetAsyncKeyState(Keys.F3)
End If
Try something like this:
Public Class Form1
Dim captureKey As Boolean
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
captureKey = True
End Sub
Private Sub Button1_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
If captureKey Then
Label1.Text = e.KeyValue.ToString
captureKey = False
End If
End Sub
End Class
I created a form with a label and a button for an example. e.KeyValue is an integer that I am converting to a string for display purposes. You also have the ability to capture other keydata. See this info on PreviewKeyDownEventArg
As for the first part of your question use a Select Case Statement to convert between your ComboBox Values and KeyData Values.