How can you cause a combobox to loose focus? - vb.net

I am developing a windows forms project in vb.net. I have added a comboBox to my form. the form automatically focuses on the comboBox. Nothing I click on causes the comboBox to loose focus. I do not want the form to focus on the comboBox because I do not want users to change the selected text in the comboBox by accidentally moving the scroll wheel.
I have tried:
comboBox1.CanFocus = False
comboBox1.Focus = false
These properties are not write-able.
InvokeLostFocus(ComboBox1, New EventArgs)
This does not throw a compiler error but it does not seem to do anything either (focus stays).
I am really stuck and can not find anything on SE or google. Any help is much appreciated.
Thanks!

You need to focus another control. Note that the form itself cannot be focused.
Something like:
label1.Focus()
If this doesn't work for you, you can try this:
Private Sub Form1_Load(sender As Object, e As EventArgs)
Me.ActiveControl = label1;
End Sub
You can even try to disable and enable the ComboBox.

Related

Showing a form in the position where last form was hidden (VB.net)

For a school project, I am developing software that requires the use of multiple forms.
At the moment, when a button is clicked to show a form and hide the current one, it opens in the default location that is set in the properties.
I would like it to know where the last form was hidden, so that it can open the new form in the same location. This is vital as all the forms are the same shape/size and it'll make for a better user experience if the software retains the position that it is moved into.
Thank you for any help! If you have anymore questions, please ask as this is my first time asking a question on here and don't know if I included all relevant information!
Current code for hiding, and showing the form,
This is opening the main form from another form when a button is pressed:
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
Dim Home As Form
Home = frmHome
Me.Hide()
Home.Show()
End Sub
This was fixed by using the line: frmHome.Location = Me.Location and changing the property of the StartPosition (of the form) to Manual
Thanks to this user!
https://stackoverflow.com/users/8967612/41686d6564

Prevent old form closing until new form opens

A very basic/simple question with I'm sure, an even simpler answer, but I just cannot figure it out. I have two forms that a user can switch between using the corresponding menu link on each form. I want to be able to keep the previous form visible on screen until the new form is displayed. In it's current state, the form disappears off screen for around 3/4 of a second before the new one is shown and from a UI/design perspective, I'd like this to stay on screen.
I'm currently using the below code to close and open the forms:
form1.Show()
Me.Close()
form2.Show()
Me.Close
I have tried experimenting with ShowDialog() which does seem to keep it on screen on first run, but clicking back into the form a second time says an error message:
System.InvalidOperationException: 'Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.'
Is there a simple line of code to achieve what I want here?
If there is some time consuming code in the Form.Load event (for example, if data is being retrieved from a database) then the following code might help.
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'Assuming default instances
Form1.Close()
End Sub
Posting an answer incase there's an inherit reason this is not best practice or to avoid doing this, but using the below sped it up:
Private Sub menu1_Click(sender As Object, e As EventArgs) Handles menu1.Click
form1.Show()
form1.Refresh()
Me.Close()
End Sub
This was inspired by adding in a Application.DoEvents working, so adjusted the code to avoid that dreaded line.

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.

VB.Net: How to display groupbox on right click?

I am making a VB.Net web browser, and to make the whole thing look nice, when the user right clicks, I want a groupbox to show up where the mouse was right clicked. I am using the ChromeWebBrowser.Net project on SourceForge and when I add the following code:
Private Sub ChromeWebBrowser1_MouseUp(sender As Object, e As MouseEventArgs) Handles ChromeWebBrowser1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Right Then
GroupBox1.Location = (e.Location)
GroupBox1.Visible = True
Else
End If
End Sub
It should be working, but when I test it and right click on the web browser control, it does not show up. When I add the same code to the main forms code, it works fine, it just is not working on the browsing control. No errors or anything, it will just not show up. Is there something special I need to do, or can there be a workaround to this?
Thanks so much!
Honestly, this sounds more like you should be using a ContextMenuStrip. You can drop one onto your form(then add items to it), and finally after that you can set the webbrowser's contextmenustrip property to be the one you just designed.

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto