WithEvents+Handles+Overrides - vb.net

Say I have an member (e.g., Button1) of an object (e.g., Form1) that is delared using withevents (e.g., Form1.Button1_Click), and there is a handler with 'Handles' in that object.
If I override it (say, Form2.Button1_Click), will the handler call the overriden version (like me.Button1_Click) or the one with the actual handles on it (like MyClass.Button1_Click)?
Here's what I tried:
Public Class Form1
Public Overridable Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Form1's Button")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim f2 As New Form2
f2.Show()
End Sub
End Class
Public Class Form2
Inherits Form1
Public Overrides Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("Form2's Button")
End Sub
End Class

Specifying the Overridable modifier indicates that the method can be overridden.
To override is to reject or cancel (a decision, view, etc.).
Overrides specifies that the method will override the existing event handlers implementation. The existing method in Form1 will never be called unless you manually call it. You can manually call it by using MyBase keyword which essentially allows you to reference the base class of the current instance.
Public Overrides Sub Button1_Click(sender As Object, e As EventArgs)
MessageBox.Show("SecondForm's Button")
MyBase.Button1_Click(sender, e)
End Sub

The overridden version is called. When I click button1 on Form1, I get 'Form1's Button'. When I start the second form using button2, I click button1, and get 'form2's button'
Just so anybody tries to google this and finds nothing like I did, I killed 10 minutes of my life to test it, and now nobody else will need to!

Related

Best way to use a form as a GUI, but code it elsewhere

I'm wanting to use visual studio to create a form with buttons, but then have it coded elsewhere, so that I can use the events (ie; button clicks) to perform specific actions in this application.
Is it possible/practical to do this? If not/if so, what do I need to look up and learn about from here to be able to implement it? My knowledge base in programming is limitted; I've just been starting to get familiar with Classes.
(I'm working in Autodesk Inventor, and am trying to create a prompt window with buttons to control the output of another program I have. In order to save on the amount of calls/interfacing, I was hoping to just create an uncoded button form, but code it within my program/macro I have within Inventor - its to be a form with six buttons that end up rotating some graphics within the program at a certain stop point, and then the program resumes when the form is closed via the "x")
I have seen posts such as the one below, but that doesn't seem to have the capability to receive user input: How to create a custom MessageBox?
Currently I am here, which works for showing the toolbox. Can someone show me how I would handle the events please?
AddReference "C:\Users\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\bin\Release\SectionSymToolBox.dll"
Imports System.Windows.Forms
Public Class SectionSymRule
'Public dlg As New System.Windows.Forms.For
Public Shared ToolBox As New SectionSymToolBox.SectionSymToolBox
Dim WithEvents EClass As New EventClass
Sub Main()
ToolBox.Show()
End Sub
End Class
Public Class SectionSymToolBox
Private Sub Main()
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Swap Symbols
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Flip Symbol
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Flip Text
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'<
End Sub
Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'>
End Sub
End Class
You can always use Partial Class to split your code into multiple files.
So you create your Form (say Form1) the normal way. Then put the code in another class file with class declared as Partial
For example,
Partial Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(TextBox1.Text)
End Sub
End Class
The other way is to inherit your Form. The inherited form will then have everything of whatever is on the form, with whatever you want to add.
Public Class Form1Code
Inherits Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(TextBox1.Text)
End Sub
End Class

Editing button properties from a module

I am just starting out with Visual basic .Net.
I can't seem to figure what's the scope of button properties like button.text. Can they be used outside the button_click event sub? And if so, how?
How can I modify button properties from a module in real time when a certain condition is met?
I'd surely appreciate some guidance and an example, if possible. Thanks.
Just as quick sample, I don't suggest doing something like this
I have 2 forms open, Form2 and Form3. Each form has a button on it.
I also have a Module, called MyModule
Public Class Form2
Public Sub ChangeButtonText(ByVal s As String)
Button1.Text = s
End Sub
End Class
.
Public Module MyModule
Sub ChangeForm2Btn()
Form2.ChangeButtonText("LOL")
End Sub
End Module
From my Form3 I click the button and call the module function to change Form2 button's text
Public Class Form3
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyModule.ChangeForm2Btn()
End Sub
End Class
You could pass a reference to the button to a sub in the module, and then call that sub from the form.
i.e.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeButtonText(Me.Button1, "Changed")
End Sub
End Class
Module modButton
Public Sub ChangeButtonText(ByRef Button As Button, ByVal Text As String)
Button.Text = Text
End Sub
End Module

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

Switching between different forms

I have some forms in my application and need to switch between them. I dont want to open another form over other form, in short I want the user to get a feel that he is working on one form only. Right now I am doing something like this but this is not what I want
Public Class Form1
Dim fo As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.TopLevel = False
Me.Panel1.Controls.Add(Form2)
Form2.Show()
End Sub
End Class
I also need to send data to other form like list of some class.
Look into UserControls(Composite Controls). They will allow you create a custom layout with events and properties and add it to your panel. I have used this to swap in/out edit pages for my applications.
Here is a very simplistic example:( 1 Form, 2 Panels, 2 UserControls)
Form1
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub UserControl11_SendText(value As String) Handles UserControl11.SendText
UserControl12.SetText(value)
End Sub
Private Sub UserControl12_SendText(value As String) Handles UserControl12.SendText
UserControl11.SetText(value)
End Sub
End Class
UserControl
Public Class UserControl1
Public Event SendText(ByVal Text As String)
Public Sub SetText(value As String)
Label1.Text = value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent SendText(TextBox1.Text)
End Sub
End Class
Have you considered using a TabControl? This will let you create different screens, and you (or the user) can switch between them easily.

vb.net passing values out of forms

I have a very simple windows forms setup. Form1 has a progress bar and a button on it, when clicked the button opens Form2 which also has a button on it that launches Form3. On Form3 is a button which I want to use to raise an event back to Form1.
To achieve this can I add an event handler on form1 that will listen for an event of the type raised in form3? Or do I have to pass references to form1 to form2 and then from form2 to form3?
Any advice on the best way to achieve this is greatly appreciated.
Many thanks
You'll want to add an event handler on each form that will "bubble-up" the event being thrown on the third form.
Public Class Form1
Private WithEvents form2 As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
form2.Show()
End Sub
Private Sub Form2_MyEvent() Handles form2.MyEvent
MessageBox.Show("We're back on Form1.")
End Sub
End Class
Public Class Form2
Private WithEvents form3 As New Form3
Public Event MyEvent()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
form3.Show()
End Sub
Private Sub Form3_MyEvent() Handles form3.MyEvent
RaiseEvent MyEvent()
End Sub
End Class
Public Class Form3
Public Event MyEvent()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent MyEvent()
End Sub
End Class