How to override internal functions? - vb.net

I want to replace xxxx function in m function in inherit class
how can do this
here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim h As New y5
h.xxxx()
h.m()
End Sub
Class x
Friend Sub xxxx()
MsgBox("JJ")
End Sub
Sub m()
xxxx()
End Sub
End Class
Class y5
Inherits x
Friend Sub xxxx()
MsgBox("KK")
End Sub
End Class

I ~think~ you want KK to appear when h.m() is executed?...instead of JJ?
If that's the case, then you need overrideable and overrides, like this:
Class x
Friend Overridable Sub xxxx()
MsgBox("JJ")
End Sub
Sub m()
xxxx()
End Sub
End Class
Class y5
Inherits x
Friend Overrides Sub xxxx()
MsgBox("KK")
End Sub
End Class

Related

Start a thread for a sub inside a class

What im trying to do is start a thread of a Sub which is inside a class , that will change form1's label1.text . Apparently this code doesnt work .
Public Class Form1
Public Class test
Public Sub doit()
Form1.chan("hello")
End Sub
End Class
Delegate Sub changeit(ByVal test As String)
Public Sub chan(ByVal test As String)
If Me.Label1.InvokeRequired Then
Dim d As New changeit(AddressOf chan)
Me.Invoke(d, {test})
Else
Me.Label1.Text = test
Label1.Refresh()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As New test
Dim thread As New Threading.Thread(AddressOf a.doit)
thread.Start()
End Sub
End Class

Navigation between pages stops working

I'm builing an app for windows iot core on a raspberry pi 3.
It is a scoreboard fot playing billiard.
The app has to work with a numpad only.
The problem is, I want to navigate between pages when the user presses the enter key, but after a couple of times de enter key doesn't work anymore.
I have made a simple code to let you see what i mean.
mainpage and onother two pages. This is the testcode.
The mainpage
Imports Windows.UI.Core
Public NotInheritable Class MainPage
Inherits Page
Public Sub New()
Me.InitializeComponent()
AddHandler Window.Current.CoreWindow.KeyUp, AddressOf CoreWindow_KeyUp
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub
Private Sub CoreWindow_KeyUp(sender As CoreWindow, e As KeyEventArgs)
Dim Value As String = e.VirtualKey
'Enter
If Value = "13" Then
GotoNext()
End If
End Sub
Private Sub GotoNext()
Frame.Navigate(GetType(BlankPage1))
End Sub
End Class
Page 1
Imports Windows.UI.Core
Public NotInheritable Class BlankPage1
Inherits Page
Public Sub New()
Me.InitializeComponent()
AddHandler Window.Current.CoreWindow.KeyUp, AddressOf CoreWindow_KeyUp
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub
Private Sub CoreWindow_KeyUp(sender As CoreWindow, e As KeyEventArgs)
Dim Value As String = e.VirtualKey
'Enter button
If Value = "13" Then
GotoNext()
End If
End Sub
Private Sub GotoNext()
Frame.Navigate(GetType(Blankpage2))
End Sub
End Class
Page 2
Imports Windows.UI.Core
Public NotInheritable Class BlankPage2
Inherits Page
Public Sub New()
Me.InitializeComponent()
AddHandler Window.Current.CoreWindow.KeyUp, AddressOf CoreWindow_KeyUp
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub
Private Sub CoreWindow_KeyUp(sender As CoreWindow, e As KeyEventArgs)
'we halen de virtuele waarde van de ingedrukte knop op
Dim Value As String = e.VirtualKey
'Enter knop
If Value = "13" Then
GotoNext()
End If
End Sub
Private Sub GotoNext()
Frame.Navigate(GetType(MainPage))
End Sub
End Class
Strangely when you hit enter 9 times it stops and I don't know why.

vb.net use function from other form

I am in a situation where i have a single helper class that i re-use on most of my forms. and it happens that my helper class needs to call a function from that form as well
Public Class Form1
Dim hc As HelperClass
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass
End Sub
Private Sub someForm1Sub()
'do something
End Sub
End Class
Public Class Form2
Dim hc As HelperClass
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass
End Sub
Private Sub someForm2Sub()
'do something
End Sub
End Class
Public Class Form3
Dim hc As HelperClass
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass
End Sub
Private Sub someForm3Sub()
'do something
End Sub
End Class
Public Class HelperClass
'i need to call here someForm1Sub(), someForm2Sub(), someForm3Sub()
'but it bases on which form uses this class
End Class
i think that there are many ways to do this. I have read some about delegate function, but i do not know how to pass a delegate to other class. I am using vb.net 2010. Please provide the best way to do this
please reply thanks.
You can pass a delegate to a method using the AddressOf keyword. For example,
Public Sub Test()
' Make Foo() call the Bar() method.
Foo(AddressOf Bar)
End Sub
Public Sub Foo(d As Action)
' Call the delegate.
d()
End Sub
Public Sub Bar()
Console.WriteLine("Bar was called.")
End Sub
But, as you say, there are many ways you could do this. One possible solution which may be a bit nicer than passing delegates (especially if there are a few methods in the form that could be called by the helper class) would be to have Form1, Form2 and Form3 all implement an interface, and have the HelperClass call a method on the interface. An instance of the interface could then be given to HelperClass in its constructor.
For example,
Public Interface IForm
Sub DoStuff()
End Interface
Public Class Form2
Implements IForm
Dim hc As HelperClass
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass(Me)
End Sub
Public Sub DoStuff() Implements IForm.DoStuff
' Do stuff here.
End Sub
End Class
Public Class HelperClass
Private _form As IForm
Public Sub New(form as IForm)
_form = form
End Sub
Public Sub MakeTheFormDoStuff()
_form.DoStuff()
End Sub
End Class

