How to automatically append to RichTextBox in Visual Basic - vb.net

I tried Google for an answer but didn't find what I was looking for.
I created a very simple app that lets users use a barcode scanner to scan barcodes into a text file. Everything works fine, I just want to simplify it a bit more.
Originally, I have a textbox1.text field where the scanned barcode would appear, then the user had to click the 'Add' button (Button1.Click) I placed next to the textbox field to append the barcode serial into a RichTextBox right below. Well the user found it tedious to have to click the 'Add' button every time they scanned an individual barcode.
My Question
Is there a way I can have the text in textbox1.text automatically append to RichTextBox as soon as a barcode is scanned? I want to eliminate having to click the 'Add' button.
Here is my current code (code for the Button1.Click button):
Dim scanData As String = TextBox1.Text
RichTextBox1.AppendText(scanData + " " + Format(TimeOfDay, "HH:mm:ss") + vbNewLine)
TextBox1.Clear()
TextBox1.Focus()

First off, I would make sure the user cannot input text themselves by disabling the TextBox control (TextBox1.Enabled=False), then add your code to the TextChanged event:
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Dim scanData As String = TextBox1.Text
RichTextBox1.AppendText(scanData + " " + Format(TimeOfDay, "HH:mm:ss") + vbNewLine)
TextBox1.Clear()
TextBox1.Focus()
End Sub
Before appending to the RTB, I would check to make sure the BarCode is valid.

Related

how to enumerate listbox?

Hi I'm new to Visual Studio and I am in need of a help in Visual Studio on a Windows Form App. also I don't really know if this is vb.net
I would like to make an application where if a user inputs something in the TextBox, it is presented in the ListBox whilst having some sort of an enumeration.
like for example.
If I type "Wow amazing" in the TextBox and confirm it. Then type another text like "I love you" in the TextBox and confirm it again
It should show up in the Listbox as "1. Wow Amazing" and "2. I love you".
Here's my code. I am not able to get it right and I don't really know how. I tried doing the for Loops and Do While but it would just duplicate the texts or am I doing something wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 0
ListBox1.Items.Add(i + 1 & ". " & TextBox1.Text)
End Sub
You are so close!
On every button click you need to:
Take number of elements in list, to determine the number.
Insert text concatenated with number.
So, you should use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Step 1
Dim i As Integer = ListBox1.Items.Count + 1
'Step 2
ListBox1.Items.Add(i & ". " & TextBox1.Text)
End Sub

mandatory field in VB and save Button

if somebody can help me in VB coding to show required field to be mandatory when I click save Button and the function of saving should not be work till all required field to be filled
(please visual basic codes not other )
You can use a simple If function that occurs when the button is pressed.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
Msgbox("A field is missing")
Exit Sub 'To avoid saving, escape the procedure
Else
' code to save goes here
End If
End Sub
Here, I assume that TextBox1 and TextBox2 are the fields you want and Button1 is the save button.

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

Customer Info Form sent to .txt file on desktop via button click

Good evening,
I am attempting to finish this last little bit of my VB.net project for school. It's the good ol' pizza ordering system. After the welcome screen the user is prompted to enter first name/last name/email and then click submit (if they leave any box empty it will pop up w/ msgbox). I need to send the information that is input into textbox1, textbox2, and textbox3 into a .txt file on the desktop if possible.
Here is the code
Public Class CustomerInfo
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
MsgBox("Please Enter Your Information")
' Forces user to input something in to all three boxes before being allowed to proceed
'Prompts user to complete form with a message box.
'Once information is complete, clicking the submit button will close this form and open the Selection Form.
Dim desktopPath As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim fileName As String = "customer.txt"
Dim fullPath As String = IO.Path.Combine(desktopPath, fileName)
IO.File.AppendAllLines(fullPath, {"textbox1.text & textbox2.text & textbox3.text"})
else
Selection.Show()
Me.Hide()
End If
End Sub
I attempted to upload a screenshot of the windows form, but am new to the forum. It's very basic, text boxes 1, 2, and 3 with a "Submit" button below them.
Thanks in advance for any assistance provided.
Kyle

keep track of richtext box text?

I'm new to the VB applications.My basic background is C.I just installed VB and learning stuff from google and microsoft help center. and I'm having fun with the things I'm doing but I got stucked up at one point and thats at richtext box.Is there any way to keep track of Rich textbox text in VB? So that I can append some text when user hits new line (i.e enter ) and do some task when he hits backspace. How do i keep track of richtextbox.?
I found
stringer = RichTextBox1.Lines(0) to read lines
& vbNewLine for new line
How do i read that user hit the new line character or backspace in vb rich textbox? because as far in C i used to do like these
if a = 13; \\ ascii for new line and 8 for backspace
I just want to do some task when user hits new line but I am unable to figure it out what the condition to be made.and Any good links for vb and documents on VB or on its windows application would be appreciated Too. Thank you in advance
You will want to link into the KeyDown event for the RichTextBox. Within this event you can modify the Text of the current line. Sample code for adding text to a line is:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Dim textToAdd As String = " ** text to add **"
'Fire when the Enter key is pressed
If e.KeyCode = Keys.Enter Then
'Get the current cursor position
Dim cursorPos As Integer = RichTextBox1.SelectionStart
'Get the current line index
Dim index As Integer = RichTextBox1.GetLineFromCharIndex(cursorPos)
'Load all the lines into a string array
'This has to be done since editing via RichTextBox1.Lines(index) = "" doesn't always work
Dim lines() As String = RichTextBox1.Lines
'Add the text to the correct line
lines(index) &= textToAdd
'Assign the text back to the RichTextBox
RichTextBox1.Lines = lines
'Move the cursor to the correct position
RichTextBox1.SelectionStart = cursorPos + textToAdd.Length
End If
End Sub
All you need to do is check if the Enter or BackSpace key was pressed, like this:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Select Case e.KeyCode
Case Keys.Enter
'Do stuff when Enter was pressed
Case Keys.Back
'Do stuff when BackSpace was pressed
End Select
End Sub
Note that Select Case is the same as the switch in C.