In my application I have problem with some windows forms. They are sometimes fell down under another window.
Is there some Z-coordinate for Form? Or how is this working?
Thank you.
EDIT: I should add, that I'm using Smart Client Software Factory.
You can use the Form.Show(IWin32Window owner) method to spawn a form as a child of another form, which will always keep it above that form.
For example:
class Form1 : Form
{
public Form1()
{
InitializeComponent();
var f2 = new Form2();
f2.Show(this);
}
}
class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
When an instance of Form1 is created, it will create and show an instance of the Form2 class as a child. Form1 will be behind Form2 regardless of which form has focus.
EDIT: I took some screenshots of the effect, complete with labels that responded to the GotFocus and LostFocus events of each form to demonstrate in case the lovely blue border wasn't enough:
Related
In C# I have two forms "Form1" and "Form2". Form1 creates images that are stored in a folder. Form2 displays the number of images in that folder.
Say I have made 2 images with Form1 and then I open Form2. Form2 now says there are 2 images. Now while keeping both forms open I want to be able to add a new image and Form2 updates. At the moment if I use Form1 to add more images while Form2 is open Form2 continues to display the number of images that were in the folder when Form2 opened.
I have found solutions that involve Form2 closing and reopening but I don't want this. It's jarring for the user having windows opening and closing every time they press a button. I just want Form2 to update live as I make changes with Form1.
Any help would be greatly appreciated!! Thanks!
You can do this with public method in Form2
In Form1 you need to save Form2 object in property
FORM 1
public partial class Form1 : Form
{
public Form2 MyProperty { get; set; }
public Form1()
{
InitializeComponent();
}
// for opening form 2
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
MyProperty = f2;
}
// adding new image
private void button_ADD_Click(object sender, EventArgs e)
{
MyProperty.updateCounter();
}
}
When you add new image, then you can call the metoh from Form2 to update counter.
In FORM 2 you need crate PUBLIC method to update counter
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// default value
label1.Text = "0";
}
// update counter
public void updateCounter()
{
label1.Text = (int.Parse(label1.Text) + 1).ToString();
}
}
You may want to take a look at events and delegates. Basically, whenever you do something to Form1 that can affect other forms, you want to trigger an event. In Form2, you have a method which you call whenever that event triggers. This way you can have multiple forms open, and as long as you set them to "listen" to a given event, they can all react.
You can read more about them here, in the Microsoft Documentation.
I have a multiple forms in my project. Form1 contains a pictureBox that displays a jpeg. In Form2 I have a trackBar that I would like to control the zoom level of the image in Form1. To keep it simple I only need 2 or 3 zoom levels. I've set the pictureBox to public in the Designer view. However, when I try to reference the pictureBox in Form2 it says it doesn't exist. Below is the code I'm using to call Form2 in Form1
Form2 dataWindow = new Form2();
dataWindow.ShowDialog();
So in short the two things I need help with is:
1) Changing the properties of pictureBox1 from a separate form.
2) Creating a simple Zoom Formula.
It's considered bad design to allow other classes to modify the internal controls of the form. The form should be responsible for all of it's components. You shouldn't ever make any of the internal controls public. It's also considered bad practice for a child form to have a reference to the parent form.
The appropriate way to approach this problem is through events. The child form, Form2, should define a public event:
public event Action<int> TrackBarMoved;
Form2 can fire that event when the track bar is moved and pass, as the parameter, the position of the trackbar (if it makes sense to pass something else, such as the zoom level, or whatever else you want, that's fine too).
Form1 can the subscribe to that event when it creates From2 and change the zoom on the picture (internally, from within Form1) based on what the trackbar position is.
1) Pass a form1 reference into form2's constructor:
Form2 dataWindow = new Form2(this);
dataWindow.Show();
...
private form1 as Form1;
public Form2(Form1 frm1)
{
form1 = frm1;
}
Then in Form2s TrackBar_Scroll event reference the PictureBox via the private member variable form1: form1.PictureBox1.Property
2) Magnify your pictures using a PictureBox so that you can zoom with the Mouse Wheel
The better way is events:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.TrackBarMoved += new Action<int>(ZoomPictureBox);
form2.ShowDialog();
form2.TrackBarMoved -= new Action<int>(ZoomPictureBox);
}
private void ZoomPictureBox(int zoomFactor)
{
pictureBox1.Width = 100 * zoomFactor;
pictureBox1.Height = 100 * zoomFactor;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public event Action<int> TrackBarMoved;
private void trackBar1_Scroll(object sender, EventArgs e)
{
TrackBarMoved(trackBar1.Value);
}
}
I have a Form (form3)which could be opened from two other forms. Form1 and Form2.
how can I get which one is the parent of the form3?
The term "parent" has a very strict definition in Windows. The Form class derives from Control like all UI classes do, but it is pretty distinct, it is a top-level window. Very unlike the other controls, like Button and TextBox, they are child windows inside a parent window. The parent of a Form is the desktop window, pretty unlikely that you are interested in that one.
So it is pretty meaningless to talk about "the parent of Form3", it is the same parent as Form1 and Form2 and it doesn't help you at all to distinguish which one might have displayed the Form3 window.
Windows does have a way to associate two top-level windows with each other, it has the notion of an owner window. It is meant to implement a tool window or a dialog, an owned window is always displayed on top of its owner and is minimized along with its owner. Creating an owned window is simple:
var toolWindow = new Form3();
toolWindow.Show(this);
This Show() overload takes an argument that indicates its owner, this can be a reference to a Form1 or Form2 object, depending on where this code appears. Inside the Form3 class, you can find the owner back by using the Owner property.
Which is fairly unlikely what you are really talking about, Winforms is frequently a programmer's first introduction to object-oriented programming and dealing with object references is often confounding. If you need a reference to a logical parent in Form3 then just write the code so you pass that parent. Which you do by giving the Form3 class a constructor:
private Form logicalParent;
public Form3(Form parent) {
InitializeComponent();
logicalParent = parent;
}
And creating the window in Form1 or Form2 just takes:
var form = new Form3(this);
form.Show();
You can further improve this code in an object-oriented way by designing a base class for Form1 and Form2, one that has members in common that a class like Form3 would be interested in. Or better yet, an interface that both Form1 and Form2 implement, that reduces the coupling significantly. Last but not least, use events to allow Form3 to notify its logical parent. Probably what you are really looking for.
You can access the Parent form from the Child like this,
Say MainForm is the Form1
MainForm parent = (MainForm)this.Owner;
Or If you want to find the Parent from the hierarchy,
In the Form1 you instantiate Form2 somewhere and pass it a reference to Form1 in ctor:
Form2 f2 = new Form2(this);
In the class definition of Form2 add a field:
private Form1 m_form = null;
In the constructor of the second form set that field:
public Form2(Form1 f)
{
m_form = f;
}
Inside your Form3 you can do this:
var form1 = this.Parent as Form1;
if(form1 != null)
{
//form1.Text
}
var form2 = this.Parent as Form2;
if(form2 != null)
{
//form2.Text
}
Normally I get a name of Parent of control via
MessageBox.Show(control_Name.Parent.Name);
but now we are finding the Parent of a form, so we cannot use the same method because we don't even need to have real Parent and Client relationship to call a Form. So we will have NullReferenceException when we do
MessageBox.Show(this.Parent.Name);
in Form3.
By doing these in Form 1 and Form2 and Form3, I can call the Form3 from both forms.
Form1
Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3(this);
}
private void button1_Click(object sender, EventArgs e)
{
frm3.Show();
}
Form2
Form3 frm3;
public Form2()
{
InitializeComponent();
frm3 = new Form3(this);
}
private void button1_Click(object sender, EventArgs e)
{
frm3.Show();
}
Form3
public Form3(Form parent)
{
InitializeComponent();
}
As my conclusion, if the Parent form can only have one, then both of Form1 and Form2 is not Form3 parent Form because either one is not exist but the other one still can call Form3 out. If the Parent form can have more than one, then I think both of Form1 and Form2 are parent forms of Form3.
By the way, if the Parent Form minimized, the Child Form also minimized, then we can make Form1 as Parent Form (or actually owner form) of Form3
Form1
Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3(this);
this.AddOwnedForm(frm3);
}
OR
we set the owner of Form3 to the Form who calling it by
Form3
public Form3(Form parent)
{
InitializeComponent();
this.Owner = parent;
}
If you are not agree with me, please give me clear definition of Parent form and Child form.thanks. I try to explain from my point of view and hope someone correct me if I am wrong.
Hi I am using windows forms in C#. I am trying to modify the visible property of a picture from main form to another. Initially, the visible property of the picture box is set to false. On a button click from another form, the visible property of the picture box is modified to true.
This is the code written in the Form2 method:
private void button_Click(object sender, EventArgs e)
{
public Form1 frm1 = new Form1();
frm1.pictureBox.Visible= true;
}
Form1 is an instance type, so when you do
public Form1 frm1 = new Form1();
frm1.pictureBox.Visible= true;
you're really just creating a new instance of Form1 completely unrelated from your original Form1, changing a picture-box's visible property on it, and then discarding it.
What you can do, is put a reference to the "parent" Form1 inside your Form2 class.
Here's an example
public partial class Form2 : Form
{
public Form2(Form1 parent)
{
InitializeComponent();
this.Parent = parent;
}
Form1 Parent;
private void button1_Click(object sender, EventArgs e)
{
Parent.pictureBox.Visible= true;
}
...
}
there you create an instance of a form :
public Form1 frm1 = new Form1();
This is then obviously NOT the form you already may have in your page, which you could simply access by its ID.
According to your written code it will create new instance of the desired form, and NOT take the existing open form. Hence to identify the existing open form containing target picture box you need the target form and controlling form be related by like Parent form or MDI Parent Form.
Assuming case of MDI Parent Form (i.e. Controlling form is MDI Parent of Target Form), you need following codes to identify to existing open form:
foreach (Form frm in MdiChildren)
{
if (frm is myTargetForm)
{
//do your code to find control using id of picture box and change the required properties
}
}
Hello guyes i have one problem i have 1 parent form and 3 children i just want to open them maximized but when i do that in left side comes this 3 controls. How can i open one form without this controls.
If im doing this with wrong way please advice me something does mdi good for such things?
please see this pictures http://img440.imageshack.us/img440/6831/mdinz.jpg
http://img139.imageshack.us/img139/4687/mdi1.jpg
This is a known bug in the MDI implementation, triggered when you create a maximized child window in the parent constructor. This is an example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var child = new Form2();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Show();
}
}
You'll see the min/max/restore glyphs displayed twice, restoring the child window leaves the MDI bar on the screen, just as in your first screen shot. The workaround is to move the child creation code to the OnLoad() method. Like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
var child = new Form2();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Show();
}
}
You can use the ControlBox, FormBorderStyle, MaximizeBox and MinimizeBox properties to remove the various window UI elements from a form, if you wish.