VB.net, Being able to select text in a richtextbox without losing focus? - vb.net

VB.net .. Currently I have two controls, one richtextbox and a textbox. The RTB is readonly, and also HideSelection is set to false.
The textbox is generic, it allows for input (to send data).
I want to be able to select things in the richtextbox without losing focus in the textbox. There is a client called 'mushclient' that does this, and it works pretty well. The text is still selected, but it doesn't lose the focus on the chatbar to type in.
I don't exactly know however how to prevent 'focus' though. At the moment it breaks flow when you are in game but want to copy something, you'll have to click the textbox again to start typing again. I understand I could setfocus after clicking the RTB, but this feels overall a bit odd. I was wondering if there is a more elegant solution.
Thanks!

This seems to work well for me. The TextBox does loose focus, but as soon as the Mouse_UP event fires, the selected text is copied to the clipboard and focus is sent back to the text box.
Public Class Form1
Dim LostFocusControl As Control
Private Sub RichTextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseUp
If RichTextBox1.SelectedText.Length > 0 Then
Clipboard.SetText(RichTextBox1.SelectedText)
End If
If Not IsNothing(LostFocusControl) Then
LostFocusControl.Focus()
End If
End Sub
Private Sub ControlLostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
LostFocusControl = Sender
End Sub
End Class
The code is a bit longer than it could be, but this makes it easier if later on you want to change the control that focus is returned to. To change the control that you want to return focus to, just change the name of the control that the handler is subscribed to e.g
Change
Private Sub ControlLostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
To
Private Sub ControlLostFocus(sender As Object, e As EventArgs) Handles Listbox1.LostFocus
or whatever the name of the control is that you want to return focus to.

Related

vb.net Add Custom validation Events in textbox

i am making vb.net application in which there are 10 textbox in which i am changing background color when it got focus and lost focus. and adding validation number or character only. is there any way i can set or add custom code that every textbox added in form change color on got focus and lost focus and can assign textbox validation number only, alphanumeric. i don't want to add code on every event on keypress , gotfocus and lostfoucs. i just want to set it as default property of text box
here is my code
Private Sub txtProductDescc_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDescc.GotFocus
txtProductDescc.BackColor = interfaceDesign.gotFocusTxtColor
End Sub
Private Sub txtProductDescc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDescc.LostFocus
txtProductDescc.BackColor = interfaceDesign.lostFocusTxtColor
End Sub
One way, which I am using myself often, is to make an array of textboxes. This way they all share the same "code" for every event and you can select them by index if you need to address a specific item.
Updated
You can also capture key events on the main form:
C# capture main form keyboard events
How do I capture Keys.F1 regardless of the focused control on a form?

Single code for Tab Key functionality using Enter Key in Vb.Net for a Windows Application

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including
Detecting Enter keypress on VB.NET and
Tab Key Functionality Using Enter Key in VB.Net
BUT all I get is either to write code for each textbox or for every individual form.
Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks
There are many ways to implement this, one of those is to create a custom textbox User Control
Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control
Give a name to your control ex. "tabedtextbox"
Add a textbox control to your User Control
Place the code in the keypress event of textbox1 of the UserControl
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
Compile once in order to update the whole project with the new user control.
Goto to your form and add your new User Control, you can locate it at the Toolbox panel.
Run your program and you will see the behavior of TAB when you press enter on each textbox.
If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button,
you can use the acceptbutton property.
One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.
Try this code:
Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
'.... actions to perfom
End Sub
Screen

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

How to write a single click event in Visual Basic?

I have created a form that allow users to close a form by clicking anywhere on the enlarged picture form (There are 3 objects to consider) and go back to the other form, which is called: "frmPhone". There's an actual picture on the form: "frmPhonePics" which is what I'm using to accomplish what I'm trying to do (was unable to insert an image on here. Sorry.) What I want to do is write a single click event to close the large picture form to allow the user to close it absolutely anywhere in the form, but I don't know how to do that. Here's the code I have so far:
Private Sub frmPhonePics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
frmPhone.Show()
Me.Hide()
End Sub
It sounds as though you have a picture on your frmPhonePics form. If you double click that (from the VBA editor), you should be taken to the code - for example, you might see
Private Sub Image1_Click()
End Sub
Now all you have to do is add your code there:
Private Sub Image1_Click()
Me.Hide
frmPhone.Show()
End Sub
Note - the order matters, since frmPhone.Show() will "hijack" the code flow until it's dismissed, and in your code Me.Hide will not execute (so the form will not close) until frmPhone has been dismissed.
You can map the click handler for various object to one thing, if that is what you are asking:
Private Sub frmPhonePics_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Click, Handles picLarge.Click, Handles otherThing.Click
frmPhone.Show()
Me.Hide() ' should be Me.Close?
End Sub
Not sure why it is MyBase.Click in your code instead of Me.Click. Is this a subclassed form?
I'd strongly suggest using a DoubleClick instead of a single Click. The chances of an errant click doing the wrong thing is very great.
The easiest way is right from the designer. Write the sub routine, then for each control, in the properties window, click the events icon(thunderbolt) and assign the sub routine to the double-click event.
Alternatively, dispense with the Handles clause completely and use a series of Addhandler statements in the Load event handler. If you put a unique string in the names of the controls or if it's all the controls, you can iterate through the controls and use one addhandler statement for all of them
For Each c As Control In Me.Controls
AddHandler c.DoubleClick, AddressOf Ctrl_DoubleClick
Next
Private Sub Ctrl_DoubleClick(sender As Object, e As EventArgs)
'Do stuff
End Sub

