I am using this block of code to show other Form1 as a dialog
private void ShowDialogWindow<T>() where T : Form, new()
{
Cursor.Current = Cursors.WaitCursor;
using (Form form = new T())
{
form.ShowDialog(this);
}
}
ShowDialogWindow<Form1>();// loads form1 as a dialog form
The code below is sort of a transparent borderless form that will add a transparent effect to parent form when the child form is active
namespace PopupEffect
{
public partial class transparentBg : Form
{
public transparentBg(Form parent, Form child)
{
InitializeComponent();
_child = child;
this.Location = parent.Location;
this.Size = parent.Size;
this.ShowDialog();
}
public transparentBg(Form child)
{
InitializeComponent();
_child = child;
this.WindowState = FormWindowState.Maximized;
this.ShowDialog();
}
_ = new PopupEffect.transparentBg(this, new Form1());//add the fade effect to the parent form when the child form is active
how can i add the transparent block of code to this ShowDialogWindow()
Related
I'm trying to use use the code form.mainPnl.Controls.Add(background); where mainPnl is a panel I have added to Form1 that im just using for a base, and background is being created during debug.
var background = new Panel
{
Name = "background",
Dock = DockStyle.Fill,
BackColor = Color.Black,
};
Im working in a side class (Not sure what it's called. It's just not the base class) which means I cant use anything from the mainPnl panel without getting it from Form1 so I need to use Form1 form = new Form1(); but whenever I use this I think it's looping and crashing the program.
Main Class:
public partial class Form1 : Form
{
public Panel mainPnl
{
get
{
return mainPanel;
}
}
public TextBox textbox
{
get
{
return hey;
}
}
Menu menu = new Menu();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
menu.Main();
}
private void Update_Tick(object sender, EventArgs e)
{
}
}
Side Class:
class Menu
{
Form1 form = new Form1();
public void Main()
{
createObjects();
}
public void createObjects()
{
var background = new Panel
{
Name = "background",
Dock = DockStyle.Fill,
BackColor = Color.Black,
};
form.mainPnl.Controls.Add(background);
}
}
Thats the only code. But when I run the code, it shows an error and highlights that code and says 'Exception of type 'System.StackOverflowException' was thrown'
In your form you have:
public partial class Form1 : Form
{
Menu menu = new Menu();
}
And in your Menu class you have:
class Menu
{
Form1 form = new Form1();
}
And when you instantiate your form, it creates a menu which instantiates a new form which creates a new menu and so on until you run out of stack space for the mutually recursive constructors.
I'm not sure what exactly you're trying to do or what your question is other than why you're getting a stack overflow, so here you go!
Recursion! StackoverflowExeption usually happens because of recursion (in my experience)
public partial class Form1 : Form
{
Menu menu = new Menu(); // Creates new Menu
}
class Menu
{
Form1 form = new Form1(); // Creates new Form1
}
My codes to change MdiChild in MdiParent class
public void SetupMdi(Form form)
{
clearMdi();
activeMdiForms.Add(form);
form.MdiParent = this;
form.Show();
form.Location = new Point(0, 0);
foreach(Form forms in activeMdiForms)
{
MessageBox.Show(forms.ToString());
}
return;
}
public void clearMdi()
{
foreach(Form form in activeMdiForms)
{
form.Dispose();
}
activeMdiForms.Clear();
return;
}
It's working perfectly in parent class
private void Menu_Load(object sender, EventArgs e)
{
VersionChecker ver = new VersionChecker();
versionLbl.Text = "Depo Stok Programı Version " + earlySettings.version;
SetupMdi(new Login());
GCTimer.Start();
}
But I called SetupMdi method from child form its working but child form not showing but it using ram
public partial class Login : Form
{
public async void login()
{
earlySettings.usrName = obj.UserName;
MainMenu form = new MainMenu();
new Menu().SetupMdi(new MainMenuMdi());
this.Dispose();
}
}
I tried an ApiClass its not working like child class
But I called SetupMdi method from child form its working but child
form not showing but it using ram
Assuming your code is in an actual MdiChild, then you can cast the MdiParent property to your parent type (I believe, Menu?), and then call the SetupMdi() method:
// ... assuming we are in an MdiChild ...
((Menu)this.MdiParent).SetupMdi(new MainMenuMdi());
I have two forms (for example Form1 and Form2) in my project and I run only one form depending on a value which is read from a .txt file in Program.cs.
The two forms have the same controls and I am wondering if I can call the controls in a workerThread (inside the class Fsm.cs) using the same parent definition for both the forms.
For example: the control TextBox1 is defined in Form1 and Form2, but I want call in the workerThread DoWork() only the parent control associated to the running form:
parent.TextBox1.Visible = true;
Where parent can be Form1 or Form2.
How can I use the same parent definition mutually in Fsm.cs?
I don't want to define two different thread functions for the two forms.
Here there is the code:
public partial class Form1 : Form
{
public Form1()
{
form2 = new Form2();
InitializeComponent();
workerObject = new Fsm(this, form2);
Thread workerThread = new Thread(workerObject.DoWork);
}
}
public partial class Form2 : Form
{
public Form2()
{
form1 = new Form1();
InitializeComponent();
workerObject = new Fsm(form1, this);
Thread workerThread = new Thread(workerObject.DoWork);
}
}
public class Fsm
{
public Form1 parent;
public Form2 parent1;
public Fsm(Form1 p, Form2 p1)
{
parent = p;
parent1 = p1;
}
public void DoWork()
{
parent.TextBox1.Visible = true;
}
}
Thank you.
Form1 objForm1 = new Form1 ();
objForm1 .MdiParent = this;
objForm1 .Show();
This is my code to open MDI form . IF I open this page again, it appears again and again and so many windows opens. Can anybody help?
If you want to create a new form only if it is not already opened you can do this:
ShowFormIfNotOpen(this,typeof(Form1));
public static void ShowFormIfNotOpen(Form mainform,Type type)
{
foreach (Form item in mainform.MdiChildren)
if (item.GetType() == type)
{
item.Activate();
return;
}
Form form = Activator.CreateInstance(type) as Form;
form.MdiParent = mainform;
form.Show();
}
Update
1)Add a public static bool field/property in your form (IsAlreadyShown)
public static bool IsAlreadyShown { get; set; }
2)Set it to true in the constructor of the form
public Form1()
{
InitializeComponent();
IsAlreadyShown = true;
}
3)Call ShowForm1(this);
public static void ShowForm1(Form parentForm)
{
if(Form1.IsAlreadyShown ==true)
return;
Form1 objForm1 = new Form1 ();
objForm1 .MdiParent = parentForm;
objForm1 .Show();
}
What i have understand so far from your question, you want form1 to be MDI Container. if you want this,then simply set the property ISMdiContainer to be true.Now if you want this form Form1 to be set as parent of any form you can use your code
FormAny objFormAny = new FormAny ();
objFormAny .MdiParent = objForm1; // reference of MDI Container
objFormAny .Show();
How to force fully focus child form when we click on client area of it's parent form? [Like MessageBox or Error message have such focus so we must click on Ok button of messagebox.]
This is my code:
form = new Form2();
form.MdiParent = this;
form.ShowDialog(ParentForm);
But it gives me the error:
Form that is not a top-level form cannot be displayed as a modal
dialog box. Remove the form from any parent form before calling
showDialog.
Original answer - for non-MDIChild
Make the child form modal using the ShowDialog method.
ChildForm.ShowDialog (ParentForm);
To keep an MDI Child form at the top:
Handle the MDIChildActivate event on the parent form, and in that, set the child form that you want to stay visible to be active:. In this example, Form2 is the modal form, and Form3 is another form for testing.
private Form2 frm2;
private Form3 frm3;
private void Form1_Load(object sender, EventArgs e)
{
frm2=new Form2();
frm2.MdiParent=this ;
frm2.Show();
frm3= new Form3() ;
frm3.MdiParent=this;
frm3.Show();
}
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
frm2.Activate();
}
I think you want to have some kind of MessageBox in the MDI Parent Form with TopLevel = false. To make the effect look like the real effect a window shown by ShowDialog() has, I think this is the only solution which requires you to create a custom Form for your MDI child form:
public class ChildForm : Form
{
[DllImport("user32")]
private static extern int FlashWindowEx(FLASHWINFO flashInfo);
struct FLASHWINFO
{
public int cbSize;
public IntPtr hwnd;
public uint flags;
public int count;
public int timeOut;
}
protected override void WndProc(ref Message m)
{
if (ParentForm != null)
{
ChildForm dialog = ((Form1)ParentForm).Dialog;
if (dialog != this && dialog!=null)
{
if (m.Msg == 0x84) //WM_NCHITTEST
{
if (MouseButtons == MouseButtons.Left)
{
Flash(dialog.Handle);
}
return;
}
}
}
base.WndProc(ref m);
}
private void Flash(IntPtr handle)
{
FLASHWINFO flashInfo = new FLASHWINFO()
{
hwnd = handle,
count = 9,
cbSize = Marshal.SizeOf(typeof(FLASHWINFO)),
flags = 3,
timeOut = 50
};
FlashWindowEx(flashInfo);
}
public void Flash()
{
Flash(Handle);
}
//This is to disable Tab when the Dialog is shown
protected override bool ProcessTabKey(bool forward)
{
if(((Form1)ParentForm).Dialog == this) return true;
return base.ProcessTabKey(forward);
}
}
//Here is your MDI parent form
public class Form1 : Form {
public Form1(){
InitializeComponent();
IsMdiContainer = true;
f1 = new ChildForm();
f2 = new ChildForm();
f1.MdiParent = this;
f2.MdiParent = this;
//Get MDI Client
foreach(Control c in Controls){
if(c is MdiClient){
client = c;
break;
}
}
client.Click += ClientClicked;
}
ChildForm f1, f2;
MdiClient client;
public ChildForm Dialog {get;set;}
private void ClientClicked(object sender, EventArgs e){
if(Dialog != null) Dialog.Flash();
else {//Show dialog, you can show dialog in another event handler, this is for demonstrative purpose
Dialog = new ChildForm(){MdiParent = this, Visible = true};
ActivateMdiChild(Dialog);
//Suppose clicking on the Dialog client area will close the dialog
Dialog.Click += (s,ev) => {
Dialog.Close();
};
Dialog.FormClosed += (s,ev) => {
Dialog = null;
};
}
}
}