-
Apr 20th, 2021, 10:48 AM
#1
Thread Starter
Hyperactive Member
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.
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() )
-
Apr 20th, 2021, 08:13 PM
#2
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.
-
Apr 21st, 2021, 02:09 PM
#3
Thread Starter
Hyperactive Member
Re: TabControl tabs text alignment issue
Originally Posted by jmcilhinney
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.
-
Apr 21st, 2021, 03:31 PM
#4
Re: TabControl tabs text alignment issue
Originally Posted by pourkascheff
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)
-
Apr 23rd, 2021, 07:20 PM
#5
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.
-
Apr 24th, 2021, 06:47 AM
#6
Re: TabControl tabs text alignment issue
Originally Posted by Poppa Mintin
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 24th, 2021, 08:50 AM
#7
Re: TabControl tabs text alignment issue
Originally Posted by .paul.
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.
-
Apr 24th, 2021, 09:26 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|