Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a program where it has two RichTextBox's on the same form. I am trying to make it so the Undo/Redo/Copy/Paste/Cut select all etc.functions run independently of each of the RichTextbox's. Instead of having to run two separate ContextMenus, MenuStrips and toolstrips.
I have duckduckgo for a solution but I'm hitting a wall.

What I have tried:

I have tried If statements, Try Catch Statements, With, Functions, While when. Also trying a different combinations in the the If statements of :

If RichTextBox1.Focus = True And RichTextBox2.Focus = False Then
    If RichTextBox1.CanUndo = True Then
        If RichTextBox1.UndoActionName <> "Undo" Then
            RichTextBox1.Undo()
        End If

    ElseIf RichTextBox2.Focus = True And RichTextBox1.Focus = False Then

        If RichTextBox2.CanUndo = True Then
            If RichTextBox2.UndoActionName <> "Undo" Then
                RichTextBox2.Undo()
            End If
        End If
    End If
End If

or
If RichTextBox1.CanUndo = True And RichTextBox1.TabIndex = 4 Then
    If RichTextBox1.UndoActionName <> "Undo" Then
        RichTextBox1.Undo()
    End If
Else
    If RichTextBox2.CanUndo = True And RichTextBox2.TabIndex = 8 Then
        If RichTextBox2.UndoActionName <> "Undo" Then
            RichTextBox2.Undo()
        End If
    End If
End If


I have also tried running the statements out of the Else. No matter what combination I try either it affects One RichTextbox1 and Not RichTextbox2 or it affects both of them at the same time.
Posted
Updated 5-Mar-23 21:49pm
v2

1 solution

VB
If RichTextBox1.Focused = true Then

   RichTextBox1.Undo()

End If

If RichTextBox2.Focused = true Then

   RichTextBox2.Undo()

End If


it's a basic,
first you check the focus 'on' of the first RTBox, then for the second RTBox.
'Better' is an enemy for the 'good' : 'elseif' are not simple to master.
 
Share this answer
 
v2
Comments
Richard Deeming 6-Mar-23 8:30am    
Assuming Windows Forms, you need to check the Focused property; Focus is a method which will set the focus to the specified control, so both of your tests will pass.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900