I am trying to set DocContent DockLeft to custom size but it is not working. Can anyone suggest how to accomplish this?
I was testing this code but it ends up with default width 404 not 700 as I set.
dockPanelMain.SuspendLayout(true);
DockContent myContent = new MenuForm();
myContent.ShowHint = WeifenLuo.WinFormsUI.Docking.DockState.DockLeft;
myContent.Show(dockPanelMain);
dockPanelMain.DockWindows[WeifenLuo.WinFormsUI.Docking.DockState.DockLeft].Width = 700;
dockPanelMain.ResumeLayout(true, true);
DockLeftPortion
Size of the left docking window. Value < 1 to specify the size in portion;
value > 1 to specify the size in pixels.
Related
I'm trying to set the height of a WPF element using C# to a programatically calculated value.
I've already tried the obvious of setting the height to a variable, a constant variable and just straight-up writing the calculation during the assignment.
Image image1 = new Image()
{
Stretch = Stretch.UniformToFill,
Height = (450 / 650) * 260
};
I'm expecting that the image appears with the size 260x180, but the image doesn't appear at all. Setting the Height to be 180 (the outcome of the calculation) makes it look as expected, though.
How do I fix it and why does it happen?
You need to convert your calculations not to use integers
Image image1 = new Image()
{
Stretch = Stretch.UniformToFill,
Height = ((double)450 / (double)650) * (double)260
};
Didn't test it, but something like this should do the trick.
The problem is that 450 / 650 is 0 when using int.
Maybe 260 * 450 / 650 works too.
I have a PDF doc that I am trying to create, with about 20 columns, varying width. It gets about half of the columns on the first page and then cuts off the rest.I would like it to determine the page width and move the remaining columns onto the second page. Is there a way to specify this in rendering or PageSetup? I think I'll have to calculate the width, create the first page and then create the second.
Table table = new Table();
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = doc;
doc.DefaultPageSetup.Orientation = MigraDoc.DocumentObjectModel.Orientation.Landscape;
//create the columns
for (int i = 1; i < tripReportGrid.Columns.Count; i++)
{
col = table.AddColumn(tripReportGrid.Columns[i].Width);
col.Format.Alignment = ParagraphAlignment.Center;
}
...fill the content same way
renderer.RenderDocument();
The width of the page is what you set - or A4 if you set nothing.
You can set the width of the page to any value. That will probably be OK when viewing the PDF file on the screen.
Or you can only add as many columns to one table as fit on one page. A4 in landscape format is 29.7 cm. Default margins are 2.5 cm left and right.
And BTW: you should never modify the DefaultPageSetup. Assign a Clone() of the DefaultPageSetup to the PageSetup of your Section and change that as needed.
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 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();
MyControl.Margin.Left = 10;
Error:
Cannot modify the return value of 'System.Windows.FrameworkElement.Margin' because it is not a variable
The problem is that Margin is a property, and its type (Thickness) is a value type. That means when you access the property you're getting a copy of the value back.
Even though you can change the value of the Thickness.Left property for a particular value (grr... mutable value types shouldn't exist), it wouldn't change the margin.
Instead, you'll need to set the Margin property to a new value. For instance (coincidentally the same code as Marc wrote):
Thickness margin = MyControl.Margin;
margin.Left = 10;
MyControl.Margin = margin;
As a note for library design, I would have vastly preferred it if Thickness were immutable, but with methods that returned a new value which was a copy of the original, but with one part replaced. Then you could write:
MyControl.Margin = MyControl.Margin.WithLeft(10);
No worrying about odd behaviour of mutable value types, nice and readable, all one expression...
The Margin property returns a Thickness structure, of which Left is a property. What the statement does is copying the structure value from the Margin property and setting the Left property value on the copy. You get an error because the value that you set will not be stored back into the Margin property.
(Earlier versions of C# would just let you do it without complaining, causing a lot of questions in newsgroups and forums on why a statement like that had no effect at all...)
To set the property you would need to get the Thickness structure from the Margin property, set the value and store it back:
Thickness m = MyControl.Margin;
m.Left = 10;
MyControl.Margin = m;
If you are going to set all the margins, just create a Thickness structure and set them all at once:
MyControl.Margin = new Thickness(10, 10, 10, 10);
Margin is returning a struct, which means that you are editing a copy. You will need something like:
var margin = MyControl.Margin;
margin.Left = 10;
MyControl.Margin = margin;
One could simply use this
MyControl.Margin = new System.Windows.Thickness(10, 0, 5, 0);
One would guess that (and my WPF is a little rusty right now) that Margin takes an object and cannot be directly changed.
e.g
MyControl.Margin = new Margin(10,0,0,0);
To use Thickness you need to create/change your project .NET framework platform version to 4.5. becaus this method available only in version 4.5. (Also you can just download PresentationFramework.dll and give referense to this dll, without create/change your .NET framework version to 4.5.)
But if you want to do this simple, You can use this code:
MyControl.Margin = new Padding(int left, int top, int right, int bottom);
also
MyControl.Margin = new Padding(int all);
This is simple and no needs any changes to your project
Depends on the situation, you can also try using padding property here...
MyControl.Margin=new Padding(0,0,0,0);
Margin = new Thickness(0, 0, 0, 0);
It's a bit unclear what are you asking, but to make things comfortable, you can inherit your own Control and add a property with the code that Marc suggests:
class MyImage : Image {
private Thickness thickness;
public double MarginLeft {
get { return Margin.Left; }
set { thickness = Margin; thickness.Left = value; Margin = thickness; }
}
}
Then in the client code you can write just
MyImage img = new MyImage();
img.MarginLeft = 10;
MessageBox.Show(img.Margin.Left.ToString()); // or img.MarginLeft