?
Solved

calling sub method vb forms

Posted on 2013-11-12
9
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 300 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

743 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