Label Box Wrapping

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an application using WinForms under C#. I do not use the IDE, I use
the command line compiler, I build my controls in my application code. The
application generally works fine, except for one small problem: I have made a
user control that has a Label Box to display the prompt. The problem is that
the text in the label box sometimes wraps to a second line on a word break,
and I do not want it to.

I have tried all the wrapping type properties I could find for that type of
control, and have tried making the control longer and less high, so it has no
reason to wrap, but it still does.

Thanks.
 
Richard MSL,

You probably need to post compilable sample code that demonsteates the
problem.
 
System.Windows.Forms.Label will always wrap text. I don't think it even has a "Wrap" property or anything of the like.

You can determine an appropriate size for the label by using a Graphics object and it's MeasureString method:

using (System.Drawing.Graphics g = myLabel.CreateGraphics())
{
// Set the width of the label to include the full size of the text
myLabel.Width = (int) g.MeasureString(myLabel.Text, myLabel.Font).Width + 10; // Pad with 10 pixels
}

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Stoitcho Goutsev (100) said:
Richard MSL,

You probably need to post compilable sample code that demonsteates the problem.


--
Stoitcho Goutsev (100) [C# MVP]

Richard MSL said:
I have an application using WinForms under C#. I do not use the IDE, I use
the command line compiler, I build my controls in my application code. The
application generally works fine, except for one small problem: I have made a
user control that has a Label Box to display the prompt. The problem is that
the text in the label box sometimes wraps to a second line on a word break,
and I do not want it to.

I have tried all the wrapping type properties I could find for that type of
control, and have tried making the control longer and less high, so it has no
reason to wrap, but it still does.

Thanks.
 
Thanks for the responses, I had already tried to make the box longer so that
it did not need to wrap. I will post the actual source code so that perhaps
someone can see what I am doing incorrectly.

Dave said:
System.Windows.Forms.Label will always wrap text. I don't think it even has a "Wrap" property or anything of the like.

You can determine an appropriate size for the label by using a Graphics object and it's MeasureString method:

using (System.Drawing.Graphics g = myLabel.CreateGraphics())
{
// Set the width of the label to include the full size of the text
myLabel.Width = (int) g.MeasureString(myLabel.Text, myLabel.Font).Width + 10; // Pad with 10 pixels
}

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Stoitcho Goutsev (100) said:
Richard MSL,

You probably need to post compilable sample code that demonsteates the problem.


--
Stoitcho Goutsev (100) [C# MVP]

Richard MSL said:
I have an application using WinForms under C#. I do not use the IDE, I use
the command line compiler, I build my controls in my application code. The
application generally works fine, except for one small problem: I have made a
user control that has a Label Box to display the prompt. The problem is that
the text in the label box sometimes wraps to a second line on a word break,
and I do not want it to.

I have tried all the wrapping type properties I could find for that type of
control, and have tried making the control longer and less high, so it has no
reason to wrap, but it still does.

Thanks.
 
I have solved the problem, once I went to extract it to post, it became
obvious, I was sizing the container not the label. Thanks for the help.

Stoitcho Goutsev (100) said:
Richard MSL,

You probably need to post compilable sample code that demonsteates the
problem.


--
Stoitcho Goutsev (100) [C# MVP]

Richard MSL said:
I have an application using WinForms under C#. I do not use the IDE, I use
the command line compiler, I build my controls in my application code. The
application generally works fine, except for one small problem: I have
made a
user control that has a Label Box to display the prompt. The problem is
that
the text in the label box sometimes wraps to a second line on a word
break,
and I do not want it to.

I have tried all the wrapping type properties I could find for that type
of
control, and have tried making the control longer and less high, so it has
no
reason to wrap, but it still does.

Thanks.
 
Back
Top