Share code between two different child classes - vb.net

In my project I use a 3rd party library (for which I don't have the
source code) that provides ribbon-style forms.
There are two different types of forms actually used in the project, Office2007Form and Office2007RibbonForm.
This is the hierarchy tree of the provided form classes:
System.Windows.Forms.Form
+-RibbonForm
+-OfficeForm
| +-Office2007Form <---
+-Office2007RibbonForm <---
In my forms' form.Designer.vb I replace Inherits System.Windows.Forms.Form with either Inherits Library.Office2007Form or Inherits Library.Office2007RibbonForm.
Now I want to add some default behaviour to my project's forms.
To do this I could create a base form class, insert there my code and then inherit from my base class.
The problem here is that I have two different types of form, Office2007Form and Office2007RibbonForm, so I should implement my code twice:
Public Class MyBaseForm
Inherits Library.Office2007Form
Public Property MyProp As String = ""
Public Sub New()
MyBase.New
Me.Icon = My.Resources.appicon
End Sub
Private Sub Form_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F12 Then Suggestions(Me.Text)
End Sub
End Class
Public Class MyBaseRibbonForm
Inherits Library.Office2007RibbonForm
Public Property MyProp As String = ""
Public Sub New()
MyBase.New
Me.Icon = My.Resources.appicon
End Sub
Private Sub Form_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F12 Then Suggestions(Me.Text)
End Sub
End Class
Since vb.net/c# don't support multiple inharitance, I can't find a way to implement this code only once.

Related

Handle a Form Controls Events from a Class

The next code works for my, but I d'ont know if it's the best way to do it.
Of this way I need to write: _Button1 = Button1 and _MyVar = MyVar
This way of doing it seems repetitive and long when the parameters
passed to the Class Constructor are many more.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim myVar As String = "My children"
Dim NewClass1 As New Class1(Button1, myVar)
'... do more ...
End Sub
End Class
Public Class Class1
Private WithEvents _Button1 As Button
Private _MyVar As String
Public Sub New(ByVal Button1 As Button, ByVal MyVar As String)
_Button1 = Button1
_MyVar = MyVar
'... do more ...
End Sub
Private Sub _Button1_Click(sender As Object, e As EventArgs) _
Handles _Button1.Click
MsgBox("Button1 clicked and I love: " & _MyVar)
End Sub
End Class
_Button1 = Button1 and _MyVar = MyVar This way of doing it seems repetitive and long when the parameters passed to the Class Constructor are many more.
This is actually dependency injection and is usually a good thing. If you end up with too many constructor parameters then it should be a clue that your class is doing too many things and is in violation of SOLID principles.
However, your Class1 should not have dependencies on Form1, it should be the other way around. Your code could much more simply be:
Public Class Class1
Friend Sub DoSomething(ByVal MyVar As String)
MsgBox(MyVar)
End Sub
End Class
Public Class Form1
Public Property class1() As Class1
Sub New()
InitializeComponent()
Me.class1 = New Class1()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
class1.DoSomething("blah, blah")
End Sub
End Class
While I am not demonstrating dependency injection here (for simplicity), you can see Class1 only knows that it can do some work, it doesn't know about the Form at all.
You could subscribe to the button click event in Class1 instead of passing the button into it. It would be cleaner, but still bad design.

How to check which form is used to access current form?

