Need help making a Windows Form App

junfanbl

Member
Joined
Feb 18, 2016
Messages
18
Programming Experience
1-3
Hello, everyone. I have some basic knowledge of VB.Net and some experience here and there making some different apps. Nothing fancy. However, I really need some help putting together a windows form app. I was trying to accomplish a specific task using a console app. I am designing an excel workbook that utilizes some remoting functionality. It is supposed to work based on sheet selection change events, where a selection change is made and it sends data to another program for processing.

The remoting functionality itself works great. The issue is what kind of application do I use to monitor the worksheet for changes, and transmit the data? Initially I wanted to use a console app, that would launch when the workbook is opened, and monitor the sheet for selection changes. That worked okay for a short time. I had to force the console app to stay open using Console.ReadKey() so it would have enough time to respond to the selection change. However, after that, Excel would just freeze up until I closed the console app. So that was no help. The App needs to stay open as long as the workbook is open.

So I thought, why not use a Windows Form App? Ideally, I would like to launch the windows from app from an excel macro when opening the workbook. However that isn't a requirement.
I could launch it outside of Excel completely if it is easier.

My question is, can somebody help me put the code together, and provide instructions on how to compile it in Visual Studio 2012 Professional Edition? What I would like is for the app to run in the background with nothing more than a system tray Icon to turn it off. I have my code below that I used for designing my console application. That is what I need packaged into a windows form app. I really need help with this. It took me months of research just to get the remoting features to work correctly and now that it finally works I don't know how to deploy it. Any help would be greatly appreciated. Thanks for your time!

VB.NET:
Option Strict Off
Option Infer Off
Imports System.IO
Imports System.Data
Imports System.Threading
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Text
Imports System.Diagnostics
Imports System.Collections
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.Utilities
Imports NXOpen.UF
Imports Microsoft.Office.Interop ' import Excel Interop Namespace
Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Windows.Forms

Module remoting_client_test
    Public theSession As Session = DirectCast(Activator.GetObject(GetType(Session), "[URL]http://localhost:6214/NXOpenSession[/URL]"), Session)
    Public ufs As UFSession = DirectCast(Activator.GetObject(GetType(UFSession), "[URL]http://localhost:6214/UFSession[/URL]"), UFSession)
    Public workPart As Part = theSession.Parts.Work
    Public displayPart As Part = theSession.Parts.Display
    Public TagIdentifier As Long
    Public xlProcesses() As Process
    Public row As Long = 3
    Sub Main()
        Dim myForm As New Form1
        myForm.OpenWorkBook()
        Console.WriteLine("Push any key to close")
        Console.ReadKey()
    End Sub

End Module
Public Class Form1
    Private WithEvents xlApp As Excel.Application
    Private WithEvents xlBook As Excel.Workbook
    Private WithEvents xlSheet As Excel.Worksheet
    Public Sub OpenWorkBook()
        'Dim xlProcess As Process
        'Dim Counter As Integer
            If Process.GetProcessesByName("Excel").GetLength(0) > 0 Then
                xlApp = CType(GetObject(, "Excel.Application"), Excel.Application)
                xlBook = xlApp.ActiveWorkbook
            xlSheet = CType(xlBook.Worksheets("NX Data"), Excel.Worksheet)
            xlProcesses = Process.GetProcessesByName("Excel")
        End If
    End Sub
    Public Sub xlSheet_SelectionChange(ByVal Target As Excel.Range) Handles xlSheet.SelectionChange
        Dim theSession As Session = DirectCast(Activator.GetObject(GetType(Session), "[URL]http://localhost:4567/NXOpenSession[/URL]"), Session)
        Dim ufs As UFSession = DirectCast(Activator.GetObject(GetType(UFSession), "[URL]http://localhost:4567/UFSession[/URL]"), UFSession)
        Dim workPart As Part = theSession.Parts.Work
        Dim dp As Part = theSession.Parts.Display
        'Dim theCompName As String
        Dim theCompTag As NXOpen.Tag = NXOpen.Tag.Null
        Dim RowNum As Long
        RowNum = Target.Row
        TagIdentifier = xlApp.Cells(RowNum, 50).value()
        ufs.Disp.SetHighlight(TagIdentifier, 1)
    End Sub
End Class




 
I found an online tutorial on how to put together a windows form app that resides in the system tray (or more presently called the notification area). However, I am having one other issue. The application loads just fine in the notification area but most of the time my code doesn't get the excel process. I am not sure if it matters if I launch my app before launching Excel, or vice versa. My code needs to be able to adapt to any particular workbook that might be open. That is why I used:

xlBook = xlApp.ActiveWorkbook

So I can pick up any workbook that might be open. The sheet is specific, but that doesn't matter. It should be able to pick up the Excel process any time.

What other approach could I use?
 
Back
Top