none
Center text in a richtextbox RRS feed

  • Question

  • I ,posted this in the wrong forum category I believe

    Here we go again.

    Can I center text somehow in a richtextbox?  Example,  its MS San Serif, size 10, unit is point, width is 250.   ie "Center This"

    I cannot solve this ...

    Thanks for any help you can provide

    cyberal

    Saturday, July 1, 2017 12:50 AM

Answers

All replies

  • Can I center text somehow in a richtextbox?

    Select the text that you want aligned, and use the HorizontalAlignment method with the selection.   See:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionalignment%28v=vs.110%29.aspx

    https://msdn.microsoft.com/en-us/library/system.windows.forms.horizontalalignment(v=vs.110).aspx

    Saturday, July 1, 2017 1:25 AM
  • Let me try to explain this a little better by showing the code

        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            '===================================
            Alspause = 5000 ' 5 seconds
            AFheight = ActiveForm.Height 'gets height of active form
            AFwidth = ActiveForm.Width 'gets width of active form
            AlsMBox.Height = 120 'set these to the size window you need
            AlsMBox.Width = 250
            AlsMBtitle = "Al's timed msgbox   --INFORMATION--"
            AlsMBtext = AlsMBtitle & vbCrLf
            AlsMBtext = AlsMBtext & vbCrLf & "This is where you can type in all the text you might need. It will wordwrap."
            AlsMBtext = AlsMBtext & vbCrLf & "Continuing in " & (Alspause / 1000) & " seconds"
            Call AlsMB(AlsMBtext, AFheight, AFwidth, Alspause)
            '====================================
        End Sub
        '

        Private Sub AlsMB(Alsmbtext, AFheight, AFwidth, Alspause)
            Beep()
            AlsMBox.Visible = True
            AlsMBox.Left = (AFwidth - AlsMBox.Width) / 2
            AlsMBox.Top = (AFheight - AlsMBox.Height) / 2
            AlsMBox.Text = Alsmbtext 'text inside my timed MBox
            Refresh()
            'sleep  sleep sleep
            Threading.Thread.Sleep(Alspause)   'stop and wait some number of seconds    3000 = 3 sec
            AlsMBox.Visible = False
            Refresh()
        End Sub

    I would like to center the variable called "AlsMBtitle" inside the "AlsMBox" richtextbox. This is richtextbox is centered inside the form itself.

    Just trying to show a message for a fixed length of time "Alspause" then hide the message again.

    I do know how to do your suggestion.

    Thanks for helping.

    Saturday, July 1, 2017 1:57 AM
  • I would like to center the variable called "AlsMBtitle" inside the "AlsMBox" richtextbox.

    Then the above comment shows the way to do it.  Select the title text and apply the horizontal alignment method with the centre alignment value.  You know what text to select because it starts at zero (the title is the first text that you place into the RTB) and you know the length of the selection is AlsMBtitle.Length.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart%28v=vs.110%29.aspx

    https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionlength(v=vs.110).aspx

    • Marked as answer by cyberal1043 Saturday, July 1, 2017 9:29 PM
    Saturday, July 1, 2017 7:19 AM