How access methods of another classes? - vb.net

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

Related

Raise Event on another Form

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

Controlling non-startup form from another class

How do you control a form from another class if that form is not the "Startup form:"?
Scenario:
I have a simple application with 2 forms (Form1 & Form2) and a class (Class1). Form1 has two controls (TextBox1 and Button1). Form2 has one control (TextBox1).
Class1 code:
Public Class Class1
Public Sub ChangeTextOnForm1()
Form1.TextBox1.Text = "Success"
End Sub
Public Sub ChangeTextOnForm2()
Form2.TextBox1.Text = "Success"
End Sub
End Class
Form1 code:
Public Class Form1
Dim Class1 As New Class1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Class1.ChangeTextOnForm1()
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New Form2
Form2.Show()
End Sub
End Class
Form2 code:
Public Class Form2
Dim Class1 As New Class1
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Class1.ChangeTextOnForm2()
End Sub
End Class
If Form1 is set as the Startup Form it calls Class1.ChangeTextOnForm1() on Load and the text in Form1.Textbox1 is changed.
If Form2 is set as the Startup Form it calls Class1.ChangeTextOnForm2() on Load and the text in Form2.TextBox1 is changed.
If Form1 is the Startup Form and I click on Button1, Form2 opens but the text in Form2.Textbox1 is not changed. How do you control a form from another class if the form is not the startup form?
The solution is to open the default instance of Form2 instead of a new instance.
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Form2.Show()
End Sub

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 pass an integer value to other forms? [duplicate]

This question already has answers here:
Passing variables between windows forms in VS 2010
(3 answers)
How to pass value from Form1 to Form2
(2 answers)
Closed 5 years ago.
Is it possible to use a label? I'm planning to display a scoring system, like every correct answer the score will add 10pts.
Use an instance of a class within a form. The Form can call methods of the Class. The Class can raise events to the Form. Don't talk directly from a Class to a Form. Or from one Form to another.
Form1 with TextBox1:
Public Class Form1
Private myClass1 As Class1
Private myForm2 As Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myClass1 = New Class1()
myForm2 = New Form2(myClass1)
myForm2.Show()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
myClass1.SetText(TextBox1.Text)
End Sub
End Class
Form2 with Label1:
Public Class Form2
Private WithEvents myClass1 As Class1
Public Sub New(instance As Class1)
InitializeComponent()
myClass1 = instance
End Sub
Private Sub myClass1TextSet(value As String) Handles myClass1.TextSet
Me.Label1.Text = value
End Sub
End Class
Class1:
Public Class Class1
Private text As String = ""
Public Event TextSet(value As String)
Public Sub SetText(value As String)
Me.text = value
RaiseEvent TextSet(value)
End Sub
End Class
Form2.Label1 will update as you type in Form1.TextBox1. You may change it around as you need to fit your application, but try to keep this structure.
Form >> instance >> Class
Class >> events >> Form
The form instantiating the other form is for simplicity of this example. Larger scale projects could have a form loader factory responsible for making forms.
In your Form2 add a public shared integer
Public Shared score As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Then in Form1 increase it by adding to it
Form2.score += 50
for example
Add two forms to project
in Form2 define Label1 control
and in Form1
Public Class Form1
Private intScore As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Label1.Text = intScore.ToString
End Sub
End Class