How to disable a form resize at design time? - c#

In my application I am deriving all my forms from a common BaseForm.
Now I need to disable the resizing in the BaseForm so that derived forms are not resizable at design-time.
How to achieve that?
I need it at design-time

This seems to work:
[BaseForm.cs]
namespace WindowsFormsApplication1
{
using System.Windows.Forms;
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
this.MaximumSize = this.MinimumSize = this.Size;
}
}
}
[DerivedForm.cs]
namespace WindowsFormsApplication1
{
public partial class DerivedForm : WindowsFormsApplication1.BaseForm
{
public DerivedForm()
{
InitializeComponent();
}
}
}

i used
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this make window form as like dialogBox. so dialog boxes are not resizable by user.

Use the following:
this.FormBorderStyle = FormBorderStyle.FixedSingle;

If you go into design view and look in the form's properties menu, there is a Locked property, which disables resizing of the form.
EDIT
Try setting the MaximumSize and MinimumSize properties to the same value.

Related

How to call forms from another class?

I want to call a chart from another class. The code of the chart is this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
chart1.Series["S1"].Points.AddXY(0, 0, 10);
chart1.Series["S1"].Points.AddXY(0, 0, 10);
}
}
and I want to call this chart from another different class, I tried that:
Form1 chart1 = new Form1();
chart1.Show();
Thanks!
For access forms class and controls from another class, there are simple and more secure methods(link) to do what you want :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
chart1.Series["S1"].Points.AddXY(0, 0, 10);
chart1.Series["S1"].Points.AddXY(0, 0, 10);
}
}
Form chart1 = (Form)Application.OpenForms["Form1"];
//do here what you want
I am assuming you want to access a specific Form1. Not a new one. You can give a reference to it to the other class or make a reference to it static. For example create a field in Form1 like this:
public static Form1 Chart;
and in the constructor do Chart = this; Then you can access it anywhere with Form1.Chart.

Load an OpenTK GameWindow inside a Windows form panel?

