Visual Basic .NET: Difference between revisions

catching command-line arguments on load event and on StartupNextInstance event (for single-instance apps)
m (improving key search)
(catching command-line arguments on load event and on StartupNextInstance event (for single-instance apps))
Line 9: Line 9:
: Q: How can I use functions from another class?
: Q: How can I use functions from another class?
: A: '''First condition:''' The function must be a "Shared" one. For example "Shared Function GetApp()". '''Second condition:''' in the ''target code'', the class must be written beforehand (for example "Oni.GetApp" whereby "Oni" is the ''source class's'' name).
: A: '''First condition:''' The function must be a "Shared" one. For example "Shared Function GetApp()". '''Second condition:''' in the ''target code'', the class must be written beforehand (for example "Oni.GetApp" whereby "Oni" is the ''source class's'' name).
===Command-line functions within a forms application===
Since you have to watch out for multiple events, the argument processing should happen in a separate function so the code can be easily re-used.
  ' Form1.vb (main form)
  Public Function processArgs(ByVal args() As String)
      For Each a In args
          MsgBox(a)
      Next
  End Function
; Catch arguments from form starts event
' Form1.vb (main form)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  If My.Application.CommandLineArgs.Count > 0 Then
      processArgs(My.Application.CommandLineArgs.ToArray)
  End If
 
  ' [...]
 
end sub
; Catch arguments when a single-instance application is already running
The load event can't be triggered if the application can't be opened more than once at a time. This is named a "single-instance application" and will trigger an StartupNextInstance event. Read [http://stackoverflow.com/questions/28831765/pass-command-line-to-first-instance-of-a-single-instance-app here] for details.
'ApplicationEvents.vb
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
    Partial Friend Class MyApplication
        Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            Dim f = Application.MainForm
            If f.GetType Is GetType(Form1) Then
                CType(f, Form1).processArgs(e.CommandLine.ToArray)
            End If
        End Sub
    End Class
End Namespace


===Registry===
===Registry===
Line 32: Line 80:


==Code snippets==
==Code snippets==
===Registry===
; Check if key exists
Private Function getXsiFromRegistry() As String()
        Dim oRegKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\\Softimage\\SOFTIMAGE Application")
        Dim XsiPathInReg As String = ""
        If oRegKey Is Nothing Then
            XsiPathInReg = ""
        Else
            Dim Arr = oRegKey.GetSubKeyNames
            For Each n In Arr
                If InStr(n, "Softimage_Mod_Tool_7.5|Application|bin") > 0 Then
                    XsiPathInReg = Replace(n, "|", "\")
                    If Directory.Exists(XsiPathInReg) = False Then
                        XsiPathInReg = ""
                    End If
                End If
            Next
            oRegKey.Close()
        End If
        If XsiPathInReg = "" Then
            Return New String() {"", "notvalid"}
        Else
            Return New String() {XsiPathInReg, "valid"}
        End If
    End Function
    If getXsiFromRegistry(1) = "valid" Then
        MsgBox(getXsiFromRegistry(0))
    End If
: example of a valid, returned path: C:\Softimage\Softimage_Mod_Tool_7.5\Application\bin
==Code snippets (old)==
Common functions that are shared among VB.net projects should have their own class.
Common functions that are shared among VB.net projects should have their own class.


Line 176: Line 260:
         Next
         Next
     End Sub
     End Sub
==Code snippets (v2)==
===Registry===
; Check if key exists
Private Function getXsiFromRegistry() As String()
        Dim oRegKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\\Softimage\\SOFTIMAGE Application")
        Dim XsiPathInReg As String = ""
        If oRegKey Is Nothing Then
            XsiPathInReg = ""
        Else
            Dim Arr = oRegKey.GetSubKeyNames
            For Each n In Arr
                If InStr(n, "Softimage_Mod_Tool_7.5|Application|bin") > 0 Then
                    XsiPathInReg = Replace(n, "|", "\")
                    If Directory.Exists(XsiPathInReg) = False Then
                        XsiPathInReg = ""
                    End If
                End If
            Next
            oRegKey.Close()
        End If
        If XsiPathInReg = "" Then
            Return New String() {"", "notvalid"}
        Else
            Return New String() {XsiPathInReg, "valid"}
        End If
    End Function
    If getXsiFromRegistry(1) = "valid" Then
        MsgBox(getXsiFromRegistry(0))
    End If
: example of a valid, returned path: C:\Softimage\Softimage_Mod_Tool_7.5\Application\bin






[[Category:Modding tutorials]]
[[Category:Modding tutorials]]
8,306

edits