Auto Size Lable to Text. How can i do this on CF2.0??

I

iKiLL

Hi All,

I am trying to Bulid Windows Mobile Forms Control with C# in VS2005 using
CF2.

On this control A lable is created and some text set for the control. My
problem is that i dont know how lond the text is going to be. so i need to
set the height and width of the label a cording to the data.

un fortunatly i have not been able to find a simple Auto Resize property
that will work so i have been trying to find a way to messure the string.
and calculat the number of lines that will be needed based on the Height of
the string and the max width of the label.

Below is my code.

The first heardal seems to be that CreateGraphics() Is not supported on the
Compact Framework.

Is there another way for me to get the lable to resize?

Thanks,
ink


CODE:


//this is the Working Width of the Slid Pannel
int iSliderWidth = pnSlide.Width - (m_iRight_Margin +
m_iLeft_Margin);

//Set up the Question Lable
Label oQuestionText = new Label();
oQuestionText.Top = m_iLine_Space;
oQuestionText.Left = m_iLeft_Margin;
oQuestionText.Font = new System.Drawing.Font(m_FontName,
m_FontSize, FontStyle.Bold);
oQuestionText.ForeColor = Color.Blue;

//Work out the Number of lines the Lable needs to be to fit
the text
Graphics g = picHidden.CreateGraphics(); //
Graphics.FromImage(img);
string sQuestionText =
oRowQA["syqu_questiontext"].ToString();
Font sFont = new Font(m_FontName, m_FontSize,
FontStyle.Bold);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = g.MeasureString(sQuestionText, sFont);
int iStringWidth =
int.Parse(stringSize.Width.ToString("####0"));
float fNoOfLines = iStringWidth / iSliderWidth;
if (fNoOfLines > 1)
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round((fNoOfLines *
int.Parse(stringSize.Height.ToString("####0"))) + 0.5));
}
else
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round(int.Parse(stringSize.Height.ToString("####0")) +
0.5));
}
g.Dispose();

oQuestionText.Text = sQuestionText;
//Add the control to the slid panel
pnSlide.Controls.Add(oQuestionText);
 
I

iKiLL

Hi Tomer,

Thanks for you input.

If you take another look at the code attached you will see that i have
actualy got that.

The problem is before getting to that on the
Graphics g = picHidden.CreateGraphics();
Line of code.

You see CreateGraphics(); does not seem to be supported in the CF so i need
some other way of Creating a Graphic in my variable.

Do you have any ideas on how to do that?

Thanks,
ink



Tomer Gabel said:
Hello iKiLL,

Graphics.MeasureString will return a SizeF construct you can use for that.

Regards,
Tomer Gabel (http://www.tomergabel.com)
Monfort Software Engineering Ltd. (http://www.monfort.co.il)

Hi All,

I am trying to Bulid Windows Mobile Forms Control with C# in VS2005
using CF2.

On this control A lable is created and some text set for the control.
My problem is that i dont know how lond the text is going to be. so i
need to set the height and width of the label a cording to the data.

un fortunatly i have not been able to find a simple Auto Resize
property that will work so i have been trying to find a way to messure
the string. and calculat the number of lines that will be needed based
on the Height of the string and the max width of the label.

Below is my code.

The first heardal seems to be that CreateGraphics() Is not supported
on the Compact Framework.

Is there another way for me to get the lable to resize?

Thanks,
ink
CODE:

//this is the Working Width of the Slid Pannel
int iSliderWidth = pnSlide.Width - (m_iRight_Margin +
m_iLeft_Margin);
//Set up the Question Lable
Label oQuestionText = new Label();
oQuestionText.Top = m_iLine_Space;
oQuestionText.Left = m_iLeft_Margin;
oQuestionText.Font = new
System.Drawing.Font(m_FontName,
m_FontSize, FontStyle.Bold);
oQuestionText.ForeColor = Color.Blue;
//Work out the Number of lines the Lable needs to be
to fit
the text
Graphics g = picHidden.CreateGraphics(); //
Graphics.FromImage(img);
string sQuestionText =
oRowQA["syqu_questiontext"].ToString();
Font sFont = new Font(m_FontName, m_FontSize,
FontStyle.Bold);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = g.MeasureString(sQuestionText, sFont);
int iStringWidth =
int.Parse(stringSize.Width.ToString("####0"));
float fNoOfLines = iStringWidth / iSliderWidth;
if (fNoOfLines > 1)
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round((fNoOfLines *
int.Parse(stringSize.Height.ToString("####0"))) + 0.5));
}
else
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round(int.Parse(stringSize.Height.ToString("####0
")) +
0.5));
}
g.Dispose();
oQuestionText.Text = sQuestionText;
//Add the control to the slid panel
pnSlide.Controls.Add(oQuestionText);
 
I

iKiLL

Looks like i found a way that works.

//Work out the Number of lines the Lable needs to be to fit the text
Bitmap bmp = new Bitmap(175, 175);
Graphics g = Graphics.FromImage(bmp);

I also found a Auto Resizing label class.

Here:
http://weblogs.asp.net/okloeten/archive/2004/03/30/103384.aspx

Hope this helps some one else to.

ink








iKiLL said:
Hi Tomer,

Thanks for you input.

If you take another look at the code attached you will see that i have
actualy got that.

The problem is before getting to that on the
Graphics g = picHidden.CreateGraphics();
Line of code.

You see CreateGraphics(); does not seem to be supported in the CF so i
need some other way of Creating a Graphic in my variable.

Do you have any ideas on how to do that?

Thanks,
ink



Tomer Gabel said:
Hello iKiLL,

Graphics.MeasureString will return a SizeF construct you can use for
that.

Regards,
Tomer Gabel (http://www.tomergabel.com)
Monfort Software Engineering Ltd. (http://www.monfort.co.il)

Hi All,

I am trying to Bulid Windows Mobile Forms Control with C# in VS2005
using CF2.

On this control A lable is created and some text set for the control.
My problem is that i dont know how lond the text is going to be. so i
need to set the height and width of the label a cording to the data.

un fortunatly i have not been able to find a simple Auto Resize
property that will work so i have been trying to find a way to messure
the string. and calculat the number of lines that will be needed based
on the Height of the string and the max width of the label.

Below is my code.

The first heardal seems to be that CreateGraphics() Is not supported
on the Compact Framework.

Is there another way for me to get the lable to resize?

Thanks,
ink
CODE:

//this is the Working Width of the Slid Pannel
int iSliderWidth = pnSlide.Width - (m_iRight_Margin +
m_iLeft_Margin);
//Set up the Question Lable
Label oQuestionText = new Label();
oQuestionText.Top = m_iLine_Space;
oQuestionText.Left = m_iLeft_Margin;
oQuestionText.Font = new
System.Drawing.Font(m_FontName,
m_FontSize, FontStyle.Bold);
oQuestionText.ForeColor = Color.Blue;
//Work out the Number of lines the Lable needs to be
to fit
the text
Graphics g = picHidden.CreateGraphics(); //
Graphics.FromImage(img);
string sQuestionText =
oRowQA["syqu_questiontext"].ToString();
Font sFont = new Font(m_FontName, m_FontSize,
FontStyle.Bold);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = g.MeasureString(sQuestionText, sFont);
int iStringWidth =
int.Parse(stringSize.Width.ToString("####0"));
float fNoOfLines = iStringWidth / iSliderWidth;
if (fNoOfLines > 1)
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round((fNoOfLines *
int.Parse(stringSize.Height.ToString("####0"))) + 0.5));
}
else
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round(int.Parse(stringSize.Height.ToString("####0
")) +
0.5));
}
g.Dispose();
oQuestionText.Text = sQuestionText;
//Add the control to the slid panel
pnSlide.Controls.Add(oQuestionText);
 
G

Guest

CreateGraphics is supported IIRC for Form, Control and Panel only. Use one
of those or override OnPaint.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


iKiLL said:
Hi Tomer,

Thanks for you input.

If you take another look at the code attached you will see that i have
actualy got that.

The problem is before getting to that on the
Graphics g = picHidden.CreateGraphics();
Line of code.

You see CreateGraphics(); does not seem to be supported in the CF so i
need some other way of Creating a Graphic in my variable.

Do you have any ideas on how to do that?

Thanks,
ink



Tomer Gabel said:
Hello iKiLL,

Graphics.MeasureString will return a SizeF construct you can use for
that.

