Adjust the height of Report viewer toolbar in windows form - c#

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.

Related

Form loads with the minimal size (which displays only the title bar) rather than the size corresponding to the value set to its properties

I have a form which contains a Menu Strip docked to the top, a Status Strip docked to the bottom, and a Panel docked to fill the entire space between the aforementioned controls. I have set the attributes to the following values for the form:
During the design phase:
AutoScaleMode: Dpi
AutoSize: false
AutoSizeMode: GrowOnly
DoubleBuffered: true
SizeGripStyle: Show
During runtime (in the form's constructor):
// Calculate the default size of the window on the basis of the ratio of the dimensions of the window to the dimension of the screen resolution of the machine used in development as the default dimensions of the window is aligned to that of the machine used to design it
this.Size = new Size(Screen.GetWorkingArea(this.Location).Size.Width * (widthOfWindowInDesignPhase /horizontalResolutionOfTheDisplayInDesignPhase), Screen.GetWorkingArea(this.Location).Size.Height * (heightOfWindowInDesignPhase / verticalResolutionOfTheDisplayInDesignPhase));
this.MinimumSize = new Size(this.Size.Width, this.Size.Height);
My first attempt to resolve this issue was to tinker around with the AutoSize and AutoSizeMode properties, but I require this to be set to the aforestated values as changing them would not allow the user to resize the form. The other approach I tried, which also failed, was by setting the AutoSize properties of the aforementioned controls to false as to force the child containers of the form to not resize.
Thanks in advance.
PS Screenshot of the concerned form:
A (blunder) numeric error occurs when calculating the size of the form as the statements (widthOfWindowInDesignPhase /horizontalResolutionOfTheDisplayInDesignPhase) and (heightOfWindowInDesignPhase / verticalResolutionOfTheDisplayInDesignPhase) return a floating point value that cannot be stored as an integer.
The appropriate statement is as follows:
this.Size = new Size(Convert.ToInt32(Screen.GetWorkingArea(this.Location).Size.Width * (Convert.ToDouble(widthOfWindowInDesignPhase) / Convert.ToDouble(horizontalResolutionOfTheDisplayInDesignPhase))), Convert.ToInt32(Screen.GetWorkingArea(this.Location).Size.Height * (Convert.ToDouble(heightOfWindowInDesignPhase) / Convert.ToDouble(verticalResolutionOfTheDisplayInDesignPhase))));

Change Label Font Size Dynamically Not Updating On Screen

Am trying to change a label font size depending on screen resolution. Have tried when the form is loading, shown and also in the form constructor, but on screen the font size is the same as design time.
Rectangle resolution = Screen.PrimaryScreen.Bounds;
if (resolution.Width == 1024 && resolution.Height == 768)
{
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f);
}
I've added a double click event to the label to check the font size, and it says it's 40 in a message box (MessageBox.Show(this.labelEnterRegistration.Font.ToString());), so why doesn't the form display reflect this?
I have tried the label Invalidate()but that didn't work either.
Have fixed it. As it was done before it was setting the fonts emSize, I did the following so it changes the pixel size:
FontStyle style = this.labelEnterRegistration.Font.Style;
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f, style, GraphicsUnit.Pixe
And now keeps the same font style as well!!
Thanks for the comments #HEPİMİZYARBAYMEHMETALKANIZ, made me think about it some more.

Paned Window in Visual C#

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:

Centering controls within a form in .NET (Winforms)?

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.

Zoom for a windows form in C#

Is there an easy way to set the zoom level for a windows form in C#? In VBA there was a zoom property of the form.
I had the same problem and I solved it this way in c#. Code goes on Form load
float scaleX = ((float)Screen.PrimaryScreen.WorkingArea.Width / 1024);
float scaleY = ((float)Screen.PrimaryScreen.WorkingArea.Height / 768);
SizeF aSf = new SizeF(scaleX, scaleY);
this.Scale(aSf);
This "more or less" scales form and all children. Loops forever in 800x600 (?)
You have to set the following Form properties:
AutoscaleMode = Font
AutoSize = False
You can get some kind of zoom by assigning different Font to the Form, all the controls will be zoomed accordingly if AutoScaleMode set to Font. Also settings AutoSize to False will keep form size intact, the controls will grow to the center of the form. You need to set up all Anchors correctly and test the look, since its just "kind of zoom".
So basically here is sample constructor:
public Form1()
{
InitializeComponent();
AutoSize = false;
AutoScaleMode = AutoScaleMode.Font;
Font = new Font("Trebuchet MS",
10.0f,
FontStyle.Regular,
GraphicsUnit.Point,
((byte)(204))
);
}
After form has been shown assigning new Font will mess up all the controls and this trick will not work.
There is no way (that I know of) to do what you ask with typical WinForms.
If you're doing custom painting/drawing, you can zoom that by using a zoom transform, but so far as I know there is no "Zoom" property for the form in the entire world of .NET and native Windows/C++ APIs combined.
You could probably rig something yourself such that you scale controls by a constant factor. And you can probably find 3rd-party controls/surfaces which support this. And who knows what is possible with WPF. But in a typical WinForms world, no.

Categories