Raise Event on another Form - vb.net

subject is declare event on Form1 , and run that event on Form2
here is the code , but event doesnt work on Form2 !! what is missing here??
many thanks
Form1:
Public Class Form1
Private Sub btnOpenForm2_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
Form2.Show()
End Sub
Public Event show_My_Message()
Private Sub btnShowMessageOnForm2_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
RaiseEvent show_My_Message()
End Sub
End Class
Form2:
Public Class Form2
Public WithEvents My_Form1 As Form1 = New Form1
Private Sub Show_My_Message_On_Form2() Handles My_Form1.show_My_Message
MsgBox("Hello")
End Sub
End Class

Here's an example of what Jimi was talking about, using the .Owner property.
Form1:
Public Class Form1
Private Sub btnOpenForm2_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
Form2.Show(Me) ' <-- Pass Form1 into Form2 via Show()
End Sub
Public Event show_My_Message()
Private Sub btnShowMessageOnForm2_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
RaiseEvent show_My_Message()
End Sub
End Class
Form2:
Public Class Form2
Private WithEvents My_Form1 As Form1
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If Not IsNothing(Me.Owner) AndAlso Me.Owner Is Form1 Then
My_Form1 = DirectCast(Me.Owner, Form1)
End If
End Sub
Private Sub F1_show_My_Message() Handles My_Form1.show_My_Message
MessageBox.Show("Hello")
End Sub
End Class

Try it without cross form event handling,
Form1
Public MyForm2 As Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If MyForm2 Is Nothing Then
MyForm2 = New Form2
End If
MyForm2.Show()
MyForm2.Show_My_Message_On_Form2("Hello")
End Sub
Form2
Public Class Form2
Public Sub Show_My_Message_On_Form2(theMess As String)
MessageBox.Show(theMess)
End Sub
End Class

Related

How to hold reference to different forms with one variable

