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 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);