Is there anyway to not let user lost focus from winforms in vb.net? - vb.net

Issue:
My Question may be weird but I need help with this.
I have this weird problem with my client PC and it all happens sometimes.
The application loses control when I close one form and opens another form.
Then he has to use the mouse and click on the form every time.
Similar but another issue.
In an MDI child form, what I do is make the user fill data in a bunch of textboxes and make the user click (or hit Enter Key) that adds that data in a datagridview row and clears all textbox and make focus on the first textbox.
At that time too the form loses focus and the user is not able to write anything. Though the textbox is still coloured (custom coloured textbox on got focus) but form loses control.
The Temporary solution:
-I make his PC restart and everything starts working fine.
-Even Closing the whole application won't help it. I have to restart the PC only.
MY Code between two forms:
Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click
....
Hide()
frmUser.Show()
End Sub
I use Hide to close 1st form else I use close() to switch between forms.
I even face this issue of losing focus in MDI child forms too.
I even made a custom textbox that changes its background colour when it gets focus (in gotfocus event) and reverse to white when loses focus (lost focus event), so when the issue starts the textbox remains coloured and even the cursor blinking inside but I cannot type in it. Again I have to click on the application form (anywhere) and I am able to type again. Until that form closed or changed in the application.
Please help with this weird issue. Thanks...

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.

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.

Form maximize on another form

I have an MDI application which contains many child forms
My problem is, on clicking a particular menu, I am opening a form with maximize window in the MDI form. This works fine.
Now if I open another form above the first one, and if i want the second form to be of normal size, i am unable to do it.
Second form also opens with maximized window similar to first one. I want the second form to be of normal small size.
I want to show second form normally and first form maximized.
How can i do that?
Private Sub TESTToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TESTToolStripMenuItem.Click
Dim f As New newCalendar2("UGHARANI")
f.Show()
f.MdiParent = Me
f.WindowState = FormWindowState.Maximized
End Sub
Okay, I think I understand what you're trying to accomplish: you want the first (data) form to be a kind of background to your MDI application and have the other forms display on top of it, right?
Well one way to do it might be to remove borders from the background form –FormBorderStyle = None– and fill-dock it in the MDI parent form. Although it would end up coming to the fore and hiding all your other forms if a user clicked anywhere on it. But if it doesn't require any user interaction you could always use its Activate event to send it back to the background, using Me.SendToBack().

vb.net activated fires multiple times

I want to update a database based on the form that is currently activated. I had originally decided to use the GotFocus event. However I now understand that will not work as the form has controls on it. So I then thought I wouls use the activated event. This works but seems to fire multiple times. I put in the following code:
Private Sub frmNewTicket_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
MsgBox("Form Activated")
End Sub
I select the form and make it activated and the message box appears about 15 times.
Why does it do this? How should I handle this. I only want my code to execute once when the form is activated.
NOTE: There are several forms that the users will be changing between, incuding forms from other applications.
Each time you click OK on the messagebox, the form regains the focus and is activated again.
Put a static Boolean value in your frmNewTicket_Activated like someone has posted here:
Static HasRan As Boolean=False
If Not HasRan Then
HasRan=True
'put code here
End If
It sounds like you are wanting to do something everytime your form gets activated. The Form Activated event will work fine as long as what you are doing doesn't pull focus from the Form which will then trigger another Activation event when the Form gets focus again. Try using something other than a MessageBox for testing like Beep or changing the Form's Color

vb.net Prevent Form 1 from activating when form 2 is clicked

im having a similar problem like the solution here Prevent main form from appearing when showing another form . but some of the suggestions were to minimize the main app so it doesnt show, which i cant do because my main app is supposed to be a desktop to be underneath all other apps to replace the windows desktop. And the second forms are supposed to be sticky notes. so i cant minimize the main window cause it has the user background and other controls. i tried making the parent of the notes a Nothing pointer, a pointer to the desktop, creating the form through a dll but i had no success.
My main problem is that when i click a note (form2) form1 comes up, even with form1 having the WS_EX_NOACTIVATE in the createparams. form1 does the form2.show() but they shouldn't be attached.
Another reason im having trouble with the solutions preseted in that post is that they are for delphi and im doing it in vb.net.
All i need is being able to click on the controls and write in the note without bringing the main form behind the note. either making them independent, or making the note not focusing the first form or being able to operate the note without it activating. i dont know. my last resource is to attach my main form to the desktop but i've heard is the worst thing you can do because it can cause problems hanging the system.
If you want both forms to co-exist but don't each to interfere with the other: In this case, then you might want to have a third Form that calls both Form1 and Form2 to be opened, and let me suggest and MDI Form with Form1 and Form2 as children forms of the MDI form
'============== my previous post ========================
You can force the user to first dismiss Form2 then allow him to go back to form1 by showing Form2 as Modal form. Here is how to display Form2 as modal
Dim f2 as New Form2
f2.ShowModal()
If that does not work, try this
Dim f2 as New Form2
f2.Show(True)