Centering text with respect to width of UserControl
-
I have several instances of a UserControl on a Form. Each UserControl represents one of several possible audio channels. This UserControl has a public property describing which audio channel the particular instance represents. The list of available audio channels is in an enum. I've been able to implement "setting" this property and the corresponding updation of a label in the UserControl to reflect which channel that particular instance of the UserControl is currently representing. The width of the control is constant. The width of the text is not. For example, Left, Right, Left Surr, Right Surr, etc. How do I get the text centered with respect to the width of the control. I tried
lblChannel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
but that doesn't work. I suppose that only aligns text with respect to the label. Do I need to calculate the location of the label for each channel selection? Thanks! -
I have several instances of a UserControl on a Form. Each UserControl represents one of several possible audio channels. This UserControl has a public property describing which audio channel the particular instance represents. The list of available audio channels is in an enum. I've been able to implement "setting" this property and the corresponding updation of a label in the UserControl to reflect which channel that particular instance of the UserControl is currently representing. The width of the control is constant. The width of the text is not. For example, Left, Right, Left Surr, Right Surr, etc. How do I get the text centered with respect to the width of the control. I tried
lblChannel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
but that doesn't work. I suppose that only aligns text with respect to the label. Do I need to calculate the location of the label for each channel selection? Thanks!Well, if middlecenter works to calculate with respect to the label, make the label fill the user control horizontally. As long as AutoSize is turned off and you have the label anchored to the left and right, it should work.
-
Well, if middlecenter works to calculate with respect to the label, make the label fill the user control horizontally. As long as AutoSize is turned off and you have the label anchored to the left and right, it should work.
Thanks for your reply! Turning Autosize off results in the label's text being truncated. "Left Surround" shows up as "Left". What am I doing wrong?
-
I have several instances of a UserControl on a Form. Each UserControl represents one of several possible audio channels. This UserControl has a public property describing which audio channel the particular instance represents. The list of available audio channels is in an enum. I've been able to implement "setting" this property and the corresponding updation of a label in the UserControl to reflect which channel that particular instance of the UserControl is currently representing. The width of the control is constant. The width of the text is not. For example, Left, Right, Left Surr, Right Surr, etc. How do I get the text centered with respect to the width of the control. I tried
lblChannel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
but that doesn't work. I suppose that only aligns text with respect to the label. Do I need to calculate the location of the label for each channel selection? Thanks!You can either spread the label to the whole width of the usercontrol and then when it is TextAlign = center it will be automaticly centered. Or you can use this: (this.Size.Width - lbl1.Size.Width - 10) / 2 -> this will be cenetered. (Set it as the location with the y position that you need.) Hope I helped... NaNg.
-
You can either spread the label to the whole width of the usercontrol and then when it is TextAlign = center it will be automaticly centered. Or you can use this: (this.Size.Width - lbl1.Size.Width - 10) / 2 -> this will be cenetered. (Set it as the location with the y position that you need.) Hope I helped... NaNg.
Ah! Ok... I didn't think about spreading the label to cover the width of the control. (sorry, I'm new to this still!) Thanks for your reply! It works now.