5 Replies - 221 Views - Last Post: 09 December 2018 - 05:57 AM Rate Topic: -----

#1 BigD3v3loper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 18

Textbox support for "Open with"

Posted 07 December 2018 - 05:49 AM

Hi. I'm re-creating notepad and I added basically everything, but I have problem with something. When you have a "txt" file, you can right click and select "Open with" to open the txt file with the program you want (Notepad ++, ms word, etc), but when I select my own program, the textbox will be empty. How can I create this? Does it require creating a registry key? I'd appreciate if someone would tell me this because I couldn't find this anywhere :)

Is This A Good Question/Topic? 0
  • +

Replies To: Textbox support for "Open with"

#2 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Textbox support for "Open with"

Posted 07 December 2018 - 07:40 AM

Are you trying to do something like this?

Attached Image

If so, you are looking for a "contextmenustrip". To show your context menu over the default, right clicking on the textbox shows the default popup box. You may need to manipulate the windows API call to not show the default contextmenustrip. But that's not complicated. Microsoft made it easy and its just a matter of setting a property on the textbox control ::

Attached Image

Or put this in your form load event ::

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.ShortcutsEnabled = False
    End Sub


Then in your TextBox properties, bind your contextmenu to the control by doing this ::

Attached Image

Or by placing this code in your form load event also ::

TextBox1.ContextMenuStrip = ContextMenuStrip


You're not done yet, you still need the coordinates from your mouse pointer for the context menu to open where it is clicked. To do this, you have a few options... You can use e As MouseEventArgs and acquire the mouse position like so ::

    Private Sub TextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseUp
        If MouseButtons.Right = MouseButtons.Right Then 'Check if mouse button right click is clicked
            ContextMenuStrip.Show(TextBox1, e.Location) 'Then show the ContextMenuStrip by passing your TextBox control and mouse event arguments for the mouse position coordinates. 
        End If
    End Sub

Or ::
        If MouseButtons.Right = MouseButtons.Right Then
            ContextMenuStrip.Show(TextBox1, e.X, e.Y)
        End If


Hope this helps. Remember to hit the green + if you find posts helpful.

Further, you also need to acquire the address of NotePad ++ and then when you right click on your address in your textbox, you can pass that address to the NotePad ++ launch properties for process info, something to the effect of ::

        Dim pStartInfo As New ProcessStartInfo
        Dim file As String = TextBox1.Text
        pStartInfo.FileName = "Address to NotePadd ++"
        pStartInfo.Arguments = file
        Process.Start(pStartInfo)


Get installed processes by searching WMI

This post has been edited by Sheepings: 07 December 2018 - 07:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 BigD3v3loper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 18

Re: Textbox support for "Open with"

Posted 07 December 2018 - 09:11 AM

Thanks, I really appreciate it :D
Was This Post Helpful? 0
  • +
  • -

#4 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Textbox support for "Open with"

Posted 07 December 2018 - 09:18 AM

You're welcome. Let me know if you get stuck along the way.
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1530
  • View blog
  • Posts: 3,856
  • Joined: 01-February 13

Re: Textbox support for "Open with"

Posted 09 December 2018 - 04:07 AM

@ BigD3v3loper,
If I understand your question correctly, you don't want a ContextMenuStrip that shows when you right click your TextBox, you want to be able to right click on a text file in the explorer window and have your application in the 'Open With' menu.

If that is correct, then yes it requires specific registry keys to be created. That is a little tricky and would also require your application to be run with administrator rights to create the registry keys. However, it is not recommended to run your app with admin rights unless you are building an 'administrator tool' of some sort. You are better off just letting the user right click on a text file and use the (Open With -> Choose Default Program) option to do this on their computer.


@ Sheepings,

If you bind a ContextMenuStrip to a TextBox, you do not need to use the MouseUp event, nor do you need to use the Show method to show the ContextMenuStrip. That will all be done internally and will show wherever you right click in the TextBox. All you have to do is set the TextBox.ContextMenuStrip property to the ContextMenuStrip that you created.

Also, even though you do not need to use the MouseUp event for this, the below line is not correct, MouseButtons.Right is always going to equal MouseButtons.Right. So, inside the MouseUp event sub you would just use the 'MouseButtons' property of the MouseEventArgs that is passed to the event sub as shown in the second code block below. 8)
If MouseButtons.Right = MouseButtons.Right Then

Should be...
If e.Button = MouseButtons.Right Then

Was This Post Helpful? 0
  • +
  • -

#6 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Textbox support for "Open with"

Posted 09 December 2018 - 05:57 AM

View PostIronRazer, on 09 December 2018 - 11:07 AM, said:

@ Sheepings,

If you bind a ContextMenuStrip to a TextBox, you do not need to use the MouseUp event, nor do you need to use the Show method to show the ContextMenuStrip. That will all be done internally and will show wherever you right click in the TextBox.

I did it the way I did as the menu was opening up away from the mouse pointer for me. I haven't looked at it since, but I actually didn't know that giving the contextmenustrip to the textbox in the designer is meant to provide that inherited mouse functionality by default.

It's true, you learn something new everyday. :)

View PostIronRazer, on 09 December 2018 - 11:07 AM, said:

If MouseButtons.Right = MouseButtons.Right Then

Should be...
If e.Button = MouseButtons.Right Then

Come on Iron, that's basics one o one, obviously you knew i knew that but I don't know how that ended up like that :lol: I even wrote how as I explained it ::

You can use e As MouseEventArgs and acquire the mouse position like so

This post has been edited by Sheepings: 09 December 2018 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1