How to give focus to the new instance of a form? - vb.net

I've a program in vb .net with a form named Form1. The form has button when I click on the button a dialog window appears. The dialog window has two buttons OK and Cancel. When the OK button is clicked a new instance of Form1 is created and the new instance is shown. See below code.
Private Sub ButtonOK_Click(...) Handles ButtonOK.Click
Dim frm1 As New Form1
frm1.Show()
End Sub
The problem is when a new Form1 is created by the above code, the previously created Form1 gets focused and the newly created Form1 looses focus. But I want the newly created (latest) Form1 to get the focus. How do I do it?

I have an application that I need to force the user to input information on a new form before proceeding. You can force the new form to show on top and give it focus by doing the following.
Dim frm1 = New Form1()
frm1.Show()
frm1.WindowState = FormWindowState.Normal
frm1.BringToFront()
frm1.TopMost = True
frm1.Focus()

Related

Setting a value in a textbox from another form

I'm using vb.net 2013.I have 3 forms : Form1, Form2, Form3
On form1 I have a button . When this button is pressed , the form2 is open.The code inside the click event is :
Dim dlg1 As New Form2
dlg1.Show(Me)
Inside From2 I have a TextBox (Txt1) and a button .When this button is clicked the Form3 is open.The code inside the click event is :
Dim dlg2 As New Form3
dlg2.Show(Me)
Inside form3 I have a button that I use to set a value in the textbox (txt1) on Form2. I use this code :
Form2.txt1.Text="123"
The problem is that after I press the button on form3 , the textbox on form2 is empty , no value is set.
What can I do ? ( I don't want to change the way the forms are open)
Thank you !
Form2.txt1 references default instance of the form.
You are using a new instance here:
Dim dlg1 As New Form2
You either need to replace this code:
Dim dlg1 As New Form2
dlg1.Show(Me)
With
Form2.Show(Me) 'not recommended
Or cast your form's Owner to your previous form's type, and set the property (recommended):
DirectCast(Me.Owner, Form2).txt1.Text = "123"
Just open The forms with Form2.Show() , Form3.Show()
Then If you put this in your Form3 button click event " Form2.txt1.Text="123" " It will work.

Working With multiple instance of a windows form in vb.net

I have three forms (form1, form2, form3) in a windows application using vb.net
form1 has a button (button1). Onclick of button1, I want to open form2 in such a way that it can also open multiple times. I achieved this with the code below:
Dim myForm As New Form2
myForm.Show()
Now form2 has a button (button2), and a label (label1). Onclick of button2, I want to open a single instance of form3 a dialog, so I have the code below:
form3.showdialog()
form3 has a textbox (textbox1).
My problem is that I want when I fill textbox1, I want the value to appear in label1 of form2 that opened the form3, I tried the code below, but it did not work:
form2.label1.Text = textbox1.Text
I need to update form2 (the last active one) once form3 has been closed
Can anybody help me out please?
When you go to show the Form3 as a dialog, you should be able to do:
Dim f3 As New Form3
f3.ShowDialog()
Me.label1.Text = f3.textbox1.Text 'Copy the value out of the dialog

Closing winform from button on another winform. Form will not close

I have 2 forms on my application. I have the main form that opens TopMost, CenterScreen and Maximized. Then I have another form on this screen that pops open when I press a button. That second screen has a button that navigates to another screen, so when I press that button the second form closes, and the main form is suppose to close as well and the selected sheet open up.
However, the second screen closes fine, but my main screen remains open and active, while the called sheet opens but does not enable. I track down what was happening and the issue is that form all code runs, but the main screen does not seem to want to close. Here is my code:
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
Dim welcomeForm As New frmWelcomePage
If lblReportTitle.Text = "Employee Dashboard" Then
Me.Close() 'This works
welcomeForm.Close() 'This one remains open and active
Globals.dsbEmployeeBoard.Select() 'This one opens but is not enabled
End If
End Sub
I assume from your description that you already have a welcome form created and displayed before the form with the button is displayed.
This line of code:
"Dim welcomeForm As New frmWelcomePage"
is creating a new copy of the Welcome Page and closing it.
Instead of creating a new one, you need to reference the original one that is open.
If I recall correctly, you should be able to just remove that line and use frmWelcomPage.Close.
You need to pass a reference of your first form (Form1) to the second form (Form2), so that in the second form you can close the first form, like this:
Public Class Form2 Inherits Form
Private _form1 As Form1
Public Sub New(form1 As Form1)
Me.Form1 = form1
End Sub
End Class
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
If lblReportTitle.Text = "Employee Dashboard" Then
_form1.Close()
End If
End Sub
Then when you instantiate Form2, you would pass a reference to Form1, like this:
Dim form2 As New Form2(Me)
Note: Me is the instance of Form1.

How can I make a custom .Net Windows Form that acts like a MessageBox?

How can I make a form that behaves like a message box?
For example, I have 2 forms named form1 and form2.
form1 contains my basic information (Name, gender, address and 1 button (educational background)).
form2 contains my educational background (school, school_add, button(OK) and button (Cancel)).
If I open form1 and I click the educational background button, then form2 will appear. What I want is that if the user tries to click form1 while form2 is still open, then form2 will blink and the user cannot manipulate form1. I want to require the user to click the Cancel or the OK button on form2 before returning to form1, just like how a message box prevents you from using the form behind it.
How can I do this?
What you are looking for is called a Modal form.
It's very easy to do: just call .ShowDialog(Me) instead of .Show().
' Form1 button handler
Private Sub buttonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles button.Click
Dim f As New Form2()
f.ShowDialog(Me)
End Sub
See also http://msdn.microsoft.com/en-us/library/39wcs2dh(v=vs.100).aspx
Load your form using .ShowDialog instead of .Show
Form2.ShowDialog

How to pass the value to current active form

Using VB.Net
I have main form name as form1 and popup form name as form2
Form1
tab button - for creating mulitple copy of form1 at runtime...
Creation of mulitple from1 at runtime code
form1 code
Dim mEntryForm As form1
mEntryForm = New form1
mEntryForm.Show()
The above code is creating a same copy of form1 at runtime.
Now i want to pass the value from popup form to current activeform
Code for sending the value to form1 from popup form (form2)
form2code
form1.textbox1.text = "100"
The above code is going to form1 textbox, instead of active form (mentryform)
How to solve this problem.
Need Vb.net code help
Although I totaly agree with the comments about your question I ll try to give you a solution
Add a property to your Form2
name smtng like ActiveForm1 as form1
now because I haven't understand completey your concept:
-> if Form2 is a ShowDialog form then you cannot change the active form1.
You need to set the property ActiveForm1
dim frm2 as new form2
frm2.ActiveForm1=me
frm2.ShowDialog
->if Form2 is not a ShowDialog that means that you can change the active form1
then you need to add this lines of code when a form1 is activated
frm2.ActiveForm1=me
Now in form2:
me.ActiveForm1.textbox1.text = "100"
I hope that I helped you.