I have an application made in C# using OpenTk.
this is in a class called ViewPort, and is initialized like this:
namespace OpenTK_viewport
{
class ViewPort : GameWindow
{
.....
my Program.cs is:
[STAThread]
static void Main()
{
using (ViewPort main = new ViewPort())
{
main.Run(60.0);
}
}
This works great, and opens the game window. Now, I want to embed this window inside a UserControl, so i can load it into a panel on an existing Form.
For example, in the project, I have:
namespace OpenTK_viewport
{
public partial class Form3dViewport : UserControl
{
public Form3dViewport()
{
InitializeComponent();
}
private void Form3dViewport_Load(object sender, EventArgs e)
{
}
}
}
I would like to embed the ViewPort class inside this UserControl.
I can't find an example of this anywhere. How can i go about this?
EDIT:
I am able to add a GLControl to the Form instead of a panel. Can I load the Viewport class into this?
Thank you.
Ah, I was looking at it wrong. OpenTK can be run straight inside a Form. As illustrated here:
https://github.com/mono/opentk/blob/master/Source/Examples/OpenTK/GLControl/GLControlGameLoop.cs

C# winform, How to create a form ContainerControl?

Recently I have been researching on how to make your own control. And I have asked in the past (2 days ago) about a Tab Control issue that I am having. Well now I want to create a ContainerControl. Something like the form in the MetroFramework because I want to develop my own custom looking form. So fair this is what I have of the form.
now the problem that I am having is that this is made inside of the form. There isn't a ContainerControl where I just type in like 1 line of code and my form will look like that. In this solution I will need to type in about 30 lines. So when I mean by container control, you change the form object in the form.cs coding and it will change the design of the form.
Example for metro framework.
public partial class Form1 : MetroFramework.Forms.MetroForm
Now if I wanted to create a custom form like the one in the picture. I don't want to add 30 - 50 lines of code per form to do that. I just want to make one form class and then add where the "MetroFramework.Forms.MetroForm" is and replace it with my form. For example
public partial class Form1 : MyFormExampleNamespace.MyForm
And this is what the regular c# winform class structure looks like
public partial class Form1 : Form
So now that I explained what I want, define what I think a form ContainerControl is, and what I want. How could I accomplish my goal? How would I make a ContainerControl. This is what I have tried before
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GoatUserControls
{
public class GoatForm : Form
{
public GoatForm()
{
this.FormBorderStyle = FormBorderStyle.Fixed3D;
base.FormBorderStyle = FormBorderStyle.Fixed3D;
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// GoatForm
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "GoatForm";
this.Load += new System.EventHandler(this.GoatForm_Load);
this.ResumeLayout(false);
}
private void GoatForm_Load(object sender, EventArgs e)
{
}
//protected override BorderStyle()
// {
// }
}
}
And this is what I have in the main form
public partial class Form1 : GoatUserControls.GoatForm
So how would I make a form ContainerControl?

User Control as container at design time

I'm designing a simple expander control.
I've derived from UserControl, drawn inner controls, built, run; all ok.
Since an inner Control is a Panel, I'd like to use it as container at design time. Indeed I've used the attributes:
[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
Great I say. But it isn't...
The result is that I can use it as container at design time but:
The added controls go back the inner controls already embedded in the user control
Even if I push to top a control added at design time, at runtime it is back again on controls embedded to the user control
I cannot restrict the container area at design time into a Panel area
What am I missing? Here is the code for completeness... why this snippet of code is not working?
[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ExpanderControl : UserControl
{
public ExpanderControl()
{
InitializeComponent();
....
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
internal class ExpanderControlDesigner : ControlDesigner
{
private ExpanderControl MyControl;
public override void Initialize(IComponent component)
{
base.Initialize(component);
MyControl = (ExpanderControl)component;
// Hook up events
ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));
s.SelectionChanged += new EventHandler(OnSelectionChanged);
c.ComponentRemoving += new ComponentEventHandler(OnComponentRemoving);
}
private void OnSelectionChanged(object sender, System.EventArgs e)
{
}
private void OnComponentRemoving(object sender, ComponentEventArgs e)
{
}
protected override void Dispose(bool disposing)
{
ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));
// Unhook events
s.SelectionChanged -= new EventHandler(OnSelectionChanged);
c.ComponentRemoving -= new ComponentEventHandler(OnComponentRemoving);
base.Dispose(disposing);
}
public override System.ComponentModel.Design.DesignerVerbCollection Verbs
{
get
{
DesignerVerbCollection v = new DesignerVerbCollection();
v.Add(new DesignerVerb("&asd", new EventHandler(null)));
return v;
}
}
}
I've found many resources (Interaction, designed, limited area), but nothing was usefull for being operative...
Actually there is a trick, since System.Windows.Forms classes can be designed (as usual) and have a correct behavior at runtime (TabControl, for example).
ParentControlDesigner doesn't know what you want do. It only knows you want your UserControl to be a container.
What you need to do is implement your own designer which enables design mode on the panel:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace MyCtrlLib
{
// specify my custom designer
[Designer(typeof(MyCtrlLib.UserControlDesigner))]
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
// define a property called "DropZone"
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel DropZone
{
get { return panel1; }
}
}
// my designer
public class UserControlDesigner : ParentControlDesigner
{
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
if (this.Control is UserControl1)
{
this.EnableDesignMode(
(UserControl1)this.Control).DropZone, "DropZone");
}
}
}
}
I learned this from Henry Minute on CodeProject. See the link for some improvements on the technique.
In addition to the answer above. It is mentioned in the comments, that the user is able to drag the WorkingArea. My fix for that is to include the WorkingArea panel in another panel, setting it to Dock.Fill. To disallow the user to change it back, I have created a class ContentPanel that overrides and hides the Dock property:
class ContentPanel : Panel
{
[Browsable(false)]
public override DockStyle Dock
{
get { return base.Dock; }
set { base.Dock = DockStyle.Fill; }
}
}
For me, this makes it sufficiently safe. We are only using the control internally, so we mainly want to prevent developers from accidently dragging things around. There are certainly ways to mess it up anyway.
To prevent the working area from being moved/resized in the designer you have to create a class for that working area that hides the Location, Height, Width, Size properties from the designer:
public class WorkingArea : Panel
{
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Point Location
{
get
{
return base.Location;
}
set
{
base.Location = value;
}
}
...
}

How do I tell the designer that my custom winforms control has a fixed height?

I've made a custom control and overridden SetBoundsCore such that the height of the control is fixed. I'd like the designer to show the same sort of resize boxes as the NumericUpDown has - just one at each end - so that it's clear that the control has a fixed height. How do I tell the designer that my control has a fixed height?
You have to apply a Designer attribute to your UserControl:
[Designer(typeof(UCDesigner))]
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
}
The UCDesigner class is defined as follow:
class UCDesigner : System.Windows.Forms.Design.ControlDesigner {
public override System.Windows.Forms.Design.SelectionRules SelectionRules {
get {
return (base.SelectionRules & ~(SelectionRules.BottomSizeable | SelectionRules.TopSizeable));
}
}
}
Note: You'll have to add a reference to the System.Design namespace.

Categories

Resources