• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

vb.net code transferred to a separate class..

Joined
Jun 5, 2007
Messages
2,147 (0.35/day)
Location
Metro Manila, Philippines
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F51e
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 @ 1.45v
Video Card(s) Gigabyte Aorus GTX 1080 +100 core / +550 mem
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 4tb WD Blue / 512GB Adata XPG Pro SX8200
Display(s) Acer Predator XB271HU |Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633/G933 Gaming Headset / Corsair H70 Pro Wireless
Power Supply Corsair HX750i
Mouse Logitech G903 and G602 Wireless Gaming | Logitech Proteus Core G502
Keyboard Corsair K70 Cherry Red | Corsair K70 RGB MK.2 Cherry Browns | Akko 3908N TTC Flame Reds
Software Windows 11 Pro
Code:
Sub Go()
        Dim p As New Process
        p.StartInfo.FileName = "ping.exe"
        p.StartInfo.Arguments = "www.bbc.co.uk -t"
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.CreateNoWindow = True
        AddHandler p.OutputDataReceived, AddressOf bleh
        p.Start()
        p.BeginOutputReadLine()
    End Sub

    Sub bleh(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        UpdateTextBox(e.Data)
    End Sub
    Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
    Private Sub UpdateTextBox(ByVal Tex As String)
        If Me.InvokeRequired Then
            Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
            Dim args As Object() = {Tex}
            Me.Invoke(del, args)
        Else
            Form1.tb.Text &= Tex & Environment.NewLine
        End If
    End Sub

This code works well when it's in the forms main code, but when I transfer it to a separate class file it doesn't work. Specifically this set of code The update/output.
Code:
 Private Sub UpdateTextBox(ByVal Tex As String)
        If Me.InvokeRequired Then
            Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
            Dim args As Object() = {Tex}
            Me.Invoke(del, args)
        Else
            Form1.tb.Text &= Tex & Environment.NewLine
        End If
    End Sub

Now when I change it to Me into form1 it compiles ok but the displayed text from ping does not show. And if I used "Me" I get this errors:

Error 1 'InvokeRequired' is not a member of 'txtbox_click_pinger.Class1'.
Error 2 'Invoke' is not a member of 'txtbox_click_pinger.Class1'.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Code:
Public Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
Public Class Form1
    Private _Process As Process
    Public Sub New()
        InitializeComponent()

        _Process = New Process
        _Process.StartInfo.FileName = "ping.exe"
        _Process.StartInfo.Arguments = "www.bbc.co.uk -t"
        _Process.StartInfo.UseShellExecute = False
        _Process.StartInfo.RedirectStandardOutput = True
        _Process.StartInfo.CreateNoWindow = True
        AddHandler _Process.OutputDataReceived, AddressOf UpdateTextBox
        _Process.Start()
        _Process.BeginOutputReadLine()
    End Sub
    Private Sub UpdateTextBox(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Try
            If Me.InvokeRequired() Then
                ' Prevent cross-thread reference error.
                Me.Invoke(New DataReceivedEventHandler(AddressOf UpdateTextBox), New Object() {sender, e})
            Else
                tb.Text &= e.Data & Environment.NewLine
            End If
        Catch ex As Exception
            ' This will catch any errors generated during termination.
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        ' Make sure to stop ping.exe or else it will run indefinitely.
        If (Not _Process Is Nothing) Then _Process.Kill()
    End Sub
End Class
You are basically overcomplicated things by making your own delegate instead of using the delegate that already exists for the event.
 
Joined
Jun 5, 2007
Messages
2,147 (0.35/day)
Location
Metro Manila, Philippines
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F51e
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 @ 1.45v
Video Card(s) Gigabyte Aorus GTX 1080 +100 core / +550 mem
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 4tb WD Blue / 512GB Adata XPG Pro SX8200
Display(s) Acer Predator XB271HU |Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633/G933 Gaming Headset / Corsair H70 Pro Wireless
Power Supply Corsair HX750i
Mouse Logitech G903 and G602 Wireless Gaming | Logitech Proteus Core G502
Keyboard Corsair K70 Cherry Red | Corsair K70 RGB MK.2 Cherry Browns | Akko 3908N TTC Flame Reds
Software Windows 11 Pro
That seems to have done the trick... I've made it as form2.. So form1 only acts as a multifunction launcher of sorts. Btw is it possible to make a protection code so that it would Only allow 1 instance of form2 running?

Thanks man! You've really guided me alot in the process of vb learning.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
If you had only one place holder for Form2 and always used that placeholder, only once instance of Form2 could exist.

Dim child As Form2

You could also just use .Show and .Hide on that form so you only create a new instance of it once.
 
Joined
Jun 5, 2007
Messages
2,147 (0.35/day)
Location
Metro Manila, Philippines
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F51e
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 @ 1.45v
Video Card(s) Gigabyte Aorus GTX 1080 +100 core / +550 mem
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 4tb WD Blue / 512GB Adata XPG Pro SX8200
Display(s) Acer Predator XB271HU |Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633/G933 Gaming Headset / Corsair H70 Pro Wireless
Power Supply Corsair HX750i
Mouse Logitech G903 and G602 Wireless Gaming | Logitech Proteus Core G502
Keyboard Corsair K70 Cherry Red | Corsair K70 RGB MK.2 Cherry Browns | Akko 3908N TTC Flame Reds
Software Windows 11 Pro
If you had only one place holder for Form2 and always used that placeholder, only once instance of Form2 could exist.

Dim child As Form2

You could also just use .Show and .Hide on that form so you only create a new instance of it once.

Ok here's what is happening now, just want to make sure I understand what you've said..

When I click ping button on form1... It shows form2 and initiates it. I've put a button on form2 with "Hide" (form2.hide). So it's still running in the background right? Now form1 has the "ping" and "show form2" buttons...

"Show Form2" button is = form2.show()

uhm I've confused my self lol... That too is the command for "Ping" button.
Is there another way to code this so THE ping command wouldn't actually start off the bat when form2 is shown?

Sorry a bit confused.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Yes, the only way the Form gets killed for good is if you call .Close() or reallocate the memory (e.g. child = Nothing).

Just remember that if you are going to run an infinite loop on ping.exe you should start/stop ping.exe when the VisbleChanged event is raised.
 
Joined
Jun 5, 2007
Messages
2,147 (0.35/day)
Location
Metro Manila, Philippines
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F51e
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 @ 1.45v
Video Card(s) Gigabyte Aorus GTX 1080 +100 core / +550 mem
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 4tb WD Blue / 512GB Adata XPG Pro SX8200
Display(s) Acer Predator XB271HU |Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633/G933 Gaming Headset / Corsair H70 Pro Wireless
Power Supply Corsair HX750i
Mouse Logitech G903 and G602 Wireless Gaming | Logitech Proteus Core G502
Keyboard Corsair K70 Cherry Red | Corsair K70 RGB MK.2 Cherry Browns | Akko 3908N TTC Flame Reds
Software Windows 11 Pro
Have made it work to make use a single instance by doing this:
Code:
 Public Sub New()
        [B][COLOR="Red"]MyBase.New()[/COLOR][/B]
        InitializeComponent()
        _p = New Process
        _p.StartInfo.FileName = "ping.exe"
        _p.StartInfo.Arguments = "www.bbc.co.uk -t"
        _p.StartInfo.UseShellExecute = False
        _p.StartInfo.RedirectStandardOutput = True
        _p.StartInfo.CreateNoWindow = True
        AddHandler _p.OutputDataReceived, AddressOf bleh
        _p.Start()
        _p.BeginOutputReadLine()
    End Sub
        InitializeComponent()
        _p = New Process
        _p.StartInfo.FileName = "ping.exe"
        _p.StartInfo.Arguments = "www.bbc.co.uk -t"
        _p.StartInfo.UseShellExecute = False
        _p.StartInfo.RedirectStandardOutput = True
        _p.StartInfo.CreateNoWindow = True
        AddHandler _p.OutputDataReceived, AddressOf bleh
        _p.Start()
        _p.BeginOutputReadLine()
    End Sub

And then added this to form 2 along with the code above:
Code:
  Private Shared _Instance As Form2 = Nothing
    Public Shared Function Instance() As Form2
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New Form2
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function

Code for button initiate ping on form1 is:
Code:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyForm2 = Form2.Instance
        MyForm2.Show()
    End Sub
^still gonna edit this further to be a 1 click start/stop button with error catching (try/catch)

Seems working perfectly now.. Also have made a show/hide check box with a try/catch combination so when there is no presence of the form2 it would not crash..

Thank you!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
MyBase.New() is a repetition of Public Sub New(). You'd only have to use MyBase.New if you were using a constructor other than New().


MyBase.New(), in a Form, is the same as calling New Form().
 
Joined
Jun 5, 2007
Messages
2,147 (0.35/day)
Location
Metro Manila, Philippines
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F51e
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 @ 1.45v
Video Card(s) Gigabyte Aorus GTX 1080 +100 core / +550 mem
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 4tb WD Blue / 512GB Adata XPG Pro SX8200
Display(s) Acer Predator XB271HU |Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633/G933 Gaming Headset / Corsair H70 Pro Wireless
Power Supply Corsair HX750i
Mouse Logitech G903 and G602 Wireless Gaming | Logitech Proteus Core G502
Keyboard Corsair K70 Cherry Red | Corsair K70 RGB MK.2 Cherry Browns | Akko 3908N TTC Flame Reds
Software Windows 11 Pro
MyBase.New() is a repetition of Public Sub New(). You'd only have to use MyBase.New if you were using a constructor other than New().


MyBase.New(), in a Form, is the same as calling New Form().

Corrected and noted, works whichever is used. Thanks!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Ehm, my point is it doesn't need it--it is implied. It is only required if you are doing inheritance with a non-default base constructor. For example, if you have two classes:

Code:
Public Class Vehicle
  Public Name As String

  Public Sub New()
  End Sub
  Public Sub New(ByVal vehiclename As String)
  End Sub
End Class

Public Class Car Extends Vehicle
  Public Sub New()
  End Sub
End Class
As is, New Car would use New Vehicle(); however, if we changed Car to...
Code:
Public Class Car Extends Vehicle
  Public Sub New()
    MyBase.New("Mustang")
  End Sub
End Class
It would call New Vehicle("Mustang"). That's not the best example but the point is that MyBase is really only useful if you are dealing with inheritance.
 
Last edited:
Top