Event from other class - vb.net

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.

Related

Creating form name and object name as parameters of a function

I'm trying to pass form name and object as parameters of my public sub because I have 2 forms and they are almost identical to one another.
For example I have richtextbox1 in Form1 and I also have richtextbox1 in Form2, I would like to create a public sub something like this.
Public Sub Sub1(ByVal frm_name As form, Byval obj_name As object)
frm_name.obj_name.Lines.Length 'Get the length of lines of that richtextbox
End Sub
Then when I want to call it
'Form 1 Codes
Public Class Form1
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(me, richtextbox1)
End Sub
End Class
'Form 2 Codes
Public Class Form2
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(me, richtextbox1)
End Sub
End Class
It is not working the way I want it, is there anything I can do for it to work?
Think about it. You are saying that you're passing the names of things but you're not. You're not passing the name of a form; you're passing the form itself. You're not passing the name of an object; you're passing the object itself. What is the actual point of this? It's to get the number of lines in a RichTextBox, right? So, write a method that takes a RichTextBox as an argument and then call it passing a RichTextBox.
Public Sub Sub1(rtb As RichTextBox)
Dim lineCount = rtb.Lines.Length
'...
End Sub
'Form 1 Codes
Public Class Form1
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(richtextbox1)
End Sub
End Class
'Form 2 Codes
Public Class Form2
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(richtextbox1)
End Sub
End Class

Passing control from one class to an event handler from another class

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

Changing Controls of a form inside a panel

I am facing a weird problem
I have 3 forms: MainForm, Form1, Form2
MainForm has 1 Panel: Panel1
Form1 has 1 Label: NameLbl and Button: ChangeBtn
Form2 has 1 textbox: NameTxt and Button: SaveBtn
I used the following code to open form1 inside Panel1 in mainform
Panel1.Controls.Clear()
Dim FormInPanel As New Form1()
FormInPanel.TopLevel = False
Panel1.Controls.Add(FormInPanel)
FormInPanel.Show()
On ChangeBtn.Click Form2 opens as showdialog
I want NameLbl.text to change to NameLbl.text when SaveBtn is clicked But normal code doesnt work.
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
Form1.NameLbl.text=NameTxt.text
End Sub
What Should I do? Any suggestions? Given that i need to open the forms in panels for certain reasons.
Please keep in mind that this is just an example. I have multiple controls in Form1 which i want to change on form2.SaveBtn.click
I have also tried this but it does nothing
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If c.Name="NameLbl" Then
c.Text = NameTxt.Text
End If
Next
End Sub
Please Somebody tell me how it do it!
Form2 doesn't seem to have any connection back to Form1 or MainForm. You'd need to raise an event from Form2 which MainForm handles or another class handles and can pass to MainForm
Edit:
Sorry, I've just seen how you're calling Form2. There are lots of ways to get a value back from Form2 after calling ShowDialog(). One is to create a property called Result and check Result if ShowDialog() == DialogResult.OK. Something like the following.
Public Class Form2
Inherits System.Windows.Forms.Form
Public Property Result() As String
Get
Return m_Result
End Get
Set
m_Result = Value
End Set
End Property
Private m_Result As String
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Public ChangeBtn As Button
Public NameLbl As Label
Public Sub New()
Me.ChangeBtn = New Button()
AddHandler Me.ChangeBtn.Click, AddressOf ChangeBtn_Click
Me.NameLbl = New Label()
End Sub
Private Sub ChangeBtn_Click(sender As Object, e As EventArgs)
Dim form As New Form2()
Dim dr = New form.ShowDialog()
If dr = DialogResult.OK Then
Me.NameLbl.Text = form.Result
End If
End Sub
End Class
I'd like to add that if you plan on growing this application much larger you'll run into issues with maintenance. Look into some patterns for dealing with UI logic like MVC, MVP, MVVM if you're interested.
you can try this code:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
panel1.Controls(0).NameLbl.text=NameTxt.text '"0" is the index of forminpanel in panel1,maybe it need to change.
End Sub
Form1 is contained in Panel1, so you can not access it via
Form1.
Only MainForm is visible from Form2:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If TypeOf c Is TextBox Then
c.Text = NameTxt.Text
End If
Next
End Sub
Try this:
For Each form1 As Form1 In MainForm.OwnedForms.OfType(Of Form1)
Form1.NameLbl.text = NameTxt.text
Next
I've faced same issue and I've fixed with this
Dim f As FormInPanel
f = Form.Panel1.Controls(0)
f.transection = True
f.NameLbl.text=NameTxt.text

VB.NET UserControl does not receive parent form's events

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

How do I handle events with interfaces in VB.NET?

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