How to pass the value to current active form - vb.net

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.

Related

How to give focus to the new instance of a form?

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()

Instances of a form

I got a problem and I'm a little confused, so I'm just going to put it simple:
I have 2 forms (form1 and form2).
Form1 has a tabcontrol which shows form2 in a tabpage.
Form2 has a button1 that closed itself and therefore the tabpage.
I'm using this code for button1:
Dim f1 As New Form1()
f1.tabControl1.Controls.Remove(f1.tabControl1.Selectedtab)
Using the above code I got an exception so just to be sure that the tabpage is there i add this in button1 before removing the tabpage:
Console.Writeline(f1.tabControl1.TabCount)
Using the instance shows 0. But using the default instance shows 1:
Console.Writeline(Form1.tabControl1.TabCount)
I want to know if I'm doing something wrong creating an instance
Pd: I know I can use this:
Form1.tabControl1.Controls.Remove(Form1.tabControl1.Selectedtab)
But I need to work with instance
Edit:
I got a NullReferenceException when i use:
Dim f1 As New Form1()
f1.tabControl1.Controls.Remove(f1.tabControl1.Selectedtab)
As i said, it works if i use the deafult instance
Edit 2:
This is the code i use to create the tabpage, this method is in form1 and i call it in a click event of a button in form1:
Dim tabpage1 as new Tabpage
Dim f2 as new Form2
tabpage1.Controls.Add(f2)
f2.show()
tabControl1.TabPages.Add(tabpage1)
i got the answer:
In form2:
Dim f1 As New Form1
Public Sub New(ByRef _f1)
' TODO: Complete member initialization
InitializeComponent()
f1 = _f1
End Sub
I just had to reference form1

Accessing text box on Form1 from Form2

I am new to vb.net so forgive me if this is an easy question.
I have a created a class library project that houses two windows forms, Form1 and Form2. The main class library has the event to open Form1. A button on Form1 launches Form2. The bulk of the code is in Form1, which I don't want to change if I can help it.
What I am trying to do, is access a sub that is on Form1 from Form2. That sub is changing the value of a text box on Form 1. I don't get any errors when I compile the project, however, nothing happens.
Here is an example
Form1:
Public Sub test()
Me.Panel1.Controls("Textbox1").Text = "Test"
End Sub
Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MainForm As New Form1
Me.Close()
MainForm.test()
End Sub
Don't get too caught up on how I built it out, I have tried about 20 different things and this is where I am at now.
I have tried defining Form1 in the sub test(). I have tried setting sub test() to shared. I have tried closing Form2 and activating Form1. I have tried changing the modifiers property on the text box to public. I have tried making Form1 the parent and Form2 a child (I honestly don't understand MDI very much). All these results end up in a project that will compile but wont give me any results. My code accesses the sub just fine, it wont access the text box's text property.
Any suggestions will help. I am trying to access the text boxes in a way that I can loop through all of them. For example: Me.Panel.Controls("Textbox" & i).Text = "Something". Also I would like to keep the sub in the class for Form1 if I can.
Any suggestions would be great!
You are creating a brand new Form1 in Form2, that is the problem.
Just use :
Call Form1.test()
By the way, I think this code in the sub is an easier way to set the text:
Panel1.TextBox1.Text = "Test"

vb.net: Using a Button on a Form to open another form, doesn't work

I encounter the following scenarios when I try to use 2 forms. My workflow is as follows:
(1) Load Form1.
(2) A click on button1 on Form1 closes Form1 and opens Form2.
Solution A: If I use the following code:
Dim oForm As New Form2
oForm.ShowDialog()
Me.Close()
Then Form1 will be under Form2 (Form1 still opens).
Solution B: If I use the following code:
Dim oForm As New Form2
oForm.Show()
Me.Close()
Then Form1 closes and Form2 opens, but Form1 is not on the top layer.
I have looked through the solutions for this, most propose solution B, but for me, both solutions won't work the way I want. Can anybody tell me the reason?
Try
Dim oForm as New Form2
oForm.Show()
and on Load event of form2
Form1.Hide()
Use form.bringtofront() if you want to see the opening Form in the front, I m little confused though about what you are trying to do
Try doing it this way:
Dim oForm As New Form2()
Me.Hide()
oForm.ShowDialog()
Me.Close()
I suspect your are building a log-in dialogue... if so, or something similar, try this..
Open your main form first... (Form 2), have form2 showdialog (modally) form1... this will put form1 on top of form2.
Add a property to form 1, that gets set depending on what happens there.. sucessfull login for instance.
Close form 1 from its own methods... (after successful authentication), set the property before closing.
On form2, read this property of form1, and then dispose form1, and decide what to do... if unsuccessful login, show the login form again, end app. If successful, just gracefully exit out of the method that showed form1. Your form 2 is now the only form open.
Start with Form2
Form2_load
dim f1 as new form1
f1.showdialog
if f1.someproperty = somevalue then
' do something here, for instance, pop the form again, if you did not get what you were lookign for...
end if
'gracefully let the function end and form2 is now the only open form..
'dispose of form1. form1's close call does not dispose it, because it was opened modally. (showdialog)
f1.dispose
f1 = nothing
in form1, depending on what you are doing, set the custom property and call me.close, this will exit the form, and run the next code in form2.
try this:
Dim oForm As New Form2
oForm.Show()
Me.Visible = False
You would to close your first form and this close your program. If you set him on invisible, he is not closed.
Just go to form2 and write the Form1.hide() . I tried to close form1 but it closed my whole program.
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Hide()
End Sub
End Class

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