How can I access the event that is triggered when a graph pane is resized?
After each resize, I want to fix the text size on the title and labels so they don't get huge when the window is maximized.
You can subscribe for ZedGraphControl's resize event:
zg1.Resize += new EventHandler(zg1_Resize);
But it's easier to achieve what you want by disabling the automatic font scaling on the GraphPane:
zg1.MasterPane[0].IsFontScaled = false;
If you have more than one GraphPane on your MasterPane, use:
foreach(GraphPane pane in zg1.MasterPane.PaneList)
pane.IsFontScaled = false;
See also:
http://zedgraph.org/wiki/index.php?title=How_does_the_font_and_chart_element_scaling_logic_work%3F
http://zedgraph.sourceforge.net/documentation/html/P_ZedGraph_PaneBase_IsFontsScaled.htm
Related
In the Form Load event I'm adding some controls to the Form dynamically.
I use this code to resize the Form to adapt its Size to the its new content:
this.MaximumSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
It works fine but StartPosition = FormStartPosition.CenterScreen doesn't work as expected:
the Form center position is calculated based on the original size.
My suggestion is to resize the Form using it's PreferredSize property. When you have added new Controls to the Form and these Controls don't fit in client area, the PreferredSize is updated to reflect the new size needed to contain them all, but limited to the Form's MaximumSize you have set.
Then use the CenterToScreen() method to center the Form to the current Screen.
CenterToParent(), suggested by Hans Passant in comments, has the same effect if the Form is not parented to any other Form: i.e., if it's not shown using the .Show(this) method overload; otherwise, it's centered to the Owner Form.
Setting AutoSizeMode = AutoSizeMode.GrowAndShrink (or calling the SetAutoSizeMode() method) and then setting AutoSize = true can have unwanted results. The more visible is that you cannot resize your Form with normal means anymore. The area occupied by a StatusStrip may not be considered etc.
As a note, if your application is not DpiAware, the Screen measures and your Form's Size may be different from what is expected. In case it's not, read about it here:
High DPI support in Windows Forms
These notes about the Screen and VirtualScreen may also be useful:
So, add your controls to the Form (maybe in the Load handler) then:
var screen = Screen.FromHandle(this.Handle);
this.MaximumSize = new Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
this.ClientSize = this.PreferredSize;
this.CenterToScreen();
I created a .NET Framework 4.0 winforms project and added a "tab" as you can see in the following picture:
From the View Designer both tabs (Profile and Features) looks like they fit the rectangle window, but when I run the program, the "Profile" tab looks the same but the "Features" tab suddenly does not fit the rectangle anymore and looks like that:
I don't want to give the user the option to enlarge the window.
I am wondering why it looks like it fit the rectangle window but when I run the application, it does not.
This is the "Features" tab from the MainForm:
this.TabPage2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.TabPage2.Controls.Add(this.listBoxShowFriendsBirthday);
this.TabPage2.Controls.Add(this.buttonShowFriendsBirthday);
this.TabPage2.Controls.Add(this.label1);
this.TabPage2.Controls.Add(this.monthCalendar1);
this.TabPage2.Controls.Add(this.postTextBox);
this.TabPage2.Controls.Add(this.pictureBoxEvent);
this.TabPage2.Controls.Add(this.listBoxFetchEvents);
this.TabPage2.Controls.Add(this.buttonFetchEvents);
this.TabPage2.Controls.Add(this.postButton);
this.TabPage2.Location = new System.Drawing.Point(8, 39);
this.TabPage2.Name = "tabPage2";
this.TabPage2.Padding = new System.Windows.Forms.Padding(3);
this.TabPage2.Size = new System.Drawing.Size(1976, 806);
this.TabPage2.TabIndex = 1;
this.TabPage2.Text = "Features";
this.TabPage2.UseVisualStyleBackColor = true;
I suggest you to use Dock and Anchor properties of controls. This way you can also allow to user resizing the window.
Dock:
Anchor:
See - How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control
To prevent the user from rescaling their window:
c# how to prevent user from resizing my application window?
but aside from that, I'd suggest, (just like ikram said) to use docking, that way you're certain your lay-out will remain, no matter the resolution/screensize the user has.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
pictureBox1.Dock = DockStyle.Fill;
}
What code I can applied on picturebox2 so that picturebox2 also displaced according to form ...
You can use the Dock and Anchor properties of the controls to determine their behavior when the form is resized. When using docks and anchors in WinForms, you typically decide for a primary control (group) that gets the main part of the screen and another group of controls that are aligned in the remaining area. So if you set DockStyle.Fill for PictureBox1 control, you set the other PictureBox to DockStyle.Right. When the form is resized, the main area is extended. Please note however, that it sometimes depends on the order that the controls were created on how they are aligned and whether it works as expected. It might take some experimenting with putting various controls to foreground in order to reach your goal.
This link lists a lot of tutorials on how to align controls on Windows Forms, specifically on setting anchors and docking controls.
In addition, you can use various layout controls, among them a TableLayoutPanel (Thanks #HansPassant for the hint). For a walk-through see this link.
You need to set anchors to all your controls on your form( by default all your controls are "tied" to the top and left of your form) . If anchors are not enough try using dock panels and dock your control.
You can se the anchors from the visual editor. Select a control and on the properties panel you should set the anchors.
Here you have to scale the child controls as the main window is scaled.
try the Scale method by calculating of the scale factor as below:
the code:
Size st = this.Size;
int height = st.Height;
int width = st.Width;
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
Size newSize = this.Size;
SizeF scaleFactor = new SizeF();
scaleFactor.Height = newSize.Height / height;
scaleFactor.Width = newSize.Width / width;
this.Scale(scaleFactor);
I have a homework, where I need to create a winforms game using C#. I have the following components:
Panel subclass with custom paint event
Panel with default windows UI elements.
I want them to arrange like this:
Because I draw on the center panel manually, I want to set it's Width, and Height fixed, so the Form subclass, what will contain it, would show the whole panel.
I tried setting the size manually in the panel subclass:
Width = someFixedWidth;
Height = someFixedHeight;
Then adding it to the containing Form:
GamePanel panel = new GamePanel(...);
panel.Dock = DockStyle.Center;
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.Controlls.Add(panel);
Using this, I thought, that the form will respect the size of the Panel, but it just shrinks the window to so small, that nothing is visible, only the title.
So my question is, how would I be able to set the size of the GamePanel manually, and then dock it in the center of the form, so that the Form will respect the size I set, and doesn't makes it smaller/bigger?
The Dock property is used to define the behavior of the component during resizing Container (Form) The way you did the screen is not centralized but is resized according to the screen changes, the ideal is to use a method to reposition the control and set its size. See this:
SuspendLayout();
Width = someFixedWidth;
Height = someFixedHeight;
panel.Size = new Size(panelWidth, panelHeight);
panel.Location = new Point( ClientSize.Width / 2 - panelWidth / 2, ClientSize.Height / 2 - panelHeight / 2);
panel.Anchor = AnchorStyles.None;
panel.Dock = DockStyle.None;
ResumeLayout();
In my case, I edited minimum height/width of the panel and it worked.
I tried to edit the code which related to design but it was not recommended to rewrite auto-generated code.
Thank you.
I have a picturebox that has a mousehover event to show a tooltip based on the status of a service. This seems to be working, but it kind of just pops up where the mouse is and sometimes under the mouse, in the middle of the picture, which doesn't look right. I was reading http://msdn.microsoft.com/en-us/library/windows/desktop/aa511495.aspx#infotipsgl and it suggested to have the tooltip moved off to the side. This would be great, but I can't figure out how to move it.
ToolTip on toolTip1 is blank and on the mouseHover event I have tried using
toolTip.SetToolTip(this.pictureBox1, "Message text.");
and
toolTip.Show("Message text.", pictureBox1);
Thanks
ToolTip tooltip = new ToolTip();
tooltip.Placement = PlacementMode.Right;
tooltip.PlacementRectangle = new Rect(50, 0, 0, 0);
tooltip.HorizontalOffset = 10;
tooltip.VerticalOffset = 20;
See Here for more details.
For windows forms you can use this overload of Show method. It allows you to set position offset relative to control that has tool tip.
In wpf as Ravi Patel has already pointed you to article simply us:
<ToolTip HorizontalOffset="10"
VerticalOffset="20" .../>