Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Centering text with respect to width of UserControl

Centering text with respect to width of UserControl

Scheduled Pinned Locked Moved C#
questiongraphicstutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheBlindWatchmaker
    wrote on last edited by
    #1

    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!

    D N 2 Replies Last reply
    0
    • T TheBlindWatchmaker

      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!

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      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.


      Logifusion[^]

      T 1 Reply Last reply
      0
      • D Dustin Metzgar

        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.


        Logifusion[^]

        T Offline
        T Offline
        TheBlindWatchmaker
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • T TheBlindWatchmaker

          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!

          N Offline
          N Offline
          NaNg15241
          wrote on last edited by
          #4

          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.

          T 1 Reply Last reply
          0
          • N NaNg15241

            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.

            T Offline
            T Offline
            TheBlindWatchmaker
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups