Which control do I use in Visual C# to create a paned window?
This is what I mean:
http://effbot.org/tkinterbook/panedwindow.htm
As mentioned in my comment you can use the SplitContainer-Control to divide your windows in multiple sections/panes.
You can use its Dock-Property to set the width and height to the parents size
splitContainer1.Dock = DockStyle.Fill //Sets the size to the one's of the parent container
Further you can set if the splitter is vertical or horizontal with following code:
[MSDN]
splitContainer1.Orientation = Orientation.Horizontal; //Or Orientation.Vertical
To show the borders of the SplitContainer you can use the BorderStyle-Property:
splitContainer1.BorderStyle = BorderStyle.Fixed3D; //3d-Effect
//BorderStyle.FixedSingle; //Shown in the example
//BorderStyle.None; //No borders
Finally you can get following result for example:
Related
I have an ElementHost which contains an WPF user control.
For some reason that I don't know, the ElementHost is not being resized to the user control when it changes its height.
My ElementHost has the property AutoSize set to True and the Dock property set to DockStyle.Fill.
Also my WPF user control that I bound to the ElementHost.Child has also the Autosize property set to True.
When my WPF changes its height dynamically, the ElementHost does not resize accordingly to have the same height as the WPF user control.
I have been searching a lot in google and I got with this interesting post and also with this one.
There is discussed about do the following:
Override the MeasureOverride method
After the measurement is calculated, use
PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point)
to get the device coordinates
Update ElementHost.Size.
So in one of the answers of that post I found the method that calculates the size which is this. Here it is:
public System.Windows.Size GetElementPixelSize(UIElement element)
{
Matrix transformToDevice;
var source = PresentationSource.FromVisual(element);
if (source != null)
transformToDevice = source.CompositionTarget.TransformToDevice;
else
using (var Hwndsource = new HwndSource(new HwndSourceParameters()))
transformToDevice = Hwndsource.CompositionTarget.TransformToDevice;
if (element.DesiredSize == new System.Windows.Size())
element.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
return (System.Windows.Size)transformToDevice.Transform((Vector)element.DesiredSize);
}
Now I am trying to follow the 3 steps above indicated, in order to update the ElementHost size. So first, I override the MeasureOverride method in my xaml.cs wpf user control, convert the size from WPF coordinates to device coordinates and finally I am trying to update the ElementHost.Size:
protected override Size MeasureOverride(Size constraint)
{
// within my wpf control I have a reference to ElementHost so I can do below
// myDlgHost is the name that my wpf user control has - I have set it through x:Name -
ElementHost.Size = this.GetElementPixelSize(this.myDlgHost);
return base.MeasureOverride(constraint);
}
But a compilation error appears when trying to update the ElementHost.Size, it says:
Cannot implicitly convert type 'System.Windows.Size' to 'System.Drawing.Size'
So anyone can indicate me how can I convert the size from WPF coordinates to device coordinates and update the elementhost size?
I am using report viewer inside a windows form and I am trying to adjust the height of the ToolStrip of the ReportViewer.
I tried to adjust the AutoSize property to false then adjust the Height but the height didn’t change:
var toolStrip = (ToolStrip)reportViewer1.Controls.Find("toolStrip1", true).First();
toolStrip.AutoSize = false;
toolStrip.Height = 100;
How can I adjust the height of Report viewer toolbar in windows form? Any suggestions would be much appreciated.
The ToolStrip of the report viewer has Dock = Fill inside a custom control (report toolbar). The report toolbar has overridden is size-related methods and properties and looks into the PreferredSize of the ToolStrip to set the bounds.
Properties like Padding, ImageScalingSize, MinimumSize, Font contribute to determining the preferred size of toolstrip; so you can set either of mentioned properties.
The most effective property is MinimumSize:
var toolStrip = (ToolStrip)reportViewer1.Controls.Find("toolStrip1", true).First();
toolStrip.MinimumSize = new Size(0, 100);
toolStrip.Parent.Height = 0; // No effect, just to force recalculation of height.
How to make a circle with text inside ?? then move it from one location to another, and then access it later (to delete it).
I want to make something like this
Your question is really very broad and you got a few nice links you should study to learn all about GDI+ drawing.
But if taken literally there is a slightly exotic alternative which puts the burdon of most chores onto the Chart control from DataVisualization.Charting.
You can create EllipseAnnotations and add them to a Chart control.
Disable the Axes and clear the Legends and then use code like this to add a moveable circle wit thext inside:
EllipseAnnotation ea = new EllipseAnnotation();
ea.X = 11; // put at..
ea.Y = 11; // 11% of the chart's area
ea.AllowMoving = true;
ea.BackColor = Color.BlanchedAlmond;
ea.Text = (chart1.Annotations.Count + 1) + "";
chart1.Annotations.Add(ea);
Note that there are quite a few annotation types available. which allow you to add Rectangles, Images, Polygons, Lines and pure Text.
And another pro is that saving or loading the graphics takes only one line each, as you can serialize a Chart out of the box!
:-)
GraphX for .NET is an advanced open-source graph layout and visualization library that supports different layout algorithms and provides many means for visual customizations It is capable of rendering large amount of vertices
https://github.com/panthernet/GraphX
To draw shapes follow here.Also you need a complete tut,you can follow here
Some insight is here:
To draw a simple shape at design time Drag the OvalShape or
RectangleShape control from the Visual Basic PowerPacks tab (to
install, see Visual Basic Power Packs Controls)in the Toolbox to a
form or container control.
Drag the sizing and move handles to size and position the shape. You
can also size and position the shape by changing the Size and Position
properties in the Properties window To create a rectangle with rounded
corners, select the CornerRadius property in the Properties window
and set it to a value that is greater than 0. In the Properties
window, optionally set additional properties to change the appearance
of the shape. To draw a simple shape at run time On the Project
menu, click Add Reference. In the Add Reference dialog box, select
Microsoft.VisualBasic.PowerPacks.VS, and then click OK. In the Code
Editor, add an Imports or using statement at the top of the module:
using Microsoft.VisualBasic.PowerPacks; Add the following code in an Event procedure:
ShapeContainer canvas = new ShapeContainer();
// To draw an oval, substitute
// OvalShape for RectangleShape.
RectangleShape theShape = new RectangleShape();
// Set the form as the parent of the ShapeContainer.
canvas.Parent = this;
// Set the ShapeContainer as the parent of the Shape.
theShape.Parent = canvas;
// Set the size of the shape.
theShape.Size = new System.Drawing.Size(200, 300);
// Set the location of the shape.
theShape.Location = new System.Drawing.Point(100, 100);
// To draw a rounded rectangle, add the following code:
theShape.CornerRadius = 12;
Customizing Shapes When you use the default settings, the OvalShape and RectangleShape controls are
displayed with a solid black border that is one pixel wide and a
transparent background. You can change the width, style, and color of
the border by setting properties. Additional properties enable you to
change the background of a shape to a solid color, a pattern, a
gradient fill, or an image. Before you change the background of a
shape, you should know how several of the properties interact. The
BackColor property setting has no effect unless the BackStyle property
is set to Opaque. If the FillStyle property is set to Solid, the
FillColor overrides the BackColor. If the FillStyle property is set to
a pattern value such as Horizontal or Vertical, the pattern will be
displayed in the FillColor. The background will be displayed in the
BackColor, provided that the BackStyle property is set to Opaque. In
order to display a gradient fill, the FillStyle property must be set
to Solid and the FillGradientStyle property must be set to a value
other than None. Setting the BackgroundImage property to an image
overrides all other background settings.
This SO link I found is also nice here
I have a PictureBox docked with Fill inside a larger control. The PictureBox is set to scale my image, but I don't want to scale the image larger than the original. Hence, my PictureBox has a maximum size set. As long as the container is smaller than the picture box, the image is fine. As the container expands beyond the maximum size of the PicutreBox, it is obvious that the picture box is tied to the top left. I would rather have the box centered vertically and horizontally in the parent. How do I make the Dock behavior fill from center rather than top left?
Use the Layout Anchor property of the PictureBox. You need to set it to "Top, Left, Bottom, Right" instead of using Dock.Fill. You can set this in the property window for the PictureBox:
Advantage of anchors against docking: the (in this case PictureBox) container can be positioned everywhere, but still be relative to other components in the parent panel/container. You can do this using Dock.Fill only with Layouts (different panels).
I think what you are looking for is the Anchor style of None, which will make the control "float" in the middle of the control. The catch though is that now you have to "initially" center it yourself:
PictureBox pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.AutoSize;
pb.Anchor = AnchorStyles.None;
pb.Image = bmp;
pb.Location = new Point((this.ClientSize.Width / 2) - (pb.Width / 2),
(this.ClientSize.Height / 2) - (pb.Height / 2));
this.Controls.Add(pb);
this.AutoScrollMinSize = pb.Size;
After further thought, I solved the issue with this line of code:
_box.SizeChanged += (sender, args) => _box.SizeMode = _box.Width < _cross.Width || _box.Height < _cross.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.CenterImage;
I'm trying to center a fixed size control within a form.
Out of interest, is there a non-idiotic way of doing this? What I really want is something analogous to the text-align css property.
At the moment, I'm setting the padding property of the surrounding form to a suitable size and setting the Dock property of the control to fill.
You could achieve this with the use of anchors. Or more precisely the non use of them.
Controls are anchored by default to the top left of the form which means when the form size will be changed, their distance from the top left side of the form will remain constant. If you change the control anchor to bottom left, then the control will keep the same distance from the bottom and left sides of the form when the form if resized.
Turning off the anchor in a direction will keep a control centered when resizing, if it is already centered. In general, a control not anchored maintains its proportional position to the dialog. E.g. If you put a control at X=75% of the dialog width and turn off left/right anchors, the control will maintain its center at X=75% of the dialog width.
NOTE: Turning off anchoring via the properties window in VS2015 may require entering None (instead of default Top,Left)
myControl.Left = (this.ClientSize.Width - myControl.Width) / 2 ;
myControl.Top = (this.ClientSize.Height - myControl.Height) / 2;
Since you don't state if the form can resize or not there is an easy way if you don't care about resizing (if you do care, go with Mitch Wheats solution):
Select the control -> Format (menu option) -> Center in Window -> Horizontally or Vertically
I found a great way to do this and it will work with multiple controls. Add a TableLayout with 3 columns. Make the center column an absolute size (however much room you need). Set the two outside columns to 100%. Add a Panel to the center column and add any controls you need and place them where you want. That center panel will now remain centered in your form.
To center Button in panel o in other container follow this step:
At design time set the position
Go to properties Anchor of the button and set this value as the follow image
In addition, if you want to align it to the center of another control:
//The "ctrlParent" is the one on which you want to align "ctrlToCenter".
//"ctrlParent" can be your "form name" or any other control such as "grid name" and etc.
ctrlToCenter.Parent = ctrlParent;
ctrlToCenter.Left = (ctrlToCenter.Parent.Width - ctrlToCenter.Width) / 2;
ctrlToCenter.Top = (ctrlToCenter.Parent.Height - ctrlToCenter.Height) / 2;
You can put the control you want to center inside a Panel and set the left and right padding values to something larger than the default. As long as they are equal and your control is anchored to the sides of the Panel, then it will appear centered in that Panel. Then you can anchor the container Panel to its parent as needed.
It involves eyeballing it (well I suppose you could get out a calculator and calculate) but just insert said control on the form and then remove any anchoring (anchor = None).
you can put all your controls to panel and then write a code to move your panel to center of your form.
panelMain.Location =
new Point(ClientSize.Width / 2 - panelMain.Size.Width / 2,
ClientSize.Height / 2 - panelMain.Size.Height / 2);
panelMain.Anchor = AnchorStyles.None;
To keep the control centered even the form or parent control were resize.
Set the following properties of the parent element (you can set it through the property window):
parentControl.AutoSize = true;
parentControl.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
Put this code in the Resize event of the form or the parent control (if the control is inside of another control).
controlToCenter.Left = (parentControl.Width- controlToCenter.Width) / 2;
controlToCenter.Top = (parentControl.Height - controlToCenter.Height) / 2;
If the parent control is docked to the form, add this line of code.
//adjust this based on the layout of your form
parentControl.Height = this.ClientSize.Height;
So, I am currently working on a pagination control and I came up with the following to achieve the below result.
Add a PanelLayout to your container (e.g. form or usercontrol)
Set the PanelLayout properties:
Dock: Bottom (or Top)
AutoSize: False
This will center the PanelLayout horizontally.
Now, in your codebehind, do something like this:
public MyConstructor()
{
InitializeComponent();
for (var i = 0; i<10; i++)
{
AddButton(i);
}
}
void AddButton(int i)
{
var btn = new Button();
btn.Width = 30;
btn.Height = 26;
btn.Text = i.ToString();
this.flowLayoutPanel1.Controls.Add(btn);
btn.Anchor = AnchorStyles.None;
}
There is a ceveat, however. If I make my form too small (horizontally) buttons will "disappear" outside of the viewport. In my case, that's not a problem, but you could take care of this by writing code that listens to the Resize event, and remove elements (buttons) based on the viewport Width.