I have 3 forms, namely Form1, Form2 & Form3.
Both Form1 and Form2 are able to access to Form3. However, I am going to give different function to a button in Form3 depends which form is used to access Form3.
Is anyone free to explain it to me how to code should work? Also, if you guys have link that this question previously answered or better concept, I will highly appreciate it. Thanks in advance.
My rough idea:
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If 'user are access from Form1 Then
'Action if user are access from Form1 here
Else
'Action if user are access from Form2 here
End If
End Sub
End Class
The problem with this code
If Me.Owner.Equals(Form1) Then . . .
this way you have tightly coupled objects - Form2 and 1 knows Form3 and Form3 knows 2 and 1. It may be not a problem for you or for now. But in OOP it is a problem, and in the future it may be a problem for your object scalability. Here is OOP approach. Writing from my head, so there could be syntactically incorrect items:
Public Interface IFormCanDoSomething
Sub DoSomething()
ReadOnly Property FormAction As EnumFormActions
End Interface
Public Class Form1
Implements IFormCanDoSomething
ReadOnly Property FormAction As EnumFormActions Implements IFormCanDoSomething.FormAction
Get
Return EnumFormActions.Action1
End Get
End Property
Sub DoSomething() Implements IFormCanDoSomething.DoSomething
Dim f As New Form3(Me)
f.Show()
End Sub
End Class
Public Class Form2
Implements IFormCanDoSomething
ReadOnly Property FormAction As EnumFormActions Implements IFormCanDoSomething.FormAction
Get
Return EnumFormActions.Action2
End Get
End Property
Sub DoSomething() Implements IFormCanDoSomething.DoSomething
Dim f As New Form3(Me)
f.Show()
End Sub
End Class
Public Class Form3
Private _owner As IFormCanDoSomething
Public Sub New(owner As IFormCanDoSomething)
_owner = owner
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If _owner.FormAction = EnumFormActions.Action1 Then
'Action if user needs one thing
ElseIf _owner.FormAction = EnumFormActions.Action2 Then
'Action if user needs another thing here
ElseIf _owner.FormAction = EnumFormActions.Action3 Then
'Action if user needs third thing
End If
End Sub
End Class
So what is the gain here? Look at Button1_Click. Do you see? - now you can have many forms for which Form3 needs to perform Action1, or/and many forms that Form3 needs to perform Action2, etc. This could go farther but for now good enough
Assuming you're using the Show method, pass the Form as the Owner into it:
Form3.Show(Form1)
Then reference the Form3 Owner:
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.Owner.Equals(Form1) Then
'Action if user are access from Form1 here
ElseIf Me.Owner.Equals(Form2) Then
'Action if user are access from Form2 here
End If
End Sub
End Class

Passing values of an object between forms in VB.NET

