How to be able to select the text but not the textbox - vb.net

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

Related

Method to tab between Text Boxes in excel?

Does anyone know of a way to press the 'Tab' key and move between text boxes in Excel (2010)? The VBA solution I've seen is way too manual, as you need to create code for the 'Tab' event on each text box.
For example, the following would need to be replicated for every TextBox in the workbook:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyTab Then TextBox2.Activate
End Sub
(The text boxes are NOT part of a VBA form. They are part of a regular worksheet.)
Thank you,
Don
First Go To Special, select Objects and OK.

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

I have a "bug/glitch" in my vb program need assistance

I have a program that replaces every letter of the alphabet with something new inside the same textbox by clicking a button then buy clicking the button it translates it back to original text.
However when i run this program, yes it does work as needed but not 100%
Because after i click the button to translate, (yes it shows translated)
If delete the translated text and type something new, it goes back to the old text.
(this does not happen if i click the button again, it shows the orignal text. But if i type something new it will translate it.)
Any suggestions?
This is a continuation of your other question. You will need to reset your Boolean variable when you type in your TextBox i.e.
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
bConverted = False
End Sub

How can I refer to a control from within a control's method (like the "me" for classes)?

How can I refer to the control while I am inside a control's method in VB.NET?
For example, I want in a textbox to show a message box with that textbox's text every time the text changes. The code would be something like:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
msgbox("The text is:"+ Me.text)
' ok the line above wont work i already know that, because "Me" refer to the form,
' not the control textbox1
' how i will refer to the textbox1's text???
' i dont want to use "textbox1.text" is there a way similar like the "Me" is for forms?
' because i want to copy-paste a code like this in a lot of controls and do not want to
' have to change in every copy the name to each control name
End Sub
I hope I made myself clear; my English needs some improvement :D
No, there's no keyword that allows you to do that. However, every event raised by a control passes in a sender parameter that you can use to determine which particular control raised that event.
Note that this parameter is always typed as a basic Object (because it can represent any possible control), so you'll need to downcast to a more specific control class if you need to access any of the unique members that it exposes. Since you're handling an event raised by a TextBox control, you know that the sender must be of type TextBox, so you can simply use DirectCast to handle the upcasting. You don't have to worry that an InvalidCastException will be thrown.
For instance, your above example would become:
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
Dim textBox As TextBox = DirectCast(sender, TextBox)
MessageBox.Show("The text is: " & textBox.Text)
End Sub
That being said, there are a couple of concerning things that jump out at me in your question:
Any time that your approach to solving a problem is "copy-pasting" code, you should stop, take a step back, and try to figure out if there's any better way to achieve your ultimate goal.
For example, if you need every textbox on your form to react in the same way whenever a particular event is raised, you should consider subclassing the existing TextBox control and consolidating all of your code in one place. Remember that you can inherit off of most of the standard controls to add custom functionality. This is often a far better solution than copying and pasting code to multiple places in your project. If you ever need to track down a bug or modify that functionality, you'll only have to change it one place in your code, rather than several. As a somewhat cheekier benefit, you'll be able to use Me to refer to that control when you're editing its subclass.
You should always prefer to concatenate (combine) strings using the & operator in VB.NET, rather than the + sign. Or perhaps even better, the String.Concat or String.Format methods.
There is no reason to use MsgBox in VB.NET, as opposed to MessageBox.Show. No, this won't improve performance of your application, but it's a good practice to get into for .NET languages.
The sender variable contains the TextBox instance you want to access. You only need to convert the sender to TextBox.

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

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.