Solved

calling sub method vb forms

Posted on 2013-11-12
9
243 Views
Last Modified: 2013-11-20
I have two forms, form1 and form2.  In form1 I am calling a sub method in form 2 but if something happens in the logic within form 2 I would like to break the code (stop execution) and display form 2 to collect some data and continue executing when a button is pressed. Me.show() will not work because it will just return control back over to the caller. me.Showdialog() will also not work. Showdialog acts like it is going to work until the user presses update and a file doesn't get updated.
0
Comment
Question by:MVLIS
  • 7
9 Comments
 
LVL 15

Expert Comment

by:unknown_routine
ID: 39642449
You need to catch and handle exceptions in form 2.

If user breaks  the logic you should:


1:Raise a custom exception
2: Catch that Exception
3: highlight the proper control on form 2 and show proper informative message to the user.
0
 

Author Comment

by:MVLIS
ID: 39642574
Custom exception, meaning a try catch statement for when a value meets a certain condition?
0
 

Author Comment

by:MVLIS
ID: 39642576
I also tried the try catch statement.
0
 

Author Comment

by:MVLIS
ID: 39642622
Try
            If String.IsNullOrEmpty(ErrorCode) Then
                If Process = "oilplug" Then
                    Me.Hide()
                    oilplug.Show()
                ElseIf Process = "oilpluginspect" Then
                    Me.Hide()
                    OilInspect.Show()
                ElseIf Process = "inspect" Then
                    Me.Hide()
                    Inspect.Show()
                End If
            Else
                Me.Show()
                Stop
            End If
        Catch ex As Exception When ErrorCode IsNot String.Empty
            ErrorCode = "made it here"


        End Try
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 39643268
If you need code to stop, then ShowDialog() is indeed the right choice.

To understand how to implement it properly, though, I think we'll need to see more of your processing code to understand the flow and what is happening.
0
 

Author Comment

by:MVLIS
ID: 39651108
I tried the showdialog() and it would act like it is working until you do an action, like press a button it will not go back into debug.

********* Starting form called process selection **************



Public Class ProcessSelection
    Public Process As String
    Public Errorcode As String
    Public Sub InspectBt_Click(sender As System.Object, e As System.EventArgs) Handles InspectBt.Click
        Me.Hide()
        Process = "Inspect"

        Errorcode = Connect.Main2(Process)
        If String.IsNullOrEmpty(Errorcode) Then
        Else
            Connect.ShowDialog()
            'Me.InspectBt_Click(sender, New System.EventArgs())

        End If

    End Sub

    Private Sub OilPlug_Click(sender As System.Object, e As System.EventArgs) Handles OilPlug.Click
        Me.Visible = False
        Process = "OilPlug"
        Connect.Main2(Process)
    End Sub

    Private Sub OilInspect_Click(sender As System.Object, e As System.EventArgs) Handles OilInspect.Click
        Me.Visible = False
        Process = "OilInspect"
        Connect.Main2(Process)
    End Sub

End Class




********************  end of process selection **********************





******************* Beginning of connect *****************************



Imports System.Net.Sockets

Public Class Connect
    Public ErrorCode As String
    Public Process As String
    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Process = "connect"
        Main2(Process)
        Me.Visible = False
        ProcessSelection.Show()
    End Sub

    Public Function Main2(ByVal Process As String)
        InitializeComponent()

        Main()
        TextBox1.Text = ErrorCode

        If String.IsNullOrEmpty(ErrorCode) Then
            If Process = "oilplug" Then
                Me.Hide()
                oilplug.Show()
            ElseIf Process = "oilpluginspect" Then
                Me.Hide()
                OilInspect.Show()
            ElseIf Process = "inspect" Then
                Me.Hide()
                Inspect.Show()
            End If
        Else
            Me.Activate()

        End If

        Return ErrorCode

    End Function


    Sub Main()

        Dim IPAddress As String
        Dim portNumber As String

        Dim path As String = "c:\ipaddress.txt"

        ' Create or overwrite the file.
        'Dim fs As FileStream = File.Create(path)

        ' Add text to the file.
        Dim file As System.IO.StreamReader
        file = My.Computer.FileSystem.OpenTextFileReader(path)
        IPAddress = file.ReadLine()

        If String.IsNullOrEmpty(IPAddress) Then
            ErrorCode = "c:\ipaddress is blank, please fix. " + "Enter ip address and port number below and press update ip"
            file.Close()
            With TextBox1
                .Focus()
                .SelectAll()
            End With
            Exit Sub
        End If

        portNumber = IPAddress.Substring(IPAddress.IndexOf(",") + 1)

        IPAddress = IPAddress.Substring(IPAddress.IndexOf("1"), IPAddress.IndexOf(","))
        file.Close()

        SysConnect(IPAddress, portNumber)

    End Sub
    Sub SysConnect(ByVal IPAddress As String, ByVal portNumber As String)
        Dim PrinterPort As New System.Net.Sockets.TcpClient 'TCPClient to comunicate


        Dim Bv As New Boolean

        Bv = PrinterPort.Connected

        If Bv Then
            Console.WriteLine("Already Connected to: " + IPAddress + " With Port Number: " + portNumber)
            ErrorCode = "Connection has already been established"
            Createtxt(ErrorCode)
        Else
            If My.Computer.Network.Ping(IPAddress) Then
                Try
                    PrinterPort.Connect(IPAddress, portNumber)
                Catch ex As Exception
                    Console.WriteLine("Host would not accept Connection: " + IPAddress + IPAddress + " With Port Number: " + portNumber)
                    ErrorCode = "Host could not connect"
                    Createtxt(ErrorCode)
                End Try

            Else
                Me.ShowDialog()

                MsgBox("Computer is not available.")
                ErrorCode = "Could not ping the computer"
                Createtxt(ErrorCode)
            End If

        End If

    End Sub

    Sub Createtxt(ByVal ErrorCode As String)
        Dim path As String = "c:\Logfile.txt"

        ' Add text to the file.
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(path, True)
        file.WriteLine(ErrorCode)
        file.Close()
    End Sub




    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim Connection As String
        Dim path As String = "c:\ipaddress.txt"
        Dim file As System.IO.StreamWriter

        file = My.Computer.FileSystem.OpenTextFileWriter(path, False)

        Connection = IPAddressTB.Text + "," + PortTB.Text
        file.Write(Connection)
        file.Close()
        TextBox1.Text = "IP Address successfully updated, thank you."

    End Sub


End Class




***************************   End of Connect **************************
0
 

Author Comment

by:MVLIS
ID: 39651232
I have also tried which acts the same. It will display my connect form but it will not execute the code.


 Errorcode = Connect.Main2(Process)
        If String.IsNullOrEmpty(Errorcode) Then
        Else
            'Connect.ShowDialog()
            'Me.InspectBt_Click(sender, New System.EventArgs())
            If Connect.ShowDialog() = DialogResult.OK Then
                ' Form was closed via OK button or similar, continue normally... '
                Me.Show()
            Else
                ' Form was aborted via Cancel, Close, or some other way; do something '
                ' else like quitting the application... '
            End If


        End If
0
 

Accepted Solution

by:
MVLIS earned 0 total points
ID: 39652077
The below finally worked.

Dim customersForm As New Connect()
customersForm.Process = "inspect"
customersForm.Show()
0
 

Author Closing Comment

by:MVLIS
ID: 39662047
really didn't get a full solution from anyone.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now