I have a form acting as a sidebar, that will always be locked in position, and width. However, if I were to resize the MDI Container, this form should also grow to fit the Parent form length-ways.
For example, if I were to use a method in the Parent form like so:
private void ParentForm_SizeChanged(object sender, EventArgs e)
{
FormWindowState LastWindowState = FormWindowState.Normal;
if (this.WindowState != LastWindowState)
{
if (this.WindowState == FormWindowState.Maximized)
ResizeChildForm(this);
}
}
And then in the Child form, if I have the property Locked = true; will I have to disable that, resize the form, then enable that again? I.e.
private void ResizeChildForm(ParentForm)
{
this.Locked = false;
//resize form
this.Locked = true;
}
Or can I just change the size of the form without having to change that property?
To answer your question,
can I just change the size of the form without having to change that property?
Yes you can, see this link for more details.
Note from the article :
Locking controls prevents them from being dragged to a new size or location on the design surface. However, you can still change the size or location of controls by means of the Properties window or in code.
Related
I work on MDIParent with background image after i move a child from in it the background image crush like this
how can i solve this issue in C# winforms??
The MDIClient Control is the object used, in Multiple-Document Interface, as the Container for all child Forms.
This Control has a BackGroundImage property. You can set an Image object using this property, instead of using the [MDIParent].BackGroundImage. This would actually solve the Image tearing problem.
But, you can't set a specific Layout property. See the Docs about the MDIClient BackgroundImageLayout:
This property is not relevant to this class.
You can set this property, but it's ignored: the default ImageLayout.Tile is used instead.
A different Layout can be set, assigning the Image object to the MDIParent BackGroundImage and specifying a BackGroundImageLayout. This will change the Layout, but it will also cause the tearing effect you are reporting.
A possible solution is to draw the Image object on the MDIClient surface, using it's Paint() event as usual.
This will solve the tearing effect. Not the flickering; this can be noticed when you resize the MDIParent Form (well, MDI applications are not resized that often, maybe maximized and normalized).
Some flickering can be seen when the background Image is not covered by a child Form.
A small adjustment to the Image specs is required: setting its DPI resolution = to the MDIParent reported device context DPI, otherwise the Image size will not match the default size (it's affected by the DPI resolution).
See this post for a description:
Image is not drawn at the correct spot.
An example:
(Here, I assume the background Image is loaded from the Project resources)
public partial class MDIParent : Form
{
private MdiClient mdiBackground = null;
private Bitmap BackGroundImage = null;
public MDIParent()
{
InitializeComponent();
//Specify an existing Resources' Image
BackGroundImage = new Bitmap(Properties.Resources.[Some Res Image] as Bitmap);
BackGroundImage.SetResolution(this.DeviceDpi, this.DeviceDpi);
}
private void Form1_Load(object sender, EventArgs e)
{
mdiBackground = this.Controls.OfType<MdiClient>().First();
mdiBackground.Paint += (s, evt) => {
evt.Graphics.DrawImage(BackGroundImage,
(mdiBackground.Width - BackGroundImage.Width) / 2.0F,
(mdiBackground.Height - BackGroundImage.Height) / 2.0F);
};
//Show some child Forms on start-up
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.mdiBackground != null) mdiBackground.Invalidate();
}
private void MDIParent_FormClosed(object sender, FormClosedEventArgs e)
{
if (BackGroundImage != null) BackGroundImage.Dispose();
}
}
I'm going to be short and sweet with only needed information. I'm making an application which has multiple forms displayed in a MDI parent.
I want my users to be able to resize this window and the things i place inside each form resize and move when they resize the window. I do not know how to do this. When i anchor the picture box containing my image to the sides of the form i'm assuming it would work but when you run the application you are not resizing the form, you are resizing the MDIparent form which does not affect the image.
Help.
Edit: removed useless code.
MDIparent code:
private void ribbonButton1_Click(object sender, EventArgs e)
{
foreach (Form f in this.MdiChildren)
{
if (f.GetType() == typeof(Form2))
{
f.Activate();
return;
}
}
Form form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
Form2 that is the MDICHILD:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.ControlBox = false;
this.WindowState = FormWindowState.Maximized;
this.BringToFront();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
When the MDI parent is resized, the child form sizes of the MDI will not be affected. You will need to do this manually. So, anchoring is the right solution to the child form. Then you will need to set the sizes of the children manually on the resize event of the parent, according to the parent's size. This is all I can say because your post is not clear. Maybe some screenshots could help.
I have a form that i set it's Opacity as 50% like this:
this.Opacity = 0.5D; <--this==Form
My problem is that everything that on the form is with an Opacity of 50%
I have two buttons on the form and I want them without Opacity.
I know that this.Opacity included all Controls and for some reason the graphics too
My question is, How to Exclude the Opacity of the controls?
Example Image:
Thanks!
Since Control doesn't have Opacity property and plus that, most of the controls doesn't support transparent colors, then a working solution can be this:
Create a Form called MainForm and place all the controls you're going to be excluded.
1.1 Set both of BackColor and TransparencyKey properties of MainForm to the same color, e.g Color.Red
Create another form named TransparentForm and place all controls that must become transparent. Set ShowInTaskbar property to False.
In the MainForm Load event show the TransparentForm and send it to back.
private void MainForm_Load(object sender, EventArgs e)
{
TransparentForm form = new TransparentForm();
form.Opacity = 0.5D;
form.Show();
form.SendToBack();
}
The position of controls in both form must be such that, when combined, it shows the proper user interface.
Crate a C# project and add 3 forms named
MAIN
BACKGOUND
Child
and add the following code for "MAIN" form load event;
private void MAIN_Load(object sender, EventArgs e)
{
Child frm1 = new Child();
BACKGOUND frm2 = new BACKGOUND();
frm2 .WindowState = System.Windows.Forms.FormWindowState.Maximized;
frm2.Opacity = 0.5;
frm2.Show();
frm1.ShowDialog();
frm2.Close();
}
Please guide and help me.
I have a MDI parent form which has a label at its center (to display application name in center). On opening a form in this MDI parent, this label should appear on back side of newly opened form, but on showing a child form, label appears in front of newly opened form (appears like newly opened form is between label and MDI parent).
How to manage it please guide me.
thanks
This will hide the label while you have active MDI Children en show it again once there is no active child anymore.
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
label1.SendToBack();
else
label1.BringToFront();
}
I hope this helps.
public partial class MyMdiForm : Form
{
public MyMdiForm()
{
InitializeComponent();
foreach (Control control in Controls)
{
if (control is MdiClient)
control.Paint += mdiBackgroundPaint;
}
}
private void mdiBackgroundPaint(object sender, PaintEventArgs e)
{
var mdi = sender as MdiClient;
if (mdi == null) return;
e.Graphics.Clip = new System.Drawing.Region(mdi.ClientRectangle);
e.Graphics.DrawString("*** YOUR NAME HERE ***",this.Font,Brushes.Red,100F,100F);
}
}
The problem is that your label is not added to the MdiClient (i.e. the grey Mdi container) but to the form.
But unfortunately, AFAIK, it's not possible to add controls to an MdiClient.
The only way is drawing what you want on the Paint event of the MdiClient, as suggested in this article:
http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp
Well, I did one trick and it works for me. We usually write an application name in the center and expect it to show to the user. And many people here said that MdiParent is only for Forms and not for other tools like we cannot hide label/panel behind MdiChild form.
So what I did is I wrote all the things like application name, contact, email etc etc etc in a new Form say frmMdiBody, Set its formBorderStyle = None and set the desired length of the form, StartPosition = CenterScreen and in Timer.Tick, I wrote the following : (Didn't work for me in Load event)
Dim NewMDIChild As frmMdiBody = MdiChildren.OfType(Of frmMdiBody)().SingleOrDefault
If NewMDIChild Is Nothing Then
NewMDIChild = New frmMdiBody
NewMDIChild.MdiParent = Me
NewMDIChild.Show()
End If
This above code also checks if there is one form open so that it won't open many frmMdiBody again and again as we are writing in Timer.Tick event
Someone can rectify me if I am wrong. I'd do the changes too if seems appealing.
Ok heres my problem. I have a form that when it is not maximised, its maximum size has to be the total height of the components inside the form. To achieve this, i use this:
private void resize_form(object sender, EventArgs e)
{
this.MaximumSize = new System.Drawing.Size(1000, this.panel4.Height + this.label2.Height + this.HeightMin);
}
That fires on the Resize event of the form. Because the component size is always changing it made sense to do this on a resize event. How ever if i want to maximise the form, the form just goes to the highest settings defined in this.MaximumSize. So i was wondering is there a way to tell when a form is going to be maximised and set its maximumsize to the screen boundarys before the form maximises.
If there is a better way to change the maximumsize value without resize event, that would also be great :)
You still need to use the resize event, but check the WindowState:
if (this.WindowState == FormWindowState.Maximized)
{
// Do your stuff
}
As yshuditelu points out you can set the minimum size property of your form too - which should, when coupled with judicious use of anchor values, mean that it can never shrink too far and when it does grow the components will move and/or grow as required.
I found the answer that suited me perfectly. A lil WndProc override :D (i love WndProc now)
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
switch (message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MAXIMIZE)
{
this.maximize = true;
this.MaximumSize = new System.Drawing.Size(0, 0);
}
break;
}
base.WndProc(ref message);
}
private void resize_form(object sender, EventArgs e)
{
if (!maximize)
{
this.MaximumSize = new System.Drawing.Size(1000, this.panel4.Height + this.label2.Height + this.HeightMin);
}
}
Basically it sets this.maximize to true when it receives teh SC_MAXIMIZE message. The resize event will only set a new MaximumSize if this.maximize is set to false. Nifty xD
Are you sure you don't want to be setting the MinimumSize property? If you set the MinimumSize to the size of all the labels, then the form will never be smaller than that. But it can still grow to whatever size the user wants, so long as it is larger than the minimum.
Check out the System.Windows.Forms.Screen class. Get the screen from a relevant point (to handle the multi-mon case) and then get its resolution.
This should work in conjunction with the other comment about checking FormWindowState.Maximized.
If the user clicks in the upper bar, they can resize the window, so I use this:
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
}
}