I have a Form (named Form1) which has a fixed size and some controls.
I created another Form (named Form2), which is a copy of Form1 with only difference being a different fixed size.
I created a Form "SharedForm", which holds subs and functions used by both forms (so I don't have to write them for each of them).
My problem is: I don't know how to keep reference for either form (only one at a time, ever).
If I declare FormRef variable as Form, I get an error that "label1 is not a member of form".
(Otherwise if I declare as Form1 or Form2 it works fine, but of course only for one form)
SharedForm looks like the following:
Public Class SharedForm
Public Shared FormRef As Form 'problem is here
Private Sub SharedForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FormRef = New Form2
FormRef.Show()
End Sub
Public Shared Sub Button1_Click(sender As Object, e As EventArgs)
FormRef.Label1.Text = "test"
End Sub
End Class
Form1 and Form2 are as so.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Just a class, not a Form.
Public Class EventCode
Public Shared Sub Button1Click(Sender As Form)
Dim frm As Form = Nothing
If Sender.Name = "Form1" Then
frm = DirectCast(Sender, Form1)
ElseIf Sender.Name = "Form2" Then
frm = DirectCast(Sender, Form2)
End If
frm.Controls("Label1").Text = "Hello World"
End Sub
End Class
In Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form1 shows "Hello World"
In Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form2 shows "Hello World"

Get values of one form into another form in Visual Basic

I am trying to get an integer value of one form (Form1) inside another form (Form2). I have tried to access it via the below code but not getting it. Can someone please tell me what I am doing wrong.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FinalPoints As New Form1
Label1.Text = FinalPoints.Label1.Text
End Sub
End Class
The problem: As you are showing your Form2 (Form2_Load is executed), a new Form1 is created. This newly created Form1 has NOT executed the Form1_Load function yet!
You would need to show the newly created FinalPoints (Form1) with FinalPoints.Show() like that:
Dim FinalPoints As New Form1
FinalPoints.Show()
Label1.Text = FinalPoints.Label1.Text
to let the Form1_Load function execute, which is then setting your FinalPoints.Label1.Text. But that would just opens a new Form1.
Also you can just get the public Points variable inside the Form2_Load like that (you also do not have to create a new Form1):
Label1.Text = Form1.Points
Alternatively: Just use a public variable inside Form2 and assign your value to it, before you show the form.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points.ToString
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FinalPoints As New Form2
FinalPoints.StringFromForm1 = Label1.Text
FinalPoints.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Public Property StringFromForm1 As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = StringFromForm1
End Sub
End Class
This line: Dim FinalPoints As New Form1 creates a new instance of Form1, but what you want is to refer to an existing instance of Form1. There are different techniques you can try. For example, overload the Show method of Form2.
Something like this:
Form1: pass the value to Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
Me.Hide()
f.Show(Points)
End Sub
Form2: fetch the value from caller (Form1)
Public Class Form2
Public Overloads Sub Show(ByVal Points As Integer)
Me.Label1.Text = Points.ToString
MyBase.Show()
End Sub
End Class

How to check if a Form has focus?

I have two WinForms. Let's say MainForm and ChildForm.
What I'm trying to make is when the MainForm is activated the ChildForm should always be visible, and when the MainForm loses focus the ChildForm should be hidden except if it's the ChildForm which was activated.
Here is my code :
AddHandler Me.MainForm.Activated, Sub()
Me.ChildForm.Show()
End Sub
AddHandler Me.MainForm.Deactivate, Sub()
If Not Me.ChildForm.Focused Then
Me.ChildForm.Hide()
End If
End Sub
AddHandler Me.ChildForm.Deactivate, Sub()
If Not MainForm.Focused Then
Me.ChildForm.Hide()
End If
End Sub
The code doesn't work. Basically when I click on a certain form (on the child form for example), the property Me.ChildForm.Focused is not correct and then ChildForm is hidden while it should be visible.
Can anyone know how to achieve that please ?
Not sure if this is a good one, but the idea is using flags for MainForm and ChildForm active status. These flags are set when MainForm and ChildForm's Activated and Deactive events are fired. For simplicity, I'll use a module to store the flags and the instance of ChildForm. Then call SetChildVisible() method in MainForm, Form1 and Form2 Activated event. Set startup object to MainForm.
Module1
Module Module1
Public FChild As ChildForm
Public FMainActive As Boolean
Public FChildActive As Boolean
Public Sub SetChildVisible()
FChild.Visible = Not FChildActive And FMainActive
End Sub
End Module
MainForm
Public Class MainForm
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim f1 = New Form1
Dim f2 = New Form2
Dim child = New ChildForm
Module1.FChild = child
child.Show()
f1.Show()
f2.Show()
End Sub
Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.FMainActive = True
Module1.SetChildVisible()
End Sub
Private Sub MainForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
Module1.FMainActive = False
End Sub
End Class
ChildForm
Public Class ChildForm
Private Sub ChildForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.FChildActive = True
End Sub
Private Sub ChildForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
Module1.FChildActive = False
End Sub
End Class
Form1
Public Class Form1
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.SetChildVisible()
End Sub
End Class
Form2
Public Class Form2
Private Sub Form2_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.SetChildVisible()
End Sub
End Class

How access methods of another classes?

I'm with troubles for access methods of another class "SocketClient" in a "Form2", but in "Form1" works very fine! In other words, I can send data from "Form1" using methods of "SocketClient", but the same thing I don't can do from "Form2".
How solve it?
Here is my code:
"Form1"
Public Class Form1
Public WithEvents C As New SocketClient
Public Yy As String = "|SPLIT|"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
C.Send("ref" & Yy & "data here")
End Sub
End Class
"Form2"
Public Class Form2
Public frm1 As Form1
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
' The error is here, don't sends data to server application
frm1.C.Send("ref" & frm1.Yy & "Name: " & TextBox1.Text)
End Sub
End Class
Assuming you have Form1 as the main form and you are creating Form2 from that main form, try passing a reference via the constructor:
Public Class Form2
Private frm1 As Form1
Public Sub New(mainForm As Form1)
Me.InitializeComponent()
frm1 = mainForm
End Sub
End Class
From your main form (form1), you would pass the reference:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 As New Form2(Me)
form2.Show()
End Sub

Event handling in VB.NET

In VB.NET, why isn't the following custom event not firing?
Public Class classes1
Public Event buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs)
Protected Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
attd.Show()
RaiseEvent buttonload(sender, e)
End Sub
End Class
Public Class attd
Dim WithEvents c1 As New classes1
Sub c1_buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles c1.buttonload
MsgBox("Event received")
End Sub
End Class
You are using your first form, classes1, to show your second form, attd, and then raise your CustomEvent. Then in your second form, attd, you are creating another instance of the first form, classes1, and then trying to attach your handler to that instance's event. They are not the same, so it will not fire.
It is really not clear exactly what you are trying to do. If you are just experimenting around with events you can try something like this.
Form1
Public Class Form1
Dim attd As Form2 = New Form2
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
attd.Show()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler attd.buttonload, AddressOf buttonLoadHandler
End Sub
Private Sub buttonLoadHandler(sender As Object, e As EventArgs)
MsgBox("Event received")
End Sub
End Class
Form2
Public Class Form2
Public Event buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent buttonload(sender, e)
End Sub
End Class
If you are just wanting to have your second Form respond to the First Forms Button Click try something like this.
Form1
Public Class Form1
Dim attd As Form2 = New Form2
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
attd.Show()
attd.showMessageBox()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Form2
Public Class Form2
Public Sub showMessageBox()
MsgBox("Hello World")
End Sub
End Class