Question Control context menues

QADUDE

Member
Joined
Dec 30, 2009
Messages
12
Programming Experience
3-5
Hi
I have searched the forum for threads looking at control context menus and found none. An application I am developing uses rich text boxes and I cannot see how to invoke the context menus. I need cut, past, copy, bold, italics etc.

Can anyone help?
 
I'm not sure if this is exactly what you were looking for, but here is goes:

There are several built in functions for the RichTextBox, such as:

VB.NET:
'Cut
RichTextBox1.Cut()

'Copy
RichTextBox1.Copy()

'Paste
If RichTextBox1.CanPaste(DataFormats.Text) Then
 RichTextBox1.Paste()
End If

'Undo
If RichTextBox1.CanUndo Then
 RichTextBox1.Undo()
End If

'Redo
If RichTextBox1.CanRedo Then
 RichTextBox1.Redo()
End If

'Bold
If Not RichTextBox1.SelectionLength = 0 Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
End If

'Italics
If Not RichTextBox1.SelectionLength = 0 Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Italic)
End If

'Underline
If Not RichTextBox1.SelectionLength = 0 Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Underline)
End If

'Normal
If Not RichTextBox1.SelectionLength = 0 Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Normal)
End If

Hope this is what you were looking for,
-Josh
 
I will just make one comment on the code to set the font style. As Joshdbr has posted it, that code would remove other styles when one is applied, e.g. applying Bold would remove Underline. If you want to apply a style without affecting other styles then do like this:
VB.NET:
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Or FontStyle.Bold)
To remove a style without affecting others do like this:
VB.NET:
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style And Not FontStyle.Bold)
to toggle, do this:
VB.NET:
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Bold)
 
Back
Top