Preventing a textbox from capturing the MouseWheel event - vb.net

I have a textbox and when I scroll down my form with the mouse wheel and hit the textbox with the mouse, it stops scrolling.
Is there a way to avoid that ?

OK, If I understand you correctly, what you are searching is to keep mouse wheeling while passing on your TextBox. Is that correct ?
There is, I think, a way to achieve that. This code however has not been tested, so keep me informed if it worked.
Public Sub New()
InitializeComponents()
'Other inits here
AddHandler TextBox1.MouseWheel, AddressOf TBMouseWheel
End Sub
Private Sub TBMouseWheel(sender As Object, e As MouseEventArgs)
Me.OnMouseWheel(e)
End Sub
This way, when your textbox captures a MouseWheel event, it is passed on the form, which will handle it (I think). Sorry I don't have the opportunity to test it right now, but I believe this should do the trick.

If you just want the visual part, change the property "Cursor" of the textbox to "Arrow"

If you don't want a TextBox to be focused, you have a few solutions
Set the ReadOnly property to True
Nobody will be able to enter text in your TextBox, exept if you do it programatically. However, it will still be possible to click on it and if clicked you will see the blinking cursor (don't know the name in English). When the mover hovers it it will turn into a cursor.
This means this option allows the focus on the control, but it will not be possible to enter data.
Set the Enabled property to False
Again, it will not be possible to enter any data. Also, it will not be possible to click on it and the cursor will not change if you hover it.
This means this option disallows the focus on the control.
Put a Label instead
If nobody will ever be able to insert data in your TextBox, maybe it is better to put a Label there. If you choose option 1 or 2, it is because at some point you might allow user to change the text inside. But if it will only be modified by the program, Label is good enough.
Focus is never allowed on Labels.

Related

highlight / select text in numeric updown on enter

Using VB.net for a windows form application. I'm tired of always having to backspace the default '0' of a numeric updown control. What I would like to do is have the value selected automatically on enter, so that I can just type over it.
I've tried this:
Private Sub updown1_Enter(sender As Object, e As EventArgs) Handles updown1.Enter
Me.updown1.Select(0, updown1.Text.Length)
End Sub
I've used a break point to verify that it does indeed run, but it doesn't seem to do anything. What am I missing?
Your code is actually selecting the value as intended, it is just being undone by a mouse event almost instantaneously. When you click to enter a NumericUpDown, the events fire in the following order:
Enter
GotFocus
MouseDown
Click
MouseUp
As you probably know, in controls with text fields the native behavior places the cursor wherever you've clicked inside the textbox. This is what's causing your problem. You have selected the text at Enter, but then a bunch of mouse events come along and undo all your hard work. The obvious solution is to just use the MouseUp event since that's at the end of the list, but MouseUp will fire for anywhere you click inside the control so you'll have to decide if that behavior is acceptable for you.

Programming method

When I am programming in VB.net, I usually use a normal methodology (I think).
When I have a form, and some buttons placed on it, and some functions under this buttons, I implement the functionality of this buttons in the moment the button is pressed, for example:
I press a button, inside the button I use nested If..else structure to do something in order of a given condition. When the condition is true, I obtain a specific result.
I have heard (and I am not able to find information) about another methodology, where this if..else structure is not done each time you press the button, but the structure only executes one time, and not need to pass throught the if..else structure more times. Is like the program knows the solution without doing this structure all the times the button is pressed. Only needs to do it the first one.
I don't know if someone understand my question, but if someone knows about what I am talking and could give me some useful information, I would be very grateful!
i don't even know if this methodology exists, but I heard about it and I would like to investigate..
I am investigating about the performance of the programs I am creating..
You can add a member-level variable to detect if the button click handler code has been executed.
Public Class Form1
Private isInitialized As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do some stuff if this is the first time the button was clicked.
If Not isInitialized Then
'do some stuff...
isInitialized = True
End If
'Do some other stuff each time the button is pressed.
'do some stuff...
End Sub
End Class
Thanks for your answers!
Finally I understood how the methodology I've heard works.
It is quite simple, but my question was not well made, so now I know that my answer is too vague.
I didn't know, because of my few experience programming controls, that I can made my own control which I can use in my forms. With this, I can paint this control in the way I want. I am going to put an example that answer the problem I had:
A control that will be a clock, and this clock can be analogic or digital. Before I knew this, I had a button named 'Change Clock' which change the appareance of the clock between analogic and digital. And when I press that button, in the click event of the button, I start to paint the clock, either is analogic or digital.
Now I know that programming controls, I am able to made a control by creating a UserControl in my project, and inside the code of this new control I can use the paint event to paint the clock in order of a property. Something like this:
Private Sub AnalogClock_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Select Case m_contenido
Case formatoReloj.analogico
'Paint analogic clock
Case formatoReloj.digital
'Paint digital clock
End Select
End Sub
and when I use this control in my form, I only need to change the property of the clock and not to paint everything each time I want to change it. That means that is already painted in the code of the control and I have not to use code in my form to do it..
I hope you understand now what I wanted..

Keep cursor out of all Textboxes on form

I have a form with several buttons and TextBoxes. After the user clicks on a button, the text in one or more of the TextBoxes changes, and the cursor (edit: caret, not the mouse cursor) goes into a textbox. (This question helped, identifying that the TextBox with the lowest TabStop as being the one where it would land, and explaining how to avoid having text in that box selected when this happens.) But what I'd like to see is that the cursor (caret) stays out of all the TextBoxes unless the user decides he wants to put it there -- highlight & copy something, for instance. This wouldn't be common, but it could happen.
How can I make adjustments to the form or the TextBoxes so that the cursor (caret) doesn't show up in any textbox (so long as the user doesn't click inside a TextBox)?
Thanks!
If the code in your button's click event causes the focus to change to another control, you could update the last line of the click event, like this:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'Your code here
Button1.Focus() 'this last line of the sub sets the focus back to the button
End Sub
After some experimentation, it looks like setting the TabStop property of each TextBox to False will also prevent the cursor from moving into any of the TextBoxes on its own after a Refresh.
I suspect that sBarbacki's answer will work equally well, or probably better with a large number of controls that one wants to avoid getting the focus automatically.

