Thursday, July 9, 2009

Visual Basic code help. I sometimes see, when I look at a shortcut that it points to, "C:\Test.exe" - console

I used "C:\Test.exe" - console" as an example. I wnat to know how to do that kind of thing using Visual Basic, I want my program to react differently when they are extra things like that. So for example:





Private Sub Button1_Click





Dim teststring As String





If teststring = "yes" Then


Me.Close


Else


Form2.Show





End Sub





Just suppose that was the code, if I just ran the program when I clicked Button1, it would take me to the next form. but if I had a shortcut which points to:





"C:\test.exe" teststring = "yes"





I would like it to just quit the program once the button is clicked, as stated in the code.





I know this is possible because I have evidence, but how?





PS: I have had answers in the past which are like:


"Why would you want to just quit the program straight away"


But I will not, this is just a sample so I can get the message across.





Thank you!

Visual Basic code help. I sometimes see, when I look at a shortcut that it points to, "C:\Test.exe" - console
You need to capture the Command line arguments. In VB.NET you can use this code:





Dim strArgs() As String = {"", "", ""}, n As Integer


For Each strArg As String In My.Application.CommandLineArgs


strArgs(n) = strArg


n += 1


Debug.Print(strArg)


Next
Reply:in VB6 you use the Command() method, then parse it depending on which (if any) delimiters you used





eg





Dim bTesting As Boolean





Private Sub Form_Load()





Dim Cmd As String


Dim MyDelimitor


Dim ArrCmd() As String


Dim Ptr As Integer





bTesting = False


MyDelimitor = "|" ' specify a command line delimiter


Cmd = Command() ' grab the command line


ArrCmd = Split(Cmd, MyDelimitor) ' parse the command line if more than 1 command line arguement





' do something with them


For Ptr = 0 To UBound(ArrCmd)


Debug.Print ArrCmd(Ptr)


' here you might check your testing mode flag


' in this case, he have the shortcut "c:\test.exe -debug"


bTesting = CBool(UCase$(ArrCmd(Ptr) = "DEBUG"))


Next Ptr





End Sub





Private Sub Command1_Click()





If bTesting = True Then


Unload Me


End If
Reply:I am not exactly sure about this but to make testring="yes" the Dim testring As String code must be outside the Private Sub Button1_Click. Place it at the top of the code window under the heading Option Explicit. You will then have to make testring="yes" or use the code Path=C:\test.exe in another sub


No comments:

Post a Comment