Windows Form Cancel Button Not Working

I have a Visual Studio, Visual Basic form that includes an OK button and a Cancel button.
What I want to do is have the OK button save the options that the user chooses and of course the Cancel button discarding them and returning them to their previous values.
But what I'm noticing is that as I'm debugging the form, the values are being saved regardless of whichever button I'm choosing. On the form's properties, I have declared that indeed the CancelBtn is the CancelBtn and that the OK button is the OK button, but the values are still being saved regardless.
Is there a better way to do what I would like this form to do?
EDIT:
Here's the code so far for the two buttons, both are being set to close the window. AcceptOption should save the values and CancelOption should just close the form. I'm sorry if this isn't done well but the FAQ's that I found only mention changing the properties of each button and nothing about the code.:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles AcceptOptionBtn.Click
' Save the Options
Me.Close()
' Close the form
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles CancelOptionBtn.Click
' Close the form
Me.Close()
End Sub
Don't change "the values" until the user clicks the Save button.
The form should be preloaded with a copy of the values you would like to update.
The Cancel button should just close the form.
The Save button should cause "the values", not the forms copy, to be updated.
EDIT:-
In regard to this question, there is nothing wrong with the code you have posted. Are the right handlers being called for the right button clicks? Are the form's AcceptButton and CancelButton properties set to the right buttons?
What data are your editing controls bound to, if at all?
There's nothing magical about OK and Cancel buttons. They're just... buttons. If you save your data every time a change is made, the Cancel button won't magically "unsave" them. Though if you save changes in the OK button's Click event handler, then clicking the Cancel button obviously won't save your changes. To help you further we'd need to know how you save your data.
Edit:
From looking at your code, I think you're passing data directly to your form, without performing a copy of your objects. Therefore if you modify this data, it will also be changed in the parent form. By working with a copy of your data in this form, any changes which aren't saved will be correctly discarded.
Your event handler for the cancel button should look like this:
Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Your event handler for the OK button should look like this:
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
SaveSettings 'call a routine to save the settings the user has entered
Me.Close()
End Sub
It is as simple as that!
If you open your form like
myForm.showdialog()
you don't have to define the handler for the close button click event, it is automatically handled; just set the 'DialogResult' property for the button
btnCancel.DialogResult = DialogResult.Cancel
Also if you want to close the form when ESC is pressed then set the 'CancelButton' property for the form:
myForm.CancelButton = btnCancel
On the other hand if you open the form like
myForm.Show()
you do need to specify the action(s) to take on the close button click event as indicated here, ie:
Private Sub BtnCancelClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
Close()
End Sub
I was having the same issues. As soon as I use My.Settings.Blabla = Blabla.value, it gets saved even if I haven't used My.Settings.Save() which makes My.Settings.Save() completely pointless as far as I can tell.
I ended up taking up Jordell's advice: Don't change "the values" until the user clicks the Save button but it wasn't too clear for me how to go about it.
I ended up using temporary variables in all my settings subs instead of the user My.Settings.UserConfigs. Only when I was in the OK sub did I call
My.Settings.UserConfigSetting = temporary_UserCofigValue
Here is an example from the code I was working on:
Private Sub btnOptionsThemeLB_Back_Update_Click(sender As System.Object, e As System.EventArgs) Handles btnOptionsThemeLB_Back_Update.Click
If (tempOptionsThemeLB_Back = Nothing) Then
tempOptionsThemeLB_Back = Me.btnOptionsThemeLB_Back.BackColor
End If
tempOptionsThemeLB_Back = RGBToColor(txtbOptionsThemeLB_Back_Red.Text, txtbOptionsThemeLB_Back_Green.Text, txtbOptionsThemeLB_Back_Blue.Text, tempOptionsThemeLB_Back)
Me.btnOptionsThemeLB_Back.BackColor = tempOptionsThemeLB_Back
End Sub
And only withing the Ok sub did I call My.Settings.
'Theme Section
My.Settings.colorBtnBack = tempOptionsThemeLB_Back