VB.NET How to insert text at cursor position in a different window? - vb.net

I have a small application that displays a listbox under the cursor position when the user uses a shortcut key.
When the user double clicks a selection from the listbox I want to insert that selected text at the curser position of that opened window.
Example: user has microsoft word open. He/she uses a shortcut key that displays a listbox just under the cursor position. The listbox has a collection of text. When the user double clicks a selection that selected text is inserted at the cursor position.
I tried the following:
Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Text.Insert(Cursor.Position, ListBox1.SelectedItem)
End Sub
But that doesn't work.
Any help will be sincerely appreciated.

The best (most generic) approaches will be to trick the application into thinking you have entered some text. For example:
Send keypress windows messages for all of the characters you wish to "type" to the target window (e.g. with WM_KEYDOWN or WM_CHAR type messages. Some experimentaiton may be needed to find the approach that works best).
Copy the text onto the clipboard and send a single ctrl+V keypress message to the application. (This will overrite the clipboard and may not work in apps that don't support that key shortcut though)
If you know the specfic application (e.g. MS Word) then you may be able to use application-specific automation (OLE, etc) interfaces to insert text.

Related

Visual Basic Can't Type in Listbox

I am using Visual Studio and working in a Windows Form Application. I have added a Listbox on my form. The problem I am having is that when I run my application I can't add any text or even select the Listbox to put my cursor in.
You have to add values or action to listbox before run it,if you run it without value or action absolutely that will give you nothing.
To add a value in listbox,right click on listbox then select "edit items...".
Or you can make a action like : add text to listbox from textbox,do looping,or another...
From the design window from theToolbox add a TextBox and a Button to your form. (The ListBox is already there, right?) Enter the following code to your form in the code window.
'This is the code that runs when the users clicks the button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This is the code that copies the text in the text box to the list box
ListBox1.Items.Add(TextBox1.Text)
'This code clears the text in the text box
TextBox1.Text = ""
End Sub
The text in the gray window that appears in light gray and is preceded with an apostrophe are comments; not part of the code.
Now run the program and type in the text box. Click the button and the text you typed will appear in the list box. Now that you have had this brief introduction to Visual Basic . NET try searching for a beginners tutorial. Come back when you have some code that you wrote but doesn't turn out as you thought.

Overload Clipboard.SetText VB.net 3.5 (or how to copy 3-4 text box conents at same time)

I have a form where I require user input and when the user presses the "generate" key it will copy all the data from the relevant text boxes to the clipboard for them to enter the data in an external application.
At the moment i can only find out how to get the function to copy one text box at a time, and not string them into a sentence.
Private Sub Notes_Click(sender As Object, e As EventArgs) Handles Notes.Click
My.Computer.Clipboard.SetText(TextBox1.Text, TextBox6.Text, TextBox5.Text, TextDataFormat.Text)
The problem is that the function can no be overloaded. Can someone please confirm the function i can use (or provide me a sample code) for how to copy all text box contents to the clipboard?
This is ALL on one form
The clipboard only accepts one text value at a time, so it's pretty clear why your overload won't work. If you need multiple values, concatenate them; if you need to keep them separately identifiable, put a separator character between them and parse them back out on the receiving end.
My.Computer.SetText(TextBox1.Text + TextBox2.Text + TextBox3.Text +...)

How to be able to select the text but not the textbox

This seems like it would be pretty simple but I can't figure out a way to do it and there isn't a solution anywhere on the internet that I can find leading me to believe it isn't possible using this language.
I have a simple program, you give it input, hit a button and it outputs into a textbox.
I had to make it a textbox and not a label because you can't highlight the text in a label, which is necessary for the user to be able to copy the text. However, as a textbox, the user can select the box. I've set it to ReadOnly, so they can't type in it, but the blinking text cursor is still there if the box is clicked on and it looks really bad. I've also tried setting Enabled to False, but then the text can't be highlighted.
The textbox itself shouldn't be able to be selected, just the text inside the textbox.
How do I do such a thing?
P/Invoke is the only method I know of.
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function HideCaret(hWnd As IntPtr) As Boolean
End Function
Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
HideCaret(TextBox1.Handle)
End Sub
Here is documentation on the user32.dll HideCaret function:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648403(v=vs.85).aspx

How to start with blank text boxes when the form loads

I have a unit converter written in Visual Basic, using Visual Studio 2012. My frmMain_Load event handler is posted below. I am using text boxes, and combo boxes. I have textchanged, and SelectedIndexChanged events set up for both sides of the converter. My problem is that when the form loads it triggers these events, therefore, causing the program to convert empty strings that returns a zero in the text box at the start of the program. I would rather have blank text boxes. Any help or opinions would be greatly appreciated on this matter. Thanks in advance.
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Show() ' Creates the onscreen controls so the focus can be set
PopCombo() ' This procedure populates the combo boxes
cboUnitType.SelectedIndex = 0 ' Sets the default selection on the main combo box
txtUnit1.Focus() ' Sets the focus on the first text box
End Sub
Instead of using SelectedIndexChanged, you should use SelectionChangeCommitted in order to respond only if the user is really the one who changed the selections.
And about the textbox event, I would go with Mr. CoDeXeR's option.

VB.net strange textbox behavior

A customer of mine is having a problem with a text box. When he clicks on the string in the text box the cursor always jumps to the end of the string. This is a standard VB.net 2005 text box with multi-line true. On my development machine it works correctly. I click in the middle of a string and can edit where I click. Can anyone suggest what is wrong?
He has run the program both under terminal server and locally on his lap top and has the same problem.
TIA,
John
Is it possible to observe the user? For example, the user might be pressing shift-tab when they are past the text-box, and refering to that as "clicking" the text box.
You can always force behavior like:
Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = 0
End Sub