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().
Related
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 currently have one form and a few user control, i have a panel set from the Form1 - mainpanel, so that whenever i click a button, one of the user control will be showing on the mainPanel.
my problem is that one of the control(InfoSetting) has some other new buttons, and when i click it I would like the mainPanel to show the new user control. However, right now all other buttons from Form1 is working fine, but when I click on the InfoSetting button, nothing seems to show up.
here is my code from Form1
public partial class Form1 : MetroFramework.Forms.MetroForm
{
InfoSettings infoSetting;
CashSales cashSales;
PriceSetting priceSetting;
public Form1()
{
InitializeComponent();
infoSetting = new InfoSettings();
cashSales = new CashSales();
priceSetting = new PriceSetting();
}
protected void ButtonClicked(object sender, EventArgs e)
{
buttonHandler(sender);
}
public void buttonHandler(object sender)
{
Button button = sender as Button;
mainPanel.Controls.Clear();
if (button != null)
{
switch (button.Name)
{
case "HomeBtn":
infoSetting.Dock = DockStyle.Fill;
mainPanel.Controls.Add(infoSetting);
break;
case "cashSalesBtn":
cashSales.Dock = DockStyle.Fill;
mainPanel.Controls.Add(cashSales);
break;
case "settingBtn":
infoSetting.Dock = DockStyle.Fill;
mainPanel.Controls.Add(infoSetting);
break;
case "priceSettingBtn":
cashSales.Dock = DockStyle.Fill;
mainPanel.Controls.Add(cashSales);
break;
default:
mainPanel.Controls.Clear();
break;
}
}
}
and here is the code from the InfoSetting user control
public partial class InfoSettings : UserControl
{
public InfoSettings()
{
InitializeComponent();
}
private void priceSettingBtn_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.buttonHandler(sender);
}
}
as stuartd said you can add a call to your parent Form:
public partial class InfoSettings : UserControl
{
public InfoSettings()
{
InitializeComponent();
}
private void settingBtn_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1) this.Parent;
form1.buttonHandler(sender);
}
}
It would probably be better to have a common class that you can access in both form and user control and have the code you want to run in a method there. Something like:
public class FormHelper
{
public void CodeIWantToRunIn2Places()
{
// Implementation Here
}
}
You can implement the above as a singleton so you have access in both places. Then in your form classes you could do:
FormHelper.Instance.CodeIWantToRunIn2Places();
In your event handler as well as in your user control.
You can make the Form 1 as "static" and also the method buttonHandler(object sender) as "static" as well.
Then inside the priceSettingBtn_Click, call the below function directly
Form1.buttonHandler(sender);
I would have a global object with a pointer to all the forms in the application.
public Global
{
public Form1 MainForm;
public Dialog1 D1;
...
}
public static Global = new Global();
In as each form is created
Global.MainForm = form1;
then you can go
private void priceSettingBtn_Click(object sender, EventArgs e)
{
Global.MainForm.buttonHandler(sender);
}
As other have said , its probably better to not call the click handler, its better to have this code and the real click handler call a business logic level function
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.
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 :).
I had windows form I did my code to display panel where condition but this error apeared (object refrencer.....) on panel1.Show();
public partial class Checker : Form
{
public Checker()
{
RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
if (adobe == null)
{
panel1.Show();
}
}
private void OK_Click(object sender, EventArgs e)
{
}
}
Seems like you don't have an instance of your panel1.
Where is it declared? Is it a panel on a form somewhere? If so, where is your form declared?
If the code above is your form, you might want to call
InitializeComponent();
before you continue your constructor.