none
Textbox Text Vertical alignment

    Question

  • Hi,

     

    I would like to align my textbox: center Vertical. 

    Something likes: _textBox.TextAlign = VerticalAlignment.Center;

     

    Would this be possible?

     

    Thanks in advance.

    • Moved by CoolDadTxMVP Tuesday, July 20, 2010 1:27 PM Winforms related (From:Visual C# General)
    Tuesday, July 20, 2010 1:16 PM

Answers

  • Hello nguyenxh,

    There is no VerticalAlignment property for TextBox control, because the text is located according to row position, so text cannot be shown across rows. In another way, once you set Multiline property of the textBox to true means that it can be vertically scrollable, center vertical align is no need to use. 

    So, since I don't know your usage here, if you just want the some words shown in textbox with vertical center, you just can use Picturebox or Label instead (for Label, set the AutoSize property to false). 

    But if you insist on doing vertical center with TextBox, you can do as follows:

    'Set MultiLine property of the TextBox to True

    Private   Type   RECT
            Left   As   Long
            Top   As   Long
            Right   As   Long
            Bottom   As   Long
    End   Type
    Private   Declare   Function   SendMessage   Lib   "user32 "   Alias   "SendMessageA "   (   _
            ByVal   hwnd   As   Long,   _
            ByVal   wMsg   As   Long,   _
            ByVal   wParam   As   Long,   _
            lParam   As   Any)   As   Long
    Private   Const   EM_GETRECT   =   &HB2
    Private   Const   EM_SETRECTNP   =   &HB4

    Sub   VerMiddleText(mText   As   TextBox)
            If   mText.MultiLine   =   False   Then   Exit   Sub
            Dim   rc   As   RECT,   tmpTop   As   Long,   tmpBot   As   Long
            SendMessage   mText.hwnd,   EM_GETRECT,   0,   rc
            With   Me.Font
                .Name   =   mText.Font.Name
                .Size   =   mText.Font.Size
                .Bold   =   mText.Font.Bold
            End   With
            tmpTop   =   ((rc.Bottom   -   rc.Top)   -   _
                    (mText.Parent.TextHeight( "H ")   \   Screen.TwipsPerPixelY))   \   2   +   2
            tmpBot   =   ((rc.Bottom   -   rc.Top)   +   _
                    (mText.Parent.TextHeight( "H ")   \   Screen.TwipsPerPixelY))   \   2   +   2
            rc.Top   =   tmpTop
            rc.Bottom   =   tmpBot
            mText.Alignment   =   vbCenter
            SendMessage   mText.hwnd,   EM_SETRECTNP,   0&,   rc
            mText.Refresh
    End   Sub

    Private   Sub   Form_Load()
            VerMiddleText(Text1)
    End   Sub

     

    Hope this helps.

     

    Best wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, July 27, 2010 9:34 AM
    Monday, July 26, 2010 8:58 AM

All replies

  • a) it can't be done.

    b) it can be done, by creating your own usercontrol and creating the graphics (paint-events etc) yourself

    c) it can be done in WPF, port your complete application

    d) it can be done in WPF by creating a WPF User Control library containing a vertically aligned textbox and importing that in your winforms.  (See http://dotnet.dzone.com/articles/hosting-wpf-controls-winforms on how to do that).

     

    Take your pick :-)


    "The improbable we do, the impossible just takes a little longer." (Steven Parker)
    Tuesday, July 20, 2010 1:39 PM
  • b) it can be done, by creating your own usercontrol and creating the graphics (paint-events etc) yourself

    => could you please more specific? what should I rewrite it?

     

    Thanks in advance.

    Tuesday, July 20, 2010 2:07 PM
  • Hi,

    Text box control don't have VerticalAlignment property.

    Just use a Label control instead of it.  BorderStyle = FixedSingle, TextAlign = MiddleCenter.

    refer this link

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0b772de4-4e56-4dd5-8952-5dcdfa853933

    otherwise

    You have to create your own custom build text box control.

     


    Thanks, Karguvel
    Wednesday, July 21, 2010 2:31 PM
  • Hi,

    Even though there is no direct option, you can do programitically

    If it is a single line text you can do like this,

     

    textBox1.TextAlign = HorizontalAlignment.Center;
    int a = textBox1.Size.Height;
    float c = textBox1.Font.Height;
    for(int i = 0; i < (int)(a/c) / 2; i++)
    {
     textBox1.Text += "\r\n";
    }
    textBox1.Text += "hai";
    

     

    If your data is multiline then you have do

    a = a - no. of lines * c

    before for loop

     


    Thanks, Karguvel
    Wednesday, July 21, 2010 2:45 PM
  • Hello nguyenxh,

    There is no VerticalAlignment property for TextBox control, because the text is located according to row position, so text cannot be shown across rows. In another way, once you set Multiline property of the textBox to true means that it can be vertically scrollable, center vertical align is no need to use. 

    So, since I don't know your usage here, if you just want the some words shown in textbox with vertical center, you just can use Picturebox or Label instead (for Label, set the AutoSize property to false). 

    But if you insist on doing vertical center with TextBox, you can do as follows:

    'Set MultiLine property of the TextBox to True

    Private   Type   RECT
            Left   As   Long
            Top   As   Long
            Right   As   Long
            Bottom   As   Long
    End   Type
    Private   Declare   Function   SendMessage   Lib   "user32 "   Alias   "SendMessageA "   (   _
            ByVal   hwnd   As   Long,   _
            ByVal   wMsg   As   Long,   _
            ByVal   wParam   As   Long,   _
            lParam   As   Any)   As   Long
    Private   Const   EM_GETRECT   =   &HB2
    Private   Const   EM_SETRECTNP   =   &HB4

    Sub   VerMiddleText(mText   As   TextBox)
            If   mText.MultiLine   =   False   Then   Exit   Sub
            Dim   rc   As   RECT,   tmpTop   As   Long,   tmpBot   As   Long
            SendMessage   mText.hwnd,   EM_GETRECT,   0,   rc
            With   Me.Font
                .Name   =   mText.Font.Name
                .Size   =   mText.Font.Size
                .Bold   =   mText.Font.Bold
            End   With
            tmpTop   =   ((rc.Bottom   -   rc.Top)   -   _
                    (mText.Parent.TextHeight( "H ")   \   Screen.TwipsPerPixelY))   \   2   +   2
            tmpBot   =   ((rc.Bottom   -   rc.Top)   +   _
                    (mText.Parent.TextHeight( "H ")   \   Screen.TwipsPerPixelY))   \   2   +   2
            rc.Top   =   tmpTop
            rc.Bottom   =   tmpBot
            mText.Alignment   =   vbCenter
            SendMessage   mText.hwnd,   EM_SETRECTNP,   0&,   rc
            mText.Refresh
    End   Sub

    Private   Sub   Form_Load()
            VerMiddleText(Text1)
    End   Sub

     

    Hope this helps.

     

    Best wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, July 27, 2010 9:34 AM
    Monday, July 26, 2010 8:58 AM