Solved

calling sub method vb forms

Posted on 2013-11-12
9
245 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

863 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

17 Experts available now in Live!

Get 1:1 Help Now