-
Aug 15th, 2017, 12:41 AM
#1
Thread Starter
Junior Member
[RESOLVED] Textbox1 to Inherit ContextMenu
I use the following code according to https://docs.microsoft.com/en-us/dot...with-a-control which works fine for windows form1
Code:
Public Class Form1
Private contextMenuStrip1 As ContextMenuStrip
Private toolStripMenuItem1 As ToolStripMenuItem
Private toolStripMenuItem2 As ToolStripMenuItem
'Private toolStripTextBox1 As ToolStripTextBox
Public Sub New()
InitializeComponent()
Me.components = New System.ComponentModel.Container()
'Me.toolStripTextBox1 = New System.Windows.Forms.ToolStripTextBox()
Me.contextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
Me.toolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem()
Me.toolStripMenuItem2 = New System.Windows.Forms.ToolStripMenuItem()
Me.contextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.toolStripMenuItem1, Me.toolStripMenuItem2})
Me.contextMenuStrip1.Name = "contextMenuStrip1"
Me.contextMenuStrip1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.contextMenuStrip1.Size = New System.Drawing.Size(131, 48)
Me.toolStripMenuItem1.Name = "toolStripMenuItem1"
Me.toolStripMenuItem1.Text = "&Cut"
Me.toolStripMenuItem2.Name = "toolStripMenuItem2"
Me.toolStripMenuItem2.Text = "&Copy"
Me.ContextMenuStrip = Me.contextMenuStrip1
End Sub
End Class
When trying to inherit to textbox1 it displays error. How can I link this menu to textbox? Can I use toolStripTextBox or something?
Thanks.
Last edited by NewWorldOrder; Aug 15th, 2017 at 12:56 AM.
-
Aug 15th, 2017, 01:28 AM
#2
Re: Textbox1 to Inherit ContextMenu
I'm not 100% sure what you're asking. Firstly, are you saying that you wrote that code yourself? If so, why? If you didn't do so already, you should have simply added ContextMenuStrip to the form in the designer and then added the desired items to it. If you wouldn't add TextBoxes and Buttons to the form using hand-coding then why do it for ContextMenuStrips?
Secondly, what do you mean by "inherit"? If you mean that you want to add a TextBox to that form and have it display that menu when it's right-clicked then all you need to do is select that menu as the value for the ContextMenuStrip property of the TextBox. If you can't do that already then that may be because the code you've written by hand is slightly different to what the designer would generate. If you let the designer generate the code then you should have no issue.
If you mean something other than that by "inherit" then you'll have to explain. "Inherit" usually means one class being derived from another but that's not possible with a TextBox and a Form and I can't imagine that that's what you would be talking about.
-
Aug 15th, 2017, 01:34 AM
#3
Thread Starter
Junior Member
Re: Textbox1 to Inherit ContextMenu
With this code when you right click inside the form window the menu is displayed correctly. I want this menu to pop up when you right click inside the textbox. I can't set the contextmenu of the textbox in properties, it is stuck to (none). By inherent I mean link this menu for the textbox.
-
Aug 15th, 2017, 01:39 AM
#4
Re: Textbox1 to Inherit ContextMenu
Just as you have set the ContextMenuStrip property of the form, so you would set the ContextMenuStrip property of the TextBox. Note: ContextMenuStrip, NOT ContextMenu. Why would it be any different? If you're doing one in code then you would presumably do the other in code too but I still have to question why you do the first one in code to begin with. Why would you not do it in the designer in the first place? Maybe you didn't know that you could and found a code example when you went looking. Now that you know, you should fix that.
-
Aug 15th, 2017, 01:45 AM
#5
Thread Starter
Junior Member
Re: Textbox1 to Inherit ContextMenu
Yes, ContextMenuStrip. I can't set the property "ContextMenuStrip" of the textbox and I didn't touch anything from the "designer" and I don't know how to do this in "designer".
-
Aug 15th, 2017, 02:14 AM
#6
Re: Textbox1 to Inherit ContextMenu
How can you not know how to do it in the designer? If you can add a TextBox to a form then you can add a ContextMenuStrip. It's exactly the same. Open the Toolbox and double-click the appropriate control or component and it gets added to your form. Select the item in the designer and open the Properties window to configure it. To add a menu item you simply type into the menu. To create a Click event handler for an item you double-click it in the designer, just as you would for a Button.
-
Aug 15th, 2017, 02:16 AM
#7
Thread Starter
Junior Member
Re: Textbox1 to Inherit ContextMenu
Alright, I got it now. For anyone wondering here is the code I used for cut/copy/paste
Code:
Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click
If TextBox1.SelectedText <> "" Then
TextBox1.Cut()
End If
End Sub
Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
If TextBox1.SelectionLength > 0 Then
TextBox1.Copy()
End If
End Sub
Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasteToolStripMenuItem.Click
TextBox1.Paste()
End Sub
-
Aug 15th, 2017, 02:27 AM
#8
Re: [RESOLVED] Textbox1 to Inherit ContextMenu
I haven't tested this to confirm but, if I remember correctly, a TextBox has a default context menu that already contains Clipboard functions. I guess those are a way to learn how to create a menu though. One thing I would suggest is that you look to make that code less tied to a specific TextBox. You could do this:
vb.net Code:
Dim tb = TryCast(myContextMenuStrip.SourceControl, TextBoxBase) If tb IsNot Nothing Then 'Use tb here. End If
or this:
vb.net Code:
Dim tb = TryCast(ActiveControl, TextBoxBase) If tb IsNot Nothing Then 'Use tb here. End If
That will work if you use the same menu for multiple controls and it will work for standard TextBoxes, RichTextBoxes, MaskedTextBoxes or any custom controls that may be derived from TextBoxBase
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|