Regards,
Tomer Gabel (http://www.tomergabel.com)
Monfort Software Engineering Ltd. (http://www.monfort.co.il)

Hi All,

I am trying to Bulid Windows Mobile Forms Control with C# in VS2005
using CF2.

On this control A lable is created and some text set for the control.
My problem is that i dont know how lond the text is going to be. so i
need to set the height and width of the label a cording to the data.

un fortunatly i have not been able to find a simple Auto Resize
property that will work so i have been trying to find a way to messure
the string. and calculat the number of lines that will be needed based
on the Height of the string and the max width of the label.

Below is my code.

The first heardal seems to be that CreateGraphics() Is not supported
on the Compact Framework.

Is there another way for me to get the lable to resize?

Thanks,
ink
CODE:

//this is the Working Width of the Slid Pannel
int iSliderWidth = pnSlide.Width - (m_iRight_Margin +
m_iLeft_Margin);
//Set up the Question Lable
Label oQuestionText = new Label();
oQuestionText.Top = m_iLine_Space;
oQuestionText.Left = m_iLeft_Margin;
oQuestionText.Font = new
System.Drawing.Font(m_FontName,
m_FontSize, FontStyle.Bold);
oQuestionText.ForeColor = Color.Blue;
//Work out the Number of lines the Lable needs to be
to fit
the text
Graphics g = picHidden.CreateGraphics(); //
Graphics.FromImage(img);
string sQuestionText =
oRowQA["syqu_questiontext"].ToString();
Font sFont = new Font(m_FontName, m_FontSize,
FontStyle.Bold);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = g.MeasureString(sQuestionText, sFont);
int iStringWidth =
int.Parse(stringSize.Width.ToString("####0"));
float fNoOfLines = iStringWidth / iSliderWidth;
if (fNoOfLines > 1)
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round((fNoOfLines *
int.Parse(stringSize.Height.ToString("####0"))) + 0.5));
}
else
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round(int.Parse(stringSize.Height.ToString("####0
")) +
0.5));
}
g.Dispose();
oQuestionText.Text = sQuestionText;
//Add the control to the slid panel
pnSlide.Controls.Add(oQuestionText);
 
I

iKiLL

Hi

i am sorry but i don't know what IIRC means.
I tried using the Panel as well as a test and it was definatly not in the
Context help for it as a method.

But i have solved the problem now using the Bitmap Object.

thanks,

ink



CreateGraphics is supported IIRC for Form, Control and Panel only. Use
one of those or override OnPaint.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


iKiLL said:
Hi Tomer,

Thanks for you input.

If you take another look at the code attached you will see that i have
actualy got that.

The problem is before getting to that on the
Graphics g = picHidden.CreateGraphics();
Line of code.

You see CreateGraphics(); does not seem to be supported in the CF so i
need some other way of Creating a Graphic in my variable.

Do you have any ideas on how to do that?

Thanks,
ink



Tomer Gabel said:
Hello iKiLL,

Graphics.MeasureString will return a SizeF construct you can use for
that.

Regards,
Tomer Gabel (http://www.tomergabel.com)
Monfort Software Engineering Ltd. (http://www.monfort.co.il)


Hi All,

I am trying to Bulid Windows Mobile Forms Control with C# in VS2005
using CF2.

On this control A lable is created and some text set for the control.
My problem is that i dont know how lond the text is going to be. so i
need to set the height and width of the label a cording to the data.

un fortunatly i have not been able to find a simple Auto Resize
property that will work so i have been trying to find a way to messure
the string. and calculat the number of lines that will be needed based
on the Height of the string and the max width of the label.

Below is my code.

The first heardal seems to be that CreateGraphics() Is not supported
on the Compact Framework.

Is there another way for me to get the lable to resize?

Thanks,
ink
CODE:

//this is the Working Width of the Slid Pannel
int iSliderWidth = pnSlide.Width - (m_iRight_Margin +
m_iLeft_Margin);
//Set up the Question Lable
Label oQuestionText = new Label();
oQuestionText.Top = m_iLine_Space;
oQuestionText.Left = m_iLeft_Margin;
oQuestionText.Font = new
System.Drawing.Font(m_FontName,
m_FontSize, FontStyle.Bold);
oQuestionText.ForeColor = Color.Blue;
//Work out the Number of lines the Lable needs to be
to fit
the text
Graphics g = picHidden.CreateGraphics(); //
Graphics.FromImage(img);
string sQuestionText =
oRowQA["syqu_questiontext"].ToString();
Font sFont = new Font(m_FontName, m_FontSize,
FontStyle.Bold);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = g.MeasureString(sQuestionText, sFont);
int iStringWidth =
int.Parse(stringSize.Width.ToString("####0"));
float fNoOfLines = iStringWidth / iSliderWidth;
if (fNoOfLines > 1)
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round((fNoOfLines *
int.Parse(stringSize.Height.ToString("####0"))) + 0.5));
}
else
{
oQuestionText.Width = iSliderWidth;
//may seem silly but this makes sure the
Math.Round
rounds up by always adding 0.5
oQuestionText.Height =
Convert.ToInt32(Math.Round(int.Parse(stringSize.Height.ToString("####0
")) +
0.5));
}
g.Dispose();
oQuestionText.Text = sQuestionText;
//Add the control to the slid panel
pnSlide.Controls.Add(oQuestionText);
 
Top