Results 1 to 8 of 8

Thread: TabControl tabs text alignment issue

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    TabControl tabs text alignment issue

    Hi all. o/

    There's a tab control object in a windows form which its tab names change repeatedly. The only problem is, they hold their previous text start position. For instance, here used "Untitled" before running the program. When it changed, suddenly 1 WBT starts from beginning of the line. Not as same as "Alarms" tab which is fixed all the time.
    Name:  Untitled.png
Views: 731
Size:  922 Bytes

    How to fix this?
    Is there any command to do such thing?

    What I tried:
    - Refreshing form. ( Form.Refresh() )
    - Refreshing tab control text (useless due to unavailability to select an index) ( TabControl1.ResetText() )

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: TabControl tabs text alignment issue

    To be clear, you're saying that both tabs look right when you display the form but, if you change the Text of one at run time, it gets rendered further to the left? It looks lkike you have turned off visual styles, so maybe that is part of it. Please provide a FULL and CLEAR explanation of the problem, which would include a set of steps that we can follow to reproduce the issue. You should also have tested with visual styles on, to confirm whether or not that is a factor, given that the vast majority of users will have it on.

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Post Re: TabControl tabs text alignment issue

    Quote Originally Posted by jmcilhinney View Post
    To be clear, you're saying that both tabs look right when you display the form but, if you change the Text of one at run time, it gets rendered further to the left? It looks lkike you have turned off visual styles, so maybe that is part of it. Please provide a FULL and CLEAR explanation of the problem, which would include a set of steps that we can follow to reproduce the issue. You should also have tested with visual styles on, to confirm whether or not that is a factor, given that the vast majority of users will have it on.
    Mate! hi, is there any templates for creating a thread? English is my second language and I am in a bit inconvenience.

    Anyways. I will try to be more specific and clear next time and yes. XP visual styles is checked in "My project" application settings.
    Last edited by pourkascheff; Apr 21st, 2021 at 02:26 PM.

  4. #4
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: TabControl tabs text alignment issue

    Quote Originally Posted by pourkascheff View Post
    Mate! hi, is there any templates for creating a thread?
    Well, it happens that there is : https://www.vbforums.com/showthread....our-hovercraft
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  5. #5
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: TabControl tabs text alignment issue

    Hi Pourkascheff,

    I do this to centralise text string in a control. Please note: the string length must be at least 2 characters.
    1. Measure the text length.
    2. Compare the length with the width of the control.
    3. Keep adding one space character after the 1st character of the text until the text no longer fits into the control,
    4. Then we just add half the number of added spaces in front of the required text.
    Code:
        Public Function Gap(ByVal ctrl As Control, ByVal msg As String) As Integer
            Dim num, spac As Integer
            num = 0 : spac = 0      ' Keep Intellisence happy... initialise these variables.
            While spac < ctrl.Width
                msg = msg.Insert(1, " ")
                num += 1
                Using gr As Graphics = ctrl.CreateGraphics
                    spac = CInt(gr.MeasureString(msg, ctrl.Font).Width)
                End Using
            End While
            num \= 2  ' Note the integer 'Divide by'.
            Return num
        End Function
    When I get 'num' returned I use it like this:
    (For example, Centralise a heading part way down a long instruction in a TextBox, or use it in a control which doesn't have a TextAlign option)
    Code:
        Dim txt As String = "NOTE!"
        TextBox1.Text &= Space(Gap(TextBox1, txt)) & txt

    Poppa


    PS. I just realised that I could add two space characters at a time, then I wouldn't have to divide by two !

    Pop
    Last edited by Poppa Mintin; Apr 23rd, 2021 at 07:30 PM. Reason: Add PostScript.
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: TabControl tabs text alignment issue

    Quote Originally Posted by Poppa Mintin View Post
    Hi Pourkascheff,

    I do this to centralise text string in a control. Please note: the string length must be at least 2 characters.
    1. Measure the text length.
    2. Compare the length with the width of the control.
    3. Keep adding one space character after the 1st character of the text until the text no longer fits into the control,
    4. Then we just add half the number of added spaces in front of the required text.
    Code:
        Public Function Gap(ByVal ctrl As Control, ByVal msg As String) As Integer
            Dim num, spac As Integer
            num = 0 : spac = 0      ' Keep Intellisence happy... initialise these variables.
            While spac < ctrl.Width
                msg = msg.Insert(1, " ")
                num += 1
                Using gr As Graphics = ctrl.CreateGraphics
                    spac = CInt(gr.MeasureString(msg, ctrl.Font).Width)
                End Using
            End While
            num \= 2  ' Note the integer 'Divide by'.
            Return num
        End Function
    When I get 'num' returned I use it like this:
    (For example, Centralise a heading part way down a long instruction in a TextBox, or use it in a control which doesn't have a TextAlign option)
    Code:
        Dim txt As String = "NOTE!"
        TextBox1.Text &= Space(Gap(TextBox1, txt)) & txt

    Poppa


    PS. I just realised that I could add two space characters at a time, then I wouldn't have to divide by two !

    Pop
    It's a hack, but it'll probably work There really should be a simpler way...

  7. #7
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: TabControl tabs text alignment issue

    Quote Originally Posted by .paul. View Post
    It's a hack, but it'll probably work There really should be a simpler way...
    It might be a hack, but I've used it for several years now.
    It does work, provided there are at least two characters in the string, and that the string does not contain leading or trailing space characters, (i.e. txt.Trim ). It seems that 'Measure String' ignores leading and trailing space characters.


    Poppa
    Last edited by Poppa Mintin; Apr 24th, 2021 at 08:51 AM. Reason: typo
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: TabControl tabs text alignment issue

    Here is an example of my little hack working. Everything between the heading and the two controls at the bottom is within just one TextBox.

    Attachment 181162

    Poppa
    Along with the sunshine there has to be a little rain sometime.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width