How do I simulate a click event in VB Windows Forms

I just want to connect two arbitrary controls, so that if one is clicked, the other should act as though it's clicked - is this even remotely possible? it seems like it SHOULD be so easy, but the internet seems dry, unless I just don't know how to ask the question properly... I see a way to "click" a button control, but what if the target is not a button? - I don't know the name of any function that might be triggered by this control's click event, so I can't call it directly. I would guess there is some way of using Windows APIs, but I can't find anything that's nice, simple VB
Example
I click a Label control on the form. I want to handle that click event, run one line of code, then simulate a click event on an associated RadioButton control
Is this possible? How?
If you must, call (System.Windows.Forms.Controls.)Control.InvokeOnClick
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick%28v=vs.71%29.aspx
or even RadioButton.PerformClick
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.performclick.aspx
A better way would be to create a common Subroutine that would be called on click of either controls. This way clicking on controls will execute their own code which can differ, and some common code as well.
This is how you accomplish executing the same code regardless of which control event was fired.
Private Sub ClickMe()
'code to execute
End Sub
Private Sub label1_Click(...) ...
ClickMe()
End Sub
Private Sub rb_checked(...) ...
ClickMe()
End Sub

How can I limit a textchanged event for a textbox to keyboard input only?

Please allow me to explain what I have and what I am trying to achieve.
I have a textbox (called txtb1) and a button under it (called btn_browse) on a winform in a vb.net project.
When the user clicks the button a folder browser dialog appears. The user selects his desired folder and when he/she clicks 'ok' the dialog closes and the path of the folder selected appears in the textbox. I also want to store that value in a variable to be used somewhere else(the value will be copied to an xml file when the user clicks 'apply' on the form, but this has no effect nor is related to my problem).
To achieve that I have the following code:
Public myVar As String
Private Sub btn_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_browse.Click
Dim f As New FolderBrowserDialog
If f.ShowDialog() = DialogResult.OK Then
txtb1.Text = f.SelectedPath
End If
myVar = txtb1.text
f.Dispose()
End Sub
This part works with no problems.
Now, what if the user either:
1- decides to enter the path manually rather than use the browse button. or,
2- after using the browse button and selecting the folder they decide to manually change the location
In trying to solve this I added a textchanged event to the textbox as follows:
Private Sub txtb1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtb1.TextChanged
myVar = txtb1.Text
End Sub
However, this is not working. Apparently, and I don't know if this is relevant, when the user selects the desired folder using the browse button the textchanged event is also triggered. and when I click on the textbox (to give it focus) and press any keyboard key the application simply stops responding.
So my questions are: am I going about this the right way? if my logic is flawed, could someone point me to how usually such a thing could be achieved? is it possible to limit the triggering events to only keyboard input as a way around this? I tried the keydown and keypress events but I am getting the freeze.
Set the TextBox.ReadOnly property to true and then set the backcolor to white and forecolor to black to look like a normal textbox but they can't edit it.
Then you have no need to worry about handling any events from the textbox like u are doing.
I think your solution is pretty simple. Just treat the textbox as a File upload control in web forms. Make it readonly. Don't let the users to edit the text. This solves two problems:
The user will always a select a folder using a known mechanism (clicking on button and seleting folder)
No need to use any variable since you can always get the location from the textbox.
HTH
Why do you need to store this value in an additional variable? So long as the textbox is visible to the user, it contains the definitive value and could be accessed directly. So, in this case you would have clicking the "Apply" button read the value from the text box instead of the variable, thus avoiding this problem with events altogether.