Hi again DaniWeb, this time I'd like you to help me with the text alignment options available in a standard text editing application. My problem is, even though I got the application to successfully align the text where I want it, that if I click on every single option (Justify Left, Justify Center, Justify Right), the next time I want to click one, I'll have to double click it in order to make it work. Here's my code. Hope you understand what I'm trying to say. Thank you in advance.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolStripButton14.Checked = True

Private Sub ToolStripButton14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton14.Click
        If ToolStripButton14.Checked = True Then
            ToolStripButton15.Checked = False
            ToolStripButton16.Checked = False
            RichTextBox1.SelectionAlignment = HorizontalAlignment.Left
            ToolStripButton14.CheckOnClick = False
        ElseIf ToolStripButton14.Checked = False Then
            ToolStripButton14.CheckOnClick = True
        End If
    End Sub

    Private Sub ToolStripButton15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton15.Click
        If ToolStripButton15.Checked = True Then
            ToolStripButton14.Checked = False
            ToolStripButton16.Checked = False
            RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
            ToolStripButton15.CheckOnClick = False
        ElseIf ToolStripButton15.Checked = False Then
            ToolStripButton15.CheckOnClick = True
        End If
    End Sub

    Private Sub ToolStripButton16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton16.Click
        If ToolStripButton16.Checked = True Then
            ToolStripButton14.Checked = False
            ToolStripButton15.Checked = False
            RichTextBox1.SelectionAlignment = HorizontalAlignment.Right
            ToolStripButton16.CheckOnClick = False
        ElseIf ToolStripButton16.Checked = False Then
            ToolStripButton16.CheckOnClick = True
        End If
    End Sub

As you can notice, when the form loads the default alignment option is applied (left). If you don't understand what I want to do, please tell me so I can explain it better to you. Nevertheless, I don't think I can explain it better.

Hope this will help you.

void CheckIt(object  button)
        {

            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;

            ((ToolStripButton)button).Checked = !((ToolStripButton)button).Checked;

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
            CheckIt(sender);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
            CheckIt(sender);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
            CheckIt(sender);
        }

Hope this will help you.

void CheckIt(object  button)
        {

            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;

            ((ToolStripButton)button).Checked = !((ToolStripButton)button).Checked;

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
            CheckIt(sender);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
            CheckIt(sender);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
            CheckIt(sender);
        }

But this is C#!! I want it in VB.NET.

Sorry!

Private Sub CheckIt(ByVal button As Object)
    
    toolStripButton1.Checked = False
    toolStripButton2.Checked = False
    toolStripButton3.Checked = False
    
        
    DirectCast(button, ToolStripButton).Checked = Not DirectCast(button, ToolStripButton).Checked
End Sub

Private Sub toolStripButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles toolStripButton1_Click
    richTextBox1.SelectionAlignment = HorizontalAlignment.Left
    CheckIt(sender)
End Sub

Private Sub toolStripButton2_Click(ByVal sender As Object, ByVal e As EventArgs)  Handles toolStripButton2_Click
    richTextBox1.SelectionAlignment = HorizontalAlignment.Center
    CheckIt(sender)
End Sub

Private Sub toolStripButton3_Click(ByVal sender As Object, ByVal e As EventArgs)  Handles toolStripButton3_Click
    richTextBox1.SelectionAlignment = HorizontalAlignment.Right
    CheckIt(sender)
End Sub

Sorry!

Private Sub CheckIt(ByVal button As Object)
    
    toolStripButton1.Checked = False
    toolStripButton2.Checked = False
    toolStripButton3.Checked = False
    
        
    DirectCast(button, ToolStripButton).Checked = Not DirectCast(button, ToolStripButton).Checked
End Sub

Private Sub toolStripButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles toolStripButton1_Click
    richTextBox1.SelectionAlignment = HorizontalAlignment.Left
    CheckIt(sender)
End Sub

Private Sub toolStripButton2_Click(ByVal sender As Object, ByVal e As EventArgs)  Handles toolStripButton2_Click
    richTextBox1.SelectionAlignment = HorizontalAlignment.Center
    CheckIt(sender)
End Sub

Private Sub toolStripButton3_Click(ByVal sender As Object, ByVal e As EventArgs)  Handles toolStripButton3_Click
    richTextBox1.SelectionAlignment = HorizontalAlignment.Right
    CheckIt(sender)
End Sub

Thank you very much!

I have marked this thread as unsolved again because I would like your precious help one more time. So, let's say the user selects some text and aligns it to the center. Then, he clicks on the left of the RichTextBox. How can I determine where the user clicks so the checked button will change to the appropriate one (you know, like MS Word)? Can you please help me on that too? If you do, my application will turn out to be really great.

Handle the SelectionChanged event

Private Sub richTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles richTextBox1_SelectionChanged
    
    toolStripButton1.Checked = False
    toolStripButton2.Checked = False
    toolStripButton3.Checked = False
    
    If richTextBox1.SelectionAlignment = HorizontalAlignment.Left Then
        toolStripButton1.Checked = True
    End If
    If richTextBox1.SelectionAlignment = HorizontalAlignment.Center Then
        toolStripButton2.Checked = True
    End If
    If richTextBox1.SelectionAlignment = HorizontalAlignment.Right Then
        toolStripButton3.Checked = True
        
    End If
End Sub

Handle the SelectionChanged event

Private Sub richTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles richTextBox1_SelectionChanged
    
    toolStripButton1.Checked = False
    toolStripButton2.Checked = False
    toolStripButton3.Checked = False
    
    If richTextBox1.SelectionAlignment = HorizontalAlignment.Left Then
        toolStripButton1.Checked = True
    End If
    If richTextBox1.SelectionAlignment = HorizontalAlignment.Center Then
        toolStripButton2.Checked = True
    End If
    If richTextBox1.SelectionAlignment = HorizontalAlignment.Right Then
        toolStripButton3.Checked = True
        
    End If
End Sub

Thank you so muchhhh! I owe you one man ;)

How to make text justify in richtextbox using vb.net

Hi,

Please create a new dicussion instead to have more luck, because this discussion is 3 years old and Solved.
Thanks in advance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.