I am trying to define an object in one form, and then when the user clicks a button, the object will be added to a new form that shows.
Form 1
Public Shared label As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
label.Text = "test"
End Sub
Form 2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls.Add(Form1.label)
End Sub
Whenever I run this, it does not put the label on the second form.
Can anyone tell me what I am doing wrong, or show a better way of doing this?
Related
When I open Order Form from Dashboard Form, Order Form appears, Dashboard Form is hidden. But when I want to close it. Dashboard Form is still running. I need to manually stop it in vb.net. Anyone help!!
Public Class frmDashboard
Private Sub btnDashboard_Click(sender As Object, e As EventArgs) Handles btnDashboard.Click
Me.Show()
End Sub
Private Sub btnOrders_Click(sender As Object, e As EventArgs) Handles btnOrders.Click
frmOrders.Show()
Me.Close()
End Sub
Well, you shouldn't do that... but if it's really what you want...
On Form2, add something like that :
public Parent as Windows.Forms.Form
On Form1, when you open Form2, set Parent = Form1
Private Sub btnOrders_Click(sender As Object, e As EventArgs) Handles btnOrders.Click
Form2.Parent = Me
Form2.Show()
End Sub
So, on Form2, you can close Form1 when Form2 is closing.
Private Sub Form2_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Parent.Close()
End Sub
But, as I wrote, I think you shouldn't do that... it's not a common behaviour.
Instead of closing Form1, I suggest you to refresh it / reload / make it visible again (by using the Parent variable)
I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.
Hello to stackoveflow community members.!
I am reading threads from stackoverflow from past 6 months and which was really helpful for me. I never get stuck to anything where i need to write my own question here.
Currently i am working on visual studio 2015 with window based .net application.
I am having multiple forms in my application. I am loading a form into another form through panel control.
Say i have a form1 which is main form and form2 which i have opened main form using panel control.
When i am opening my form1 as startup form the data transfer is taking place. But when i am using another form say form3 for login page as startup the data transfer is not working between form1 and form2.
Could some please help.!!
Please find example code as below.
enter image description here
Number of Forms - 3,
Form3 as Login Form,
Form1 as Main Form,
Form2 as Entry Form,
Code for Form3-==========
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form1 As New Form1
form1.Show()
Me.Hide()
End Sub
Code for Form1-===========
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = "Hello"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim f As Form
f = Form2
f.TopLevel = False
f.WindowState = FormWindowState.Normal
f.FormBorderStyle = FormBorderStyle.None
Me.Panel1.Controls.Add(f)
f.Dock = DockStyle.Fill
f.Show()
End Sub
Code for Form2-=========
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.RichTextBox1.Text = Me.TextBox1.Text
End Sub
As suggested by 'Preciousbetine', i have removed new instance for form1. Application is working as desired.
Thank you so Much!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Hide()
End Sub
Just noticed in one of my programs that the form event Shown only runs the code when the form is first displayed. Every other time after that it does not run the code. Is there a form event that does what shown does, but every time it is displayed instead of just the first? Or is there a way to get around it? This is the exact same for the Load event too.
Thanks heaps, appreciate any response.
I just tested with this in Form1:
Private f2 As New Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
f2.Show()
End Sub
and this in Form2:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Form2_Load")
End Sub
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Visible Then
MessageBox.Show("Form2_VisibleChanged")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
End Sub
and it worked exactly as expected. Every time I clicked Button1 in Form1, Form2 was displayed. The first time I saw messages for "Form2_Load"and "Form2_VisibleChanged" and on subsequent occasions on for "Form2_VisibleChanged".
***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub