Right click shell extention. 2 questions

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
Hello. Just putting final touches to my app and i just wanted to know two things.

The first thing is this is the code i use to run my app via right click(it hosts an image)

VB.NET:
            'Lets set the application dir for are context menu
            Dim AppPath As String = Application.ExecutablePath & " " + Chr(34) + "%1" + Chr(34)
            '
            'lets set subkeys
            My.Computer.Registry.ClassesRoot.CreateSubKey("jpegfile\shell\Simple Host")
            My.Computer.Registry.ClassesRoot.CreateSubKey("jpegfile\shell\Simple Host\command")
            '
            'Lets set string value
            My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\jpegfile\shell\Simple Host\command", "", AppPath)

which all works fine but if the application is already open. it opens another instance of the app. is there a way to stop this? Not a mjor issue but still.

The second is this is the code i use to get the image to host.

VB.NET:
            '************** Right click context menu *****************
            '
            'User has chosen to host a file via right click....
            '

            For Each arg As String In Environment.GetCommandLineArgs()

                'Display argument
                txtHiddenArgument.Text = arg

                Dim Dir As String = txtHiddenArgument.Text

                Dim fi As New System.IO.FileInfo(Dir)

                txtSplit.Text = fi.Name

                If fi.Extension.ToLower = ".jpg" Then

                    txtFilePath.Text = txtHiddenArgument.Text

                    txtSendTray.Text = "1"

                    Me.Size = New Size(764, 416)
                    chkPre.Checked = True

                    Dim Pre As Image = _
                    Image.FromFile(txtHiddenArgument.Text)
                    PictureBox1.Image = Pre

                    txtHiddenArgument.Text = ""
                    Call UploadPicture()


                ElseIf fi.Extension.ToLower = ".jpeg" Then

                    txtFilePath.Text = txtHiddenArgument.Text

                    txtSendTray.Text = "1"

                    Me.Size = New Size(764, 416)
                    chkPre.Checked = True

                    Dim Pre As Image = _
                    Image.FromFile(txtHiddenArgument.Text)
                    PictureBox1.Image = Pre

                    txtHiddenArgument.Text = ""
                    Call UploadPicture()


                ElseIf fi.Extension.ToLower = ".gif" Then

                    txtFilePath.Text = txtHiddenArgument.Text

                    txtSendTray.Text = "1"

                    Me.Size = New Size(764, 416)
                    chkPre.Checked = True

                    Dim Pre As Image = _
                    Image.FromFile(txtHiddenArgument.Text)
                    PictureBox1.Image = Pre

                    txtHiddenArgument.Text = ""
                    Call UploadPicture()


                ElseIf fi.Extension.ToLower = ".tif" Then

                    txtFilePath.Text = txtHiddenArgument.Text

                    txtSendTray.Text = "1"

                    Me.Size = New Size(764, 416)
                    chkPre.Checked = True

                    Dim Pre As Image = _
                    Image.FromFile(txtHiddenArgument.Text)
                    PictureBox1.Image = Pre

                    txtHiddenArgument.Text = ""
                    Call UploadPicture()


                ElseIf fi.Extension.ToLower = ".bmp" Then

                    txtFilePath.Text = txtHiddenArgument.Text

                    txtSendTray.Text = "1"

                    Me.Size = New Size(764, 416)
                    chkPre.Checked = True

                    Dim Pre As Image = _
                    Image.FromFile(txtHiddenArgument.Text)
                    PictureBox1.Image = Pre

                    txtHiddenArgument.Text = ""
                    Call UploadPicture()

                ElseIf fi.Extension.ToLower = ".png" Then

                    txtFilePath.Text = txtHiddenArgument.Text

                    txtSendTray.Text = "1"

                    Me.Size = New Size(764, 416)
                    chkPre.Checked = True

                    Dim Pre As Image = _
                    Image.FromFile(txtHiddenArgument.Text)
                    PictureBox1.Image = Pre

                    txtHiddenArgument.Text = ""
                    Call UploadPicture()

                End If

            Next arg

is this the best way? It seems a biot messy to me.

thankis
 
but if the application is already open. it opens another instance of the app. is there a way to stop this?
In Application tab of project properties check "make single instance". From same page go to Application Events and handle the Startup and StartupNextInstance events.
is this the best way? It seems a biot messy to me.
No, there are several ways that are better for this using basic code constructs.
  • With a Select Case you can match multiple cases:
    VB.NET:
    Select Case fi.Extension.ToLower
       Case ".jpg", ".jpeg", ".tif", ".bmp", ".gif", ".png"
          'code to apply
    End Select
  • or using combined expressions:
    VB.NET:
    If fi.Extension.ToLower = ".jpg" _
    OrElse fi.Extension.ToLower = ".jpg" etc... Then
       'code to apply
  • or if there was different parameters to be using the code:
    VB.NET:
    If fi.Extension.ToLower = ".jpg" Then
       HandleImage("something")
    ElseIf fi.Extension.ToLower = ".jpeg" Then
       HandleImage("something else")
    You would then have a HandleImage method defined that used the parameter value for something, apart from that the same code applied.
Always look out for possibilities of code reuse and refactor your code.
 
as always thank you very much john.

Just one thing. It works perfectly just if the application is already open and i right click a image to host it now does not pick up the event.

VB.NET:
            For Each arg As String In Environment.GetCommandLineArgs()

                'Display argument
                txtHiddenArgument.Text = arg

                Dim Dir As String = txtHiddenArgument.Text

                Dim fi As New System.IO.FileInfo(Dir)

                txtSplit.Text = fi.Name



                Select Case fi.Extension.ToLower
                    Case ".jpg", ".jpeg", ".tif", ".bmp", ".gif", ".png"
                        txtFilePath.Text = txtHiddenArgument.Text

                        '  txtSendTray.Text = "1"

                        Me.Size = New Size(764, 416)
                        chkPre.Checked = True

                        Dim Pre As Image = _
                        Image.FromFile(txtHiddenArgument.Text)
                        PictureBox1.Image = Pre

                        txtHiddenArgument.Text = ""
                        Call UploadPicture()
                End Select


            Next arg

any idea how i can work round this? I imagine it does this as its refusing to load a new instance of the app. hmm toughie.
 
Last edited:
JohnH said:
From same page go to Application Events and handle the Startup and StartupNextInstance events.
Did you do this? Both these also provides commandline arguments in the e.CommandLine parameter.
 
Hello john. I have never had to do this and never heard about these events. I am reading this [ame=http://www.vbforums.com/showthread.php?t=398309]VB.NET: Single Instance Application - VBForums[/ame] but its not quite what i want.

do you know of a tutorial i can read up on how to handle these events to suit me? google is not beiung to great for me
 
My.Application.StartupNextInstance Event
This link was also given in previous article I gave you from the help library. When you read this article it has several other links, click them to read these articles if you need further help about what is explained.
 
Right i have spent the last hour reading it. I currently bring my form into focus if the instance is already running

VB.NET:
        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, _
              ByVal e As StartupNextInstanceEventArgs) _
              Handles Me.StartupNextInstance
            e.BringToForeground = True
            MainForm.Visible = True
            MainForm.WindowState = FormWindowState.Normal
            MainForm.BringToFront()
            MainForm.Activate()
            For Each arg As String In Application.CommandLineArgs

                Dim Dir As String = arg
                Dim fi As New System.IO.FileInfo(Dir)
                Dim sTemp As String = fi.Name

                Select Case fi.Extension.ToLower
                    Case ".jpg", ".jpeg", ".tif", ".bmp", ".gif", ".png"
                        ' CALL UPLOADHEREHOW????
                End Select

            Next arg

            Application.DoEvents()
        End Sub

But i am struggling still to capture the 'arg' event onload.

"For Each arg As String In Environment.GetCommandLineArgs()"
 
Last edited:
Right, after literally hours i have come up with...

VB.NET:
            If e.CommandLine.Count > 0 Then
                For Each arg As String In e.CommandLine
                    'Process the commandline args
                    Dim Dir As String = arg
                    Dim fi As New System.IO.FileInfo(Dir)
                    Dim sTemp As String = fi.Name
                    MessageBox.Show(sTemp)
                Next
Which gives me the file name.

My question is how to i use this varible to transfer it accross and my application to upload?
 
Last edited:
Johnson said:
My question is how to i use this varible to transfer it accross and my application to upload?
For example use the default form instance and pass the filename to one of it's methods/properties:
VB.NET:
SomeForm.ShowImage(filename)
 
For example use the default form instance and pass the filename to one of it's methods/properties:
VB.NET:
SomeForm.ShowImage(filename)


Am i being thick again john lol. But i dont actually want to show an image. I just need the images location whihc i get with Dim Dir As String = arg

set txtFilePath.Text as Dir

Then i need to call my sub Private Sub UploadPicture()


i really have no hair left lol
 
It's not any different than what's already been said.
VB.NET:
SomeForm.txtFilePath.Text = filename
SomeForm.SomeMethod()
 
Now some complaining about your complaints...
Johnson said:
Right i have spent the last hour reading it.
StartupNextInstance:
For Each arg As String In Application.CommandLineArgs
Johnson said:
Right, after literally hours i have come up with...
StartupNextInstance:
If e.CommandLine.Count > 0 Then
For Each arg As String In e.CommandLine
It really took you hours to read 20 sentences of text and "come up with" this code given in code sample in article? You have to read more carefully.
Also, checking 'If Count > 0' has no effect in your code and should be removed.
help said:
You must use the CommandLine property of the e parameter to access the arguments for subsequent attempts to start a single-instance application.
If you can't understand English too well you should have a look at very top menu on help library pages, there you can select many other languages of the help library.
 
Back
Top