How to fix "Change MdiParent in MdiChild" in C# - c#

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());

Related

When use this code ```Form1 form = new Form1();``` something goes wrong

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
}

How load a form into another form's panel

I have a form called frmTest1 with a splitcontainer with two panels. Panel 2 should load many forms one at a time. It works fine for the first form, but the second form cant then load a third form into panel 2 of frmTest1.
Here is stripped frmTest 1 code:
namespace Project1
{
public partial class frmMain3 : Form
{
public frmMain3()
{
InitializeComponent();
}
private void btnShowTest1_Click(object sender, EventArgs e)
{
showScreen(new frmTest1());
}
public void showScreen(Control ctl)
{
while (splitContainer1.Panel2.Controls.Count > 0)
splitContainer1.Panel2.Controls[0].Dispose();
if (ctl is Form)
{
var frm = ctl as Form;
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Visible = true;
}
ctl.Dock = DockStyle.Fill;
splitContainer1.Panel2.Controls.Add(ctl);
}
}
}
The second form's code is below:
namespace Project1
{
public partial class frmTest1 : Form
{
public frmTest1()
{
InitializeComponent();
}
private void btnShowTest4_Click(object sender, EventArgs e)
{
frmMain3 main = new frmMain3();
main.showScreen(new frmTest4()); //Nothing shows
}
}
}
From the research I have done it seems the solution is to use a usercontrol but having never used it before, I am struggling. Can someone please show me how to resolve this?
Please try to use a UserControl. Just change the base class of frmMain3 to "UserControl" and delete the whole "if (ctl is Form)"-part from showScreen().

Accessing methods on open form from a parent form c#

I want to access a method in a child form from parent form.I have used the following code to access the controls.
Form form = (Form)Application.OpenForms["frmname"];
if (tableform != null)
{
GroupBox grp = (GroupBox)tableform.Controls["grpbxname"];
Panel table = (Panel)grp.Controls["panelname"];
}
Using the following code I am able to access controls in the child form from the parent form.
Same way I want to access the function/method in the child form.
For ex: from.newmethod();
Is there any possibility to achieve this,without creating new instance of form.It windows application using c#.net
Thanks.
declaring a method public is not a good practice. you can create delegate or event instead. you can create public delegate for that method and execute that delegate from the outside the class Or you can create Event that you can handle from the outside of the class.
public partial class Form1 : Form
{
public delegate void dMyFunction(string param);
public dMyFunction delMyFunction;
public Form1()
{
InitializeComponent();
delMyFunction = new dMyFunction(this.MyFunction);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void MyFunction(string param)
{
this.Text = param;
}
}
Now, you can call this delegate from the outside the class
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 frm = (Form1)Application.OpenForms["Form1"];
frm.delMyFunction.Invoke("Hello");
//On Form load this method will be invoked and Form1 title will be changed.
}
}

Update textbox contained within a different form

I have 2 forms. The second form is opened from a method in the first form and I wish to be able to update the textbox that exists within that second form.
Basically I have the following code:
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI();
sendOutGUI.Show();
sendOutGUI.sendConsoleTextBox.Text = "Test";
}
When I press the button the second form (SendConsoleGUI form) opens but "Test" is never added to its textbox.
Am I doing something wrong here?
You need to use invoke method.
sendOutGUI.Invoke((MethodInvoker) delegate { sendOutGUI.sendConsoleTextBox.Text = "Test"; });
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI("Test");
sendOutGUI.Show();
}
}
public partial class ChildForm : Form
{
public ChildForm(string str)
{
InitializeComponent();
sendConsoleTextBox.Text = str;
}
}
This will work for you if you only wish to update it when the ChildForm is initially created.

C# Windows Forms - link instances of same Form

I'd like to create, say 32 Windows Forms based upon a single template Form, and those instances should be linked to each other. That is, every Form has a button for calling the next instance and so on. I am able to create as many forms as I like but how would I link those instances together ?
This is what I use to create several child forms:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm child = new ChildForm();
child.Show();
}
}
Sequence of events would be like:
User starts application, main form is displayed (only has "Open Child" button)
User pushes "Open child" button, first instance of child form opens
first child form (caption "Child Form 1") has button "Open Child Form 2"
if user pushes "Open Child Form 2" child form 1 is hidden and child form 2 is displayed
if the last child form is reached wrap-around to child form 1
Any ideas are welcome !
regards
Chris
You can create a static collection of the form, in the constructor add the form instance to the list (and remove it during dispose). To figure out the next form, you can find the index of the current form and grab the next form in the list based on that. Create a form with two buttons and modify it as below to test it out.
public partial class Form1 : Form
{
static List<Form1> formList = new List<Form1>();
public Form1()
{
InitializeComponent();
formList.Add(this);
}
private void button1_Click(object sender, EventArgs e)
{
int idx = formList.IndexOf(this);
int nextIdx = (idx == formList.Count()-1 ? 0: idx+1 );
Form1 nextForm = formList[nextIdx];
nextForm.changeTextAndFocus("next form: " + nextIdx);
}
// moves to the next form and changes the text
public void changeTextAndFocus(string txt)
{
this.Focus();
this.Text = txt;
}
//Creates 5 forms
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
Form1 newForm = new Form1();
newForm.Show();
}
}
}
I don't really know what you are planning to do :). If you want to count several forms, you can add a property Number to the form. And searching upwards from there.
Mainform;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm first = new ChildForm();
first.Number = 1;
first.Show();
}
}
ChildForm
public partial class ChildForm : Form
{
public ChildForm()
{
// createButton here
}
private void button_Click(object sender, EventArgs e)
{
ChildForm _childForm = new ChildForm();
_childForm.Owner = this;
_childForm.Number = this.Number + 1;
this.Hide();
_childForm.Show();
}
public void FirstChildForm()
{
if (this.Number != 1) //maybe not that static
{
(this.Owner as ChildForm).FirstChildForm();
this.Close(); // or hide or whatever
}
}
public int Number
{ get; set; }
}
Not tested code, hope this helps a bit :).

Categories

Resources