Here's my problem.
I'm making a project in VB.NET that (currently) exists out of 1 class (let's call it User.vb here) and 2 WinForms (frmDisplay & frmMain).
Let's say User.vb is currently looking like this:
Public Class User
Private mName As String
Public Sub New(ByVal name As String)
Me.Name = name
End Sub
Public Property Name As String
Get
Return mName
End Get
Set(value As String)
mName = value
End Set
End Property
End Class
Let's also say the form frmDisplay is just a form with a textfield txtString and a button btnSend.
Public Class frmDisplay
Dim usr As New User()
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
usr.Name = txtString.Text
frmMain.Show()
Me.Hide()
End Sub
End Class
On the form frmMain I want to reach the value in the property Name that I stored in the class User on the first form.
The basic idea is (I know it doesn't work):
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblStoredString.Text = usr.Name << This is where I'm stuck
End Sub
End Class
I googled my problem and read many posts, but I just can't seem to understand it. Maybe you guys can help me. I am new to VB.NET and WinForm-stuff (about 3 months of exp.), but I have done some programming in the past in C# with webapplications.
Every bit of help is greatly appreciated.
Thanks a lot in advance!
Will there only ever be one User.Name that you are interested in throughout the app?
If yes, then change the class to:
Public Class User
Public Shared Name As String
End Class
Then you can use User.Name from any form (or anywhere in the application) to get/set that value.
Note that you can still wrap the field in a property if you like:
Public Class User
Private Shared _Name As String
Public Shared Property Name As String
Get
Return _Name
End Get
Set(value As String)
If (value.Trim <> "") Then
_Name = value.Trim
End If
End Set
End Property
End Class
My focus is ASP.NET, and I prefer C#, but I'll chime in. There are numerous ways of providing data between the forms. The first one that comes to mind is to use a cache of some kind. The idea is that once the cache is made available to your program, you can add the value to the cache when the button is clicked, and then safely read the value whenever you need it. This can be a static class with a Dictionary, or you can look into using the functionality provided by the System.Web.Caching namespace. http://www.codeproject.com/Articles/8977/Using-Cache-in-Your-WinForms-Applications has an example.
Another way would be to use a shared data source. The concept is similar to the caching, but this would allow you to pass more complex relational data between your forms, assuming your real goal is more complicated than you describe. Here is a walkthrough for that: https://msdn.microsoft.com/en-us/library/ms171925.aspx.
You could be quick and dirty, and write the values to a text file at some location, and then read the values from the second form.
The simplest way is probably to define a custom constructor for the second form, and pass the values you need when you instantiate the second form. This is best suited if the values from the first form can be considered "parameters" to the instance of the second form. Passing a textbox value from one form to another in windows application
Declare the usr variable Friend
Public Class frmDisplay
Friend usr As New User()
It will then be available from the other form
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblStoredString.Text = frmDisplay.usr.Name
End Sub
End Class
It's a quirk of VB.NET that forms are automatically created with a public variable name the same as the class name. That's why you are able to use frmMain without having to create it (e.g. Dim frmMain as New frmMain). You can turn off this behaviour, but it isn't relevant to your problem.
On the other hand, if you want to do it "properly"...
Public Class frmDisplay
Private usr As User
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
usr = New User(txtString.Text)
Dim f As New frmMain(Me, usr)
f.Show()
Me.Hide()
End Sub
End Class
and frmMain...
Public Class frmMain
Private myParent As Form
Private usr As User
Sub New(parent As Form, _usr As User)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
usr = _usr
myParent = parent
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = usr.Name
End Sub
Private Sub frmMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
myParent.Show()
End Sub
End Class
Here we instantiate frmMain and pass the User object to its constructor. We also pass the calling form so we can display it again when frmMain is closed.

VB.Net Inheritance Override

I'm new to VB.Net (I'm from a foxpro background) and have had my head in a book for the last two weeks trying to get started with some of the basics.
I'm trying to master class inheritance and have what I hope is not too much of a challenging question.
I've created a class and compiled it as a DLL. It simply allows me to place a button on a form. I just want to capture the Click event - which I've managed to do but would like to override the inherited code rather than having both fire which seems to be happening at the moment.
I realise I could just double click the control and enter code directly into the MyButton1 click event but wanted to trap this programmatically instead via the handler.
I thought this would just be a case of using the overridable / overrides options.
Here's the code in my class:
Imports System.Windows.Forms
Imports System.Drawing
Public Class MyButton
Inherits Windows.Forms.Button
Sub New()
End Sub
Protected Overridable Sub MyButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click
MsgBox("Base Click")
End Sub
End Class
Then I place the button on my form and name it MyButton1 and in the load event:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler MyButton1.Click, AddressOf Button_Click
End Sub
Private Sub Button_Click()
MsgBox("Actual Click")
End Sub
Problem is, both events fire and I want the option to override / turn off the base event.
I thought I could just add the 'overrides' keyword to the Button_Click routine i.e.:
Private Sub Overrides Button_Click()
but I get an error message Sub Button_Click() cannot be declared 'overrides' because it does not override a sub in a base class
So to clarify - at the moment my code fires both events so I get two messages. I want to be able to turn off / supress the base class event.
Any help would be much appreciated.
I thought this would just be a case of using the overridable /
overrides options.
The fundamental problem here is that you're trying to push a square peg into a round hole.
To override something, you need to have inheritance involved. The derived class is overriding something that was inherited from the base class. For instance, if you inherited from your MyButton class to create a new type of Button called MyButtonDerived, then you could do it as expected:
Public Class MyButton
Inherits Windows.Forms.Button
Protected Overridable Sub MyButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click
MsgBox("Base Click")
End Sub
End Class
Public Class MyButtonDerived
Inherits MyButton
Protected Overrides Sub MyButton_Click(sender As Object, e As EventArgs)
' We don't call the base method...
' MyBase.MyButton_Click(sender, e)
' ... and instead do something else:
MsgBox("Derived Click")
End Sub
End Class
In contrast, when you've placed MyButton onto the Form as in your original problem description, no inheritance has taken place. Instead what you've setup is "object composition"; the form contains an instance of the button (not derived from it). While it may be possible to change what happens when the button is clicked from the form itself, this is not a case that can be solved with OOP, inheritance and overriding.
If MyButton was not designed in such a way that allows the end user to suppress its base functionality, then your options are limited in how you can use it. Here is an example of what it might look like if MyButton was designed to allow the end user to suppress its base click functionality:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyButton1.SuppressDefaultClick = True
End Sub
Private Sub MyButton1_Click(sender As Object, e As EventArgs) Handles MyButton1.Click
MsgBox("Form Click Code")
End Sub
End Class
Public Class MyButton
Inherits Windows.Forms.Button
Private _suppress As Boolean = False
Public Property SuppressDefaultClick As Boolean
Get
Return _suppress
End Get
Set(value As Boolean)
_suppress = value
End Set
End Property
Protected Overridable Sub MyButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click
If Not SuppressDefaultClick Then
MsgBox("Base Click")
End If
End Sub
End Class
If MyButton didn't include a way to suppress its built-in click handler like above then you'd have to resort to other means to solve your problem. In that case you'd have to prevent the button from ever receiving the message that the left mouse button has been clicked at all, and instead implement your own routine. This approach would be a considered a hack, since you are working around the limitations of something and not using it in the way it was originally intended. Here's one way the hack could be implemented:
Public Class Form1
Private WithEvents TMBC As TrapMyButtonClick
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TMBC = New TrapMyButtonClick(Me.MyButton1)
End Sub
Private Sub TMBC_Click(sender As MyButton) Handles TMBC.Click
MsgBox("Form Click Code")
End Sub
Private Class TrapMyButtonClick
Inherits NativeWindow
Private _mb As MyButton
Private Const WM_LBUTTONDOWN As Integer = &H201
Public Event Click(ByVal sender As MyButton)
Public Sub New(ByVal mb As MyButton)
If Not IsNothing(mb) AndAlso mb.IsHandleCreated Then
_mb = mb
Me.AssignHandle(mb.Handle)
End If
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_LBUTTONDOWN
RaiseEvent Click(Me._mb) ' raise our custom even that the form has subscribed to
Exit Sub ' Suppress default behavior
End Select
MyBase.WndProc(m)
End Sub
End Class
End Class
Public Class MyButton
Inherits Windows.Forms.Button
Protected Overridable Sub MyButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click
MsgBox("Base Click")
End Sub
End Class
You are getting 2 messages because you have set 2 different event handlers for the Click event:
The MyButton_Click method defined in your MyButton class.
The Button_Click method set in your AddHandler call on the form.
As noted in a comment above, you need to override the Button.OnClick method in your MyButton class instead of creating a new method:
Imports System.Windows.Forms
Imports System.Drawing
Public Class MyButton
Inherits Windows.Forms.Button
Sub New()
End Sub
' Override the OnClick event defined in "Button" class.
Protected Overrides Sub OnClick(e As System.EventArgs)
' Call the Click event from "Button" class.
MyBase.OnClick(e)
' Some custom events.
MsgBox("MyButton Click")
End Sub
End Class
It might be a good exercise to set breakpoints in the Button_Click and MyButton.OnClick methods so you can see exactly how the stack is created.

Call a tabPage from a separate window form

I have literally just started programming VB today; so forgive my incompetence.
I currently have two window forms, one to navigate though different pages via TabControl (Which is called BusinessSalesPage.vb) and the other is separate (BusinessQuestion.vb). The second window form opens when a button is pressed on BusinessSalesPage.vb.
When the second window opens it has two buttons, I would like the user to be able to click one button which then takes them to the third tab from the TabControl that is on the first window (BusinessSalesPage.vb). Thanks in advance.
Here is my code:
Public Class BusinessQuestion
Inherits System.Windows.Forms.Form
Friend WithEvents mainMenu As System.Windows.Forms.TabControl
Friend WithEvents TabPage3 As System.Windows.Forms.TabPage
Private Sub yesButn_Click(sender As Object, e As EventArgs) Handles yesButn.Click
mainMenu.SelectedTab = TabPage3()
End Sub
End Class
Here is the error I am getting:
NullReferenceException was handled - Object reference not set to an instance of an object.
Edit
Public Class BusinessSalesPage
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mainMenu.SelectedTab = TabPage2
BusinessQuestion.Show()
End Sub
End Class
#LarsTech I tried this as well but mine is having issues with the code. On First Form when I type the mainMenu.SelectedTab = TabPage 1 I get error: "SelectedTab is not a member of System.Windows.Forms.MainMenu and TabPage1 is not declared. It may be inaccessible due to its protection level. On Second Form it doen not find InitializeControls, only InitializeComponents. I am in VS 2010 and here is my code:
First Form:
Public Class WCC
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mainMenu.SelectedTab = TabPage1
Dim MSO As New MSO2014CC(mainMenu:=, TabPage1:=)
MSO.ShowDialog()
End Sub
End Class
Second Form:
Public Class MSO2014CC
Inherits System.Windows.Forms.Form
Friend WithEvents mainMenu As System.Windows.Forms.TabControl
Public Sub New(mainMenu As TabControl, tabPage1 As TabPage)
InitializeComponent()
Me.mainMenu = mainMenu
Me.TabPage1 = tabPage1
End Sub
End Class
You need a reference to the actual instance of the mainForm. You currently don't.
You can try passing a reference in the constructor:
Public Class BusinessQuestion
' code...
Public Sub New(mainMenu As TabControl, tabPage3 As TabPage)
InitializeControls()
Me.mainMenu = mainMenu
Me.TabPage3 = tabPage3
End Sub
' code...
End Class
Your BusinessSalesPage should probably look something like this:
Public Class BusinessSalesPage
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mainMenu.SelectedTab = TabPage2
Dim bq As New BusinessQuestion(mainMenu, TabPage3)
bq.ShowDialog()
End Sub
End Class