VB.NET add text to the front of text - vb.net

I am trying to add text to current text that is in a textbox using a checkbox. Once the checkbox is checked, it will "add" the text to the textbox before the text already in the textbox.
example:
if the textbox said "Jake", it would say "Hello Jake" once the checkbox was checked.
EDIT: sorry for the quick question. I'm in a hurry. But the only method I could think of was concatenating and appending text. As far as I know append() adds only to the end, and concatening isn't the logical approach. I don't have a coded example because I dont even know how to approach this issue. thanks.
It's getting a thumbs down because its so simple, but not. I'm using multiple checkboxes. So one needs to be in the very front, one in the center, etc. Each checkbox injects text into the textbox a certain way. I can do this with nested if statements, but then we got a mess.

Try using this code :
if chbHello.Checked then
txtName.Text = chbHello.Text + " " + txtName.Text
Note : if the checkbox is the trigger put this code in the checkbox.
All it does is see if the checkbox was checked or not.
When its checked just concatenate the text of the textbox and the checkbox with the
checkbox first.

Related

Insert/replace string in existing text form field in Word (VBA)

So I'm trying to create a conditional dropdown in Word. I've used a Legacy Drop-Down Form Field with multiple options.
What I want to happen is when one of the options is selected from the dropdown (and then I guess you have to Tab to enter it...), the Legacy Text Form Field below, that has a simple default text, should populate with a new string from the case statement.
Things that I've already done/got working:
the drop down is working, and on exit it runs a macro
case statement is written out
the result of the case statement is in a variable (string type), let's call it StringVar
can pull the text form field's text (default text) with ActiveDocument.FormFields("TextBox").Result
What I figure out, however, is how to replace the default text in the already existing Text Form Field with the case statement's string variable text.
I've tried
ActiveDocument.FormFields("TextBox").Result = StringVar
but it doesn't change anything inside the text form field. The default text is still there.
So I can answer my own question after pondering and being hit with an "oh, duh" moment.
I had unprotected the document (ActiveDocument.Unprotect Password:="") in order to do this:
ActiveDocument.Content.InsertAfter Text:=("This is my text")
because I was wondering if I was grabbing the right string.
Turns out,
ActiveDocument.FormFields("TextBox").Result = StringVar
DOES work. BUT the file has to be protected (filling in forms) for it to replace the string in the text form field. Otherwise, nothing shows up even though the result had indeed been updated. Fancy that.

Automatically send barcode scanner input to textbox VB.Net / Winforms

I've reviewed dozens of options/solutions on this and I just can't get it to work.
Simply put, I have a VB.Net Winform that has a textbox where a user can manually type in text or they can use a USB connected barcode scanner (that simulates a keyboard) to capture a UPC.
What I'm trying to do is get the barcode input to get entered into the textbox regardless of which control has the current focus.
I have set the KeyPreview property of the form to True.
I then added some code to the frmMain_Keypress event as follows:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
End If
Very simple...and it works, sort of...
If txtSearch already has the focus, the entire barcode/UPC gets entered into the text box.
However, if another control has the focus, every character of the barcode/UPC EXCEPT THE FIRST CHARACTER gets entered into the text box. It always strips off the first character.
I placed some debug statements in the above code to see if the initial character was being read at all and it is being read...just not sent to the text box.
I've seen so many other REALLY complicated solutions to barcode scanning and it seems like I'm really close with something really simple, but, obviously it won't work if it strips the leading character.
Hopefully I'm missing something very obvious.
Change the code in your KeyPress event to:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
txtSearch.Text = e.KeyChar.ToString
txtSearch.SelectionStart = txtSearch.Text.Length
e.Handled = True
End If
That way you capture the first key that comes in.

Can a combobox present more then one column on its textbox part?

I have a two column list in my combobox and when I choose one of the items in it using the drop down list, it stores (what I see in the textbox part of the combobox) only the value I selected (wether it's the one on the right or left column according to boundcolumn)
My question is: Is there a way to store(or present-that is my goal) in the textbox part of the combobox, both of the columns of one row selected?
For example: [column1] Daniel [column2] Smith. And in the textbox I want: Daniel Smith (and not just Daniel, or Smith on they're own)
What you describe is theoretically possible, but with restrictions.
Even when you didn't ask for that, here comes my idea:
Like described here you can change your ComboBox's text without changing its value. This allows you to 'store' the underlying value while displaying both columns in one.
Listen on the SelectedIndexChanged Event and change the text property as follows:
Sub ComboBox1_SelectedIndexChanged()
ComboBox1.Text = ComboBox1.Column(0) & "-" & ComboBox1.Column(1)
End Sub
(This is only a basic example.) Can't test it right now, but in .Net you can use CType to explicitly convert the sender argument to a ComboBox variable and access it this way.
The Boundcolumn property can't be changed to multiple values. You can try to use VbTab as a delimiter in the text field but i'm not sure how it will appear.
Edit:
Don't forget the default values. I think your Text field should show both columns before the user clicked on the list the first time, too.
You can set a combobox text field to use data from multiple columns but you may need to write some code to do it.
Try http://www.contextures.com/Excel-VBA-ComboBox-Lists.html

vb.net designing questions

i have created a dynamic table through vb.net, so htmltable, htmltablerow, htmltablecell.
I have added all style and properties in there. so eg.
dim td as htmltablecell
td.style.add("width","100px")
td.style.add("color","blue")
now what i want to do is add the tag to each row. this is because the first td of every row is a checkbox. and then textboxes follow. So when anyone clicks on the textbox, i want to checkbox to get checked.
Any idea how I can do this?
Your question is near incomprehensible, consider revising?
td.style.add("color","blue") now what i want to do is add the tag to each row
Since your table is dynamic, to add more styles and properties onto htmltablerow, you would do just as you did with your htmltablecells.
So when anyone clicks on the textbox, i want to checkbox to get checked. Any idea how I can do this?
You're looking for what is called the on_click event for the textbox. Programatically, when this event is triggered, you check your checkbox. Here's an example with a on_click in a textbox.

Text input issues with Visual Basic

Ok, I'm writing a program in vb. It's simply a text editor. However i've run into a little snag. My program has options to click a button and text is inserted into the text box. I am using this line:
textbox.AppendText (sometext)
Now this code works great if i want the text to be added at the bottom of the page. So here's my question, is there any way to modify or maybe replace this code so that the text is inserted were the cursor is?
Try setting .SelectedText property or using .Paste(String).
Not through the textbox control itself, as far as I know. You will need to use the SelectionStart property to manually insert the text.
textBox1.Text = textBox1.Text.Substring(0, textBox1.SelectionStart) & "INSERTEDTEXT" & textBox1.Text.Substring(textBox1.SelectionStart)
This is assuming you want to insert the text and not just replace it.