Nested user controls events

I'm trying to understand how I can expose an event in this situation:
Class C has an event E
Class B instance an object of class C
Class A instance an object of class B
When the event is raised from class C, is possible to manage it directly from class A?
Public Class C
Public Event E()
Public Sub Function_of_C()
RaiseEvent E()
End Sub
End Class
Public Class B
Public WithEvents m_C as new C
End Class
Public Class A
Public WithEvents m_B as new B
Private Sub Function_of_A() Handles m_B.m_C.E '<-- Doesn't work
'Do something
End Sub
End Class
As explained in the comment to your question. You need to bubble up the event in class B when it receives from C.
Sub Main
Dim test = new A()
End Sub
Public Class C
Public Event E()
Public Sub Function_of_C()
RaiseEvent E()
End Sub
End Class
Public Class B
Public WithEvents m_C as new C
Public Event E()
Public Sub FnB() Handles m_C.E
Console.WriteLine("In B received Event from C")
RaiseEvent E()
End Sub
Public Sub TriggerC()
Console.WriteLine("In B TriggerC, call C.Function_Of_C")
m_C.Function_Of_C()
End Sub
End Class
Public Class A
Public WithEvents m_B as new B
Private Sub Function_of_A() Handles m_B.E
Console.WriteLine("In A received Event from B")
End Sub
public Sub New()
Console.WriteLine("In A constructor, call B.TriggerC")
m_b.TriggerC()
End Sub
End Class
Just as a supplement to Steve's answer, here's an example of the common design pattern to forward events:
Public Class Doorbell
Public Event Ding As EventHandler
Protected Overridable Sub OnDing(e As EventArgs)
RaiseEvent Ding(Me, e)
End Sub
Friend Sub RaiseDing()
Me.OnDing(EventArgs.Empty)
End Sub
End Class
Public Class House
Implements IDisposable
Public Event DoorbellDing As EventHandler
Public Property Doorbell() As Doorbell
Get
Return Me.m_doorbell
End Get
Set(value As Doorbell)
If (Not value Is Me.m_doorbell) Then
Me.UnookDoorbell()
Me.m_doorbell = value
Me.HookDoorbell()
End If
End Set
End Property
Public Sub Dispose() Implements IDisposable.Dispose
Me.UnookDoorbell()
End Sub
Private Sub HandleDoorbellDing(sender As Object, e As EventArgs)
Me.OnDoorbellDing(e)
End Sub
Private Sub OnDoorbellDing(e As EventArgs)
RaiseEvent DoorbellDing(Me, e)
End Sub
Private Sub HookDoorbell()
If (Not Me.m_doorbell Is Nothing) Then
AddHandler Me.m_doorbell.Ding, AddressOf Me.HandleDoorbellDing
End If
End Sub
Private Sub UnookDoorbell()
If (Not Me.m_doorbell Is Nothing) Then
RemoveHandler Me.m_doorbell.Ding, AddressOf Me.HandleDoorbellDing
End If
End Sub
Private m_doorbell As Doorbell
End Class

vb 2012 module (F as Form)

my module code is
Module print
Sub Pad (F as form)
F.Textbox1.text = "testing"
End sub
End module
is there a way to use the "print.pad" to my 3 form (FrmNew,FrmOld,FrmAdmin) like this
Public Class FrmNew
Private Sub FrmNew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Print.Pad(Me)
End Sub
End Class
Because in VBA it work.
VBA MODULE code (Module name Printt)
Sub Pad(f As Form)
f.Text0 = "testing"
End Sub
VBA FORM Code (Form name Form1)
Private Sub Form_Load()
Call Printt.Pad(Form_Form1)
End Sub
or any way around to do it. tnx in advance...
Module print
Public Sub Pad (ByVal txtControl As TextBox)
txtControl.Text = "testing"
End sub
End module
and call like
Public Class FrmNew
Private Sub FrmNew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Print.Pad(textbox1)
End Sub
End Class
here textbox1 is the control in your FrmNew,In this you want show textbox1.text = "testing"