How can i measure the width of whitespace in winrt.
I'm trying the below code for calculate the size,
For example,
var text = " ";
TextBlock txtBlock = new TextBlock();
txtBlock.Text = text;
txtBlock.FontSize = 14;
...
txtBlock.Measure(size);
var actualWidth = txtBlock.ActualWidth;
But i'm getting width as 0. Please any one help me.
Call Measure() then Arrange() and then ActualWidth and ActualHeight will be updated. This works for me.
Related
I am trying to vertically align text in ActiveReports 13. I am creating this report in code. The example I am trying to match is this:
However, after my efforts to do it, my result ends up looking like this:
The spacing seems to break at odd spots, even though the text in the data source is correct. The code I am using is:
for (int i = 0; i < dataTable.Columns.Count; i++)
{
ctl = new GrapeCity.ActiveReports.SectionReportModel.TextBox();
ctl.Name = columnName;
ctl.Text = dt.Columns[i].ColumnName;
ctl.Location = new PointF((0.3f * i) + 1.7f, 0.4f);
ctl.Size = new SizeF(0.3f, 1.0f);
ctl.VerticalText = true;
ctl.VerticalAlignment = GrapeCity.ActiveReports.Drawing.VerticalTextAlignment.Middle;
}
Increasing the width doesn't help. If I narrow the Size value and adjust the CharacterSpacings, the text spacing problem improves, but the background is narrowed and the text alignment shifts - the characters are rotated 90 degrees:
Any suggestions?
I found the way to solve this was to convert the TextBox to a Label, and then change the Angle property to 2700. Setting the Alignment to "Right" also aligned the text just how I wanted it:
ctl = new GrapeCity.ActiveReports.SectionReportModel.Label();
ctl.Angle = 2700;
ctl.Alignment = GrapeCity.ActiveReports.Drawing.TextAlignment.Right;
I want to be able to set the number of lines in a multilined TextBox.
I've tried the following:
int initHeight = textBox1.Height;
textBox1.Height = initHeight * numOfLines;
But this makes it too large when numOfLines gets large. So then I tried this:
float fontHeight = textBox1.CreateGraphics().MeasureString("W", textBox1.Font).Height;
textBox1.Height = fontHeight * numOfLines;
But this was too small when numOfLines was small, and too large when numOfLines was large.
So I'm doing SOMETHING wrong... any ideas?
This would set the exact Width & Height of your multi line Textbox:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height + Convert.ToInt32(textBox1.Font.Size);
Something like this should work:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height;
This was from C# Resize textbox to fit content
What you are doing should work, but you need to set the MinimumSize and MaximumSize I am not 100% positive, but I think this constraint will still hold if height is set via code
From the documentation of Graphics.MeasureString:
To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.
As such, you should use one of these overloads, such as this one, which allow you to specify StringFormat.GenericTypograpic to get the required size.
Try this:
float fontHeight;
using (var g = textBox1.CreateGraphics())
fontHeight = g.MeasureString("W", textBox1.Font, new PointF(), StringFormat.GenericTypograpic).Height;
I have a wide merged cell showing the title of the sheet. I also have a colored Textbox that I would like to position immediately to the right of the text in the merged cell.
The width of the title is variable so I have used a very wide merged cell.
sheet.Range["A2"].Value = title;
//Red Square
sheet.Shapes.Item("redSquare").Left += 0; // Position sheet.Range["A2"].TextWidth ???
Is there a property I can access on Range that will give me the actual text width in pixels of the text in merged cell?
As far as I know, there isn't anything in Interop that will tell you the width of text in pixels, but you can do it with the following. It seems to return a value reasonably similar to the column widths that Excel sets based on the same fonts.
Excel.Range xlRange = sheet.get_Range("A2");
Excel.Font xlFont = xlRange.Font;
string fontName = xlFont.Name;
double fontSize = xlFont.Size;
Font font = new Font(fontName, (float)fontSize);
float width = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(title, font).Width;
You might need to add a few pixels here and there to make sure your textbox clears the end of the text, but I think it's about as accurate as you're going to get.
double peratusE = ((double)(bilanganE / calonAmbil)) * 100.00;
Label peratusELabel = row.Cells[16].FindControl("peratusELabel") as Label;
peratusELabel.Text = String.Format("{0:0.00}", peratusELabel);
i use that particular code to calculate the percentage and to assign the percentage value to a label. however, when running it, it displays "System.Web.UI.WebControls.Label" instead of the value.
for your information: i use
double peratusD = ((double)(bilanganD / calonAmbil)) * 100.00;
Label peratusDLabel = row.Cells[14].FindControl("peratusDLabel") as Label;
peratusDLabel.Text = String.Format("{0:0.00}", peratusD);
but this time it works just fine. i'm stucked.
It should be:
peratusELabel.Text = String.Format("{0:0.00}", peratusE);
In your first code block you are formatting a Label object, not a double object.
I would like to set my ListBox.Width property so that it is no wider nor narrower than needed, in order to display the items in it. There is a margin of a few pixels between the left of the ListBox and the start of the text - I would like there to be a similar margin on the right. (i.e. there shouldn't be a large gap, and the letters shouldn't be touching the right edge).
Given that I'm not sure how many pixels a given string will be, I'm not sure how to calculate this width.
I believe you're looking for the MeasureString method of the Graphics class.
Try this:
Graphics graphics = this.createGraphics();
SizeF mySize = graphics.MeasureString("Ahoy there", this.Font);
Hope this helps!
This may be what you want. Also play around with Integral Height and padding.
http://www.codeproject.com/KB/combobox/resizablelistbox.aspx
This worked for me, it wasn't until I changed the width of the listbox that I saw the results I wanted. I looped through the items in the listbox to get the longest. Hope this helps.
int LongestItemLength = 0;
for (int i = 0; i < listBox1.Items.Count;i++ ){
Graphics g = listBox1.CreateGraphics();
int tempLength = Convert.ToInt32((
g.MeasureString(
listBox1.Items[i].ToString(),
this.listBox1.Font
)
).Width);
if (tempLength > LongestItemLength){
LongestItemLength = tempLength;
}
}
listBox1.Width = LongestItemLength;
listBox1.Show();