vb.net: setting focus on nested custom controls - vb.net

I have a nested custom control that I need to set focus on. I have it all internally wired up to automatically set the focus when the form loads, but when it comes up on screen, the designated accept button for the parent form is in focus instead. Even when is disassociate the accept button it still does not set correctly. How can I ensure my desired control gets focus.

I think what you're asking is that you would like to make a textbox (or button, etc) on a user control have focus when the form loads. Try setting the ActiveControl of the user control to the textbox (or which ever control) and then calling focus on it. For example:
Private Sub myUserControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ActiveControl = myTextbox
myTextbox.Focus()
End Sub

Set the tab order. If your custom control is on the same level as buttons (has the same parent), make sure your control and any containter it is in has tab index 0.

Related

Keep track of Window being Last Focus

The VB.Net program I am creating dynamically created Panels within a TableLayoutPanel and adds form elements to them. I need to keep track of what the last of these Panels to have focus was, and am hitting a bit of a brick wall.
I have tried creating an event class for when the Panel has focus:
Private Sub Self_GotFocus(ByVal sender As Object, ByVal e As EventArgs) Handles Me.GotFocus
GlobalController.Focus_Target = Me.Name
End Sub
The classes for each Panel Inherit from Windows.Forms.FlowLayoutPanel, which I why I have the call being Me.GotFocus. Additionally, the GlobalController class is just a class meant to hold global variables for the program.
Now the issue I am having, is that this event only seems to trigger when I actually am deleting the panel. When the panel is created, if I click on it, or any of it's form elements, the event never gets triggered (I debugged the program with a breakpoint).
I can't exactly figure out why this only triggers when I go to delete the panel, and not at any other time. Is there another event I should be using instead of GotFocus?
Use .Enter event in your panel since GotFocus is related only to focused control (not it's parent), mostly when UICues is set.
See MSDN GotFocus

Set Focus to Form After Me.Show

I've come across a peculiar focusing issue. I have created the following "search" program:
It runs in the background.
When you double-tap the Ctrl key it becomes visible.
You can type in the textbox because the form has focus.
If the form loses focus (I click on my desktop, for example), it disappears after 3 seconds.
I double-tap the Ctrl key again, and again it becomes visible.
But this time, no matter what I try, the form is not focused and I cannot type in the textbox without first manually clicking on the form.
What's particularly interesting is that when I run this program in debug mode from Visual Studios, the program regains focus upon double-tapping Ctrl key and becoming visible, and I can immediately start typing in the text box. However, when I build this program and run it alone, the program appears but does not regain focus upon double-tapping Ctrl key, and therefore I cannot type in the text box until I manually click the form.
After Me.Show() I have tried:
Me.Focus()
Me.Validate()
Me.Select()
Textbox1.Select()
Textbox1.Focus()
The form is topmost and normally running in administrator, but the same problem arises regardless.
The issue can be recreated in a more simple manner. Create a form with
Button ("Button1")
TextBox
Two timers ("hideForm", "showForm") both with intervals of 1000
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
hideForm.Start()
sender.Enabled = False
End Sub
Private Sub hideForm_Tick(sender As Object, e As EventArgs) Handles hideForm.Tick
Me.Hide()
hideForm.Stop()
showForm.Start()
End Sub
Private Sub showForm_Tick(sender As Object, e As EventArgs) Handles showForm.Tick
showForm.Stop()
Me.Show()
Me.Activate()
End Sub
End Class
Click the button, and immediately click on a different window (so the form loses focus). Wait until the form is hidden and shown again. The textbox should have focus. Try typing.
If the program is run in debugging mode in Visual Studios, it works as expected. If you build the program and run it outside of VS, the form will reappear without focus, and you cannot type in the textbox without manually selecting the form.
Sorry for the long-winded explanation. It's a difficult issue to properly describe.
Try the form event handler Activate. Inside that method, you can use setFocus to gain focus for that particular Text Box. I know this answer is too late. But hope this helps someone.
Private Sub Form_Activate()
TextBox1.SetFocus
End Sub
Try an event handler for Form_Activate, and within that handler pass the focus to your textbox.
Instead of Focus, you can also try TextBox1.Select. This SO link provides some additional information and something about the difference between Focus and Select.
Select the Textbox you want to assign a focus to in the Design View Window.
Under the Properties window, set the TabIndex to 0 (zero).
I didn't even have to use the TextBox1.Focus() command. It still bothers me that the TabIndex overrides the Focus command.
What I tried (and worked for me), was to set the Focus() of the Textbox in the event handler Shown() [VB]:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Me.Textbox1.Focus()
End Sub
Note: the Select() method just didn't do the job. I hope this helps anyone else that comes with this same issue.

Prevent ListView from bringing old form to front

I have a form (we'll call it Form A) that contains a ListView control. When I select an item in the ListView, the event handler creates and shows a Form B.
I want Form B to appear in front of Form A. Both forms are maximized when shown. The problem is, the ListView in Form A is making Form A take the focus back right after Form B is shown. The screen flickers temporarily, so Form B is initially in front. I know it's the ListView doing it because if I open Form B from another control, it works as intended.
I'm displaying Form B with Show(). Changing it to ShowDialog() fixes the problem, but I don't want it to be modal.
This is happening during the handler for the ListView1.ItemSelectionChanged event, and changing to ListView1.SelectedIndexChanged doesn't help.
Some code, if it makes it any clearer:
Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
If e.IsSelected Then
Dim my_form As New MyForm
my_form.Show()
End If
End Sub
Set the ListViews Activation Property to "OneClick", then handle the ItemActivate Event and display your Form from there.

Visual Basic 2008 auto-scan controls

I have a form containes some controls (buttons) how i can make the program autoscan these controls one by one and whenever a button higlited I can bress enter to press this button. Using Visual Basic 2008
You don't have to autoscan the controls, pressing TAB cycles through the controls on the form. Just press TAB until you have the right control selected and then use the KeyDown method for the control:
Private Sub Button1_KeyDown(byVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
If e.KeyCode = Windows.Forms.Keys.Enter Then
' Do whatever
End If
End Sub
In the If Statement, I recommend you reference a custom Sub that you'll also reference in the Button1_Click event. This will allow you to do make sure that hitting Enter on the selected control and clicking on it do the same thing.
The only downside to this is that you have to write Event Handlers as shown above for every control on your form if that's what you want.
HTH

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.