I have a TextBox1 in Form1. I need to pass it to another class (in class libraries/another project). So, that instance of class can modify what inside TextBox1 within the entire class (not just a scope). For my problem i need to pass it to an event handler.
Public Class TheClass
WithEvents Timer1 As New Timer
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
End Sub
End Class
I can think of passing by reference. But, I can't find a way to pass TextBox1 to that event handler.
What should I do to make the Timer1 have access to modify TextBox1 in Form1?
If TheClass is created after Form1, you can use constructor injection in order to pass a reference of Form1 to TheClass.
Public Class TheClass
Private WithEvents Timer1 As New Timer
Private m_form1 As Form1
Public Sub New (ByVal form1 as Form1)
m_form1 = form1
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
m_form1.TextBox1.Text = "tick"
End Sub
End Class
Note that TextBox1 must be Public or Friend.
Another way is to add a public event to TheClass.
Public Class TheClass
Public Event Tick()
Private WithEvents Timer1 As New Timer
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
RaiseEvent Tick()
End Sub
End Class
Now the form can handle this event.
' In Form1
Private WithEvents theObj As New TheClass
Private Sub theObj_Tick() _
Handles theObj.Tick
Me.Textbox1.Text = "tick"
End Sub
Now you can keep Textbox1 private and TheClass does not need to know anything about a textbox.
The event handler can also have parameters
' In TheClass
Public Event Tick(ByVal counter As Integer)
...
Counter += 1
RaiseEvent Tick(Counter)
and
' In Form1
Private Sub theObj_Tick(ByVal counter As Integer) _
Handles theObj.Tick
Me.Textbox1.Text = "counter = " & counter
End Sub
Related
I would like to share an event to another form via a common class with a public shared event.
My code is setup like this:
Form1 is the main form where the button.click event is generated
Form2 is a subform displayed in Form1 in a panel control, where if the event is generated executes a sub.
To share data I use a commonData class, to share the only data i need, because I want to keep private function and variables in the single forms.
Can someone help me figure out what I'd like to do?
something like this, but working
Public Class commonData
Public Shared Event event1()
End class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.Click
RaiseEvent commonData.event1()
End Sub
End class
Public Class Form2
Private Sub eventFired(sender As Object, e As EventArgs) Handles commonData.event1
MsgBox("event")
End Sub
End class
There is many techniques to do that but this one (which is one of those) I hope might help you to have an idea how can have a shared Event between Forms or other classes.
First as you want here is the common module (you can use a class instead, is your choice; but <Extension()> go only in modules).
CommonData as a Class and ExtensionUtils as a Module for extensions:
Imports System.Runtime.CompilerServices
Public Class CommonData
Public Shared Event MyGlobalEvent(eventSender As Object, otherParams As String)
Shared Sub RaiseMyGlobalEvent(eventSender As Object, otherParams As String)
RaiseEvent MyGlobalEvent(eventSender, otherParams)
End Sub
End Class
Module ExtensionUtils
<Extension()>
Public Sub ButtonClick(ByVal buttonCaller As Button, eventArgs As String)
CommonData.RaiseMyGlobalEvent(buttonCaller, eventArgs)
End Sub
End Module
And here the implementation:
In this example I use a button named “Button1” in Form2 and when I click on it show a msgbox in Form1.
Form1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
AddHandler CommonData.MyGlobalEvent, Sub(objSender As Object, message As String)
MsgBox(message & vbCrLf & " But I'm telling you that from form " & Me.Name)
End Sub
End Sub
End Class
Form2:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, Sub()
Button1.ButtonClick(("I'm clicked from form " & Me.Name))
End Sub
End Sub
End Class
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
I have to "catch" an event from another class but I can't.
Project contains two forms: Form1 and Form2 and class params. Form 1 contain one button which have to change property value in class props. And Form2 have to know when props raises event.
Here is situation:
''Form1 code
Public Class Form1
Dim p As New params
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
p.demo = "mytry"
End Sub
End Class
''Form2 code
Public Class Form2
Private WithEvents p As params
Private Sub pChanged(ByVal propertyName As String) Handles p.PropChanged
MessageBox.Show(propertyName)
End Sub
End Class
''Class params code
Public Class params
Public Event PropChanged(ByVal propName As String)
Private _demo As String
Public Property demo() As String
Get
Return _demo
End Get
Set(ByVal value As String)
_demo = value
RaiseEvent PropChanged("demo")
End Set
End Property
End Class
Problem is that sub pChanged in Form2 is never fired and have to be fired when property "demo" raises an event PropChanged.
How to get this working?
You have two params variables and you are handling the wrong in Form2 if you change the property in Form1. You could handle the event in Form2 and raise it again to handle it in Form1.
One here:
Public Class Form1
Dim p As New params
and one here
Public Class Form2
Private WithEvents p As params
Note that you have to use WithEvents p As New params also in Form1 if you want to handle the event there.
I have created a usercontrol and added it to a form.
I would like to receive the form's event using Private WithEvents _Parent As Form.
But none of the events is being received.
The entire code of my usercontrol is attached.
Does anybody see what I am doing wrong?
Public Class UserControl1
Private WithEvents _Parent As Form
Public Sub New()
InitializeComponent()
_Parent = Me.Parent
End Sub
Private Sub _Parent_Activated(sender As Object, e As EventArgs) Handles _Parent.Activated
MsgBox("activated")
End Sub
Private Sub _Parent_Resize(sender As Object, e As EventArgs) Handles _Parent.Resize
MsgBox("resize")
End Sub
End Class
When the constructor is called, there is no parent yet (Windows Forms controls are added to their parents after the class is created). Me.Parent returns Nothing at this point.
Handle Me.ParentChanged to initialize _Parent:
Private Sub UserControl1_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged
_Parent = Me.Parent
End Sub
I'm new to interfaces and I'm trying to understand how they work.
I wrote the following code, which works properly except the click event which is not firing.
Public Class Form1
Dim WithEvents Button As IClass = New MyButton
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button.Parent = Me
End Sub
Public Sub ClickEventHandler(ByVal Sender As Object, ByVal E As EventArgs) Handles Button.Click
MsgBox("Piwpiw !")
End Sub
End Class
Public Interface IClass
Event Click(ByVal Sender As Object, ByVal E As EventArgs)
Property Parent
End Interface
Public Class MyButton
Inherits SimpleButton
Implements IClass
Public Event click1(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.click
Public Property Parent1 As Object Implements IClass.Parent
Get
Return MyBase.Parent
End Get
Set(ByVal value As Object)
MyBase.Parent = value
End Set
End Property
End Class
What's wrong with that code?
(PS: This is just an example allowing me to understand how interfaces work and doesn't have any functional meaning.)
You're missing a single method in the MyButton class to make this work.
You need this:
Private Sub MyButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click
RaiseEvent click1(sender, e)
End Sub
Essentially SimpleButton already has a click method. It's being raised when you click your derived MyButton class. But the Click event on SimpleButton isn't the same event as Click on the IClass interface. You implemented that as click1. So you just need to raise the click1 method when the Click method is raised. Hence the above method.
In your sample code, I don't see a line where the event is raised. In order to be able to handle an event in the event handler, you'd have a spot in your MyButton class that raises the event, i.e.:
Public Class MyButton
Inherits SimpleButton
Implements IClass
Public Event click1(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.click
Public Property Parent1 As Object Implements IClass.Parent
Get
Return MyBase.Parent
End Get
Set(ByVal value As Object)
MyBase.Parent = value
End Set
End Property
Public Sub SimulateClick()
RaiseEvent click1(Me, New System.EventArgs())
End Sub
End Class
You use the RaiseEvent statement to raise an event.
Above sample, of course, assumes that at some point in your code, you call SimulateClick instead of having a real mouse click (which would involve painting the button and reacting to several mouse events). Once the event is raised, your handler will be called. You could do this from your Form1_Load method:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button.Parent = Me
Button.SimulateClick()
End Sub
I have noticed that your MyButton class derives from a SimpleButton class. Instead of having the SimulateClick-method, you could also react to a click in the SimpleButton class and place the RaiseEvent statement there.
Here is the solution:
Replace MyButton class with:
Public Class MyButton
Inherits SimpleButton
Implements IClass
Public Shadows Event click(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.Click
Public Property Parent1 As Object Implements IClass.Parent
Get
Return MyBase.Parent
End Get
Set(ByVal value As Object)
MyBase.Parent = value
End Set
End Property
Public Sub ClickEventHandler(ByVal Sender As Object, ByVal E As EventArgs) Handles MyBase.Click
RaiseEvent click(Sender, E)
End Sub
End Class