open form in panel from another form - c#

I have a main form. The main form work properly when I click any button. In the main form, I have a button open a Dialog Form. In that Dialog Form I select a button and get sent to another target form. Currently the target form opens as a separate form.
I want to open the target form in a panel in main form.
This is code from the Dialog Form.
private void BtnTemp1_Click(object sender, EventArgs e)
{
FrmEditTemplete FET = new FrmEditTemplete();
FrmMain FM = ActiveForm as FrmMain;
FET.FormBorderStyle = FormBorderStyle.None;
FET.TopLevel = false;
FET.Visible = true;
FM.PnlAreaForms.Controls.Add(FET);
FET.Dock = DockStyle.Fill;
FET.panel1.BackgroundImage = Properties.Resources.Squares_3_5_By_3_5;
FET.BringToFront();
FET.Show();
}
The error message I'm getting is
Object reference not set to an instance of an object

Related

How to add a control to another form and take it back

I use a WebBrowser control in my main form. After loading a webpage, I want to allow the user to manipulate the page in another form. So, I add the webBrowser to that form.
FormBrowser form2 = new FormBrowser();
form2.Browser = webBrowser1;
form2.ShowDialog();
When the form2 is shown, the webBrowser1 disappears from main form and is added to the form2, because I add it in the form_load event:
private void FormBrowser_Load(object sender, EventArgs e)
{
if (browser != null)
{
this.Controls.Add(browser);
}
}
Now I want to add it to the main form and place it in its original location, I tried the following code:
.....
browser.Browser = webBrowser1;
browser.ShowDialog();
Controls.Add(webBrowser1);
webBrowser1.BringToFront();
But it doesn't place it in its original location. Is there any standard way to do such a task? Why does it disappears from the main form? Can I retain it?
Set the x and y values of the Location property of the new control.

Redisplay the splash screen on a button click in 2nd form

//Splash form:
//------------
frmMain main = new frmMain();
this.Hide();
main.Show();
//MainForm:
//---------
var formToShow = (frmSplash)System.Windows.Forms.Application.OpenForms.Cast<Form>().FirstOrDefault(c => c is frmSplash);
formToShow.Show();
//formToShow.ShowDialog() execute the load event which I dont want.
formToShow.RunTestMetod()
//How I can get the splash hidden form without loading its Load Event and running its //TestMethod(). I want the mainform under it not accessible same like a message box dialog.
I dont want formtoShow.topmost = true as that keep it on the top but the 2nd form is still clickable. Any help would be greatly appreciated.
You can use myNewForm.Show(this); for the Window you want to be shown as a dialog. This will show myNewForm as a child of the current form, but lets you interact with the current form.
You could just disable MainForm?
// ...
var formToShow = (frmSplash)System.Windows.Forms.Application.OpenForms.Cast<Form>().FirstOrDefault(c => c is frmSplash);
this.Enabled = false;
formToShow.FormClosed += frm_FormClosed;
formToShow.Show(this);
formToShow.RunTestMetod()
void frm_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
}

Keep form on top and open and close other forms

I'm new to C# and could use some help.
What I have so far is a set of 8 windows forms I have created in C#, these forms have basic things like text boxes, labels, radio buttons, etc. Now that I have completed making all of these forms I want to have one additional form (called the Selector form) I can use to select one of the other 8 forms. At any given time, I want the Selector form to be on top of other windows and it will have 8 radio buttons (or regular buttons, doesn't matter). When one of the buttons is clicked, the current form (not the Selector form) should disappear and a new form should appear. The name of the button will be the name of the new form that appears.
I have seen a few examples and here is the code I have so far:
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
form1.Closed += (sender1, args) => this.Close();
form1.Show();
}
void Button2Click(object sender, EventArgs e)
{
// this.Hide();
var form2 = new CCARAdmin();
form2.Closed += (sender1, args) => this.Close();
form2.Show();
//Application.Run(new CCARAdmin());
}
Problem I am having is I don't want to hide the Selector form, which this does, and I don't know how to identify the other form that is open to close it and then open a different form.
From starting the program the logic would be like this:
Show Selector form
When a button is clicked on the Selector form, keep the Selector form on top and show the other form with the name of the button.
When a different button is clicked on the Selector form, close the previous form that was open (not the Selector form) and open the new form corresponding to the name of the button. Keep the Selector form on top.
When the Selector form is close, application stops.
Problem I am having is I don't want to hide the Selector form, which
this does, and I don't know how to identify the other form that is
open to close it and then open a different form.
Set Selector form TopMost to True to make it always on top. Or
you can use BringToFront after opening a new form
to know other forms that are open check this answer. Or you can define each From as a field in the Selector form, and check that.
selectorForm.TopMost = true ( this will help to keep the selector form always on top).
Create a form variable in your selector form to keep the reference of your currently opened form.
Sample code for 1 button click :
Form frm = null;
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
if (frm == null)
{
frm = form1;
}
else
{
frm.Close();
}
form1.Show();
this.TopMost = true;
frm = form1;
}
I resolved this by setting TopMost to true and then using the following code under each one of the buttons:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "FormSelector")
Application.OpenForms[i].Close();
}
var form = new TRAC();
if (radioButton9.Checked == true)
{
form.Show();
}

Panel Could not set Visible True in MDI

I am using C#. Net Windows application.
I have one MDI parent form and many child forms. I put panel in MDI parent form and drag several button inside panel.
When I click the button they open another child form and set visible false to panel
like this (sample code):
private void Button_Click(object sender, EventArgs e)
{
panel1.Visible = false;
ChildForm Form2 = new ChildForm();
Form2.WindowState = FormWindowState.Maximized;
Form2.Show();
}
Now they perfectly working. What the problem is, when I close the child form the panel could not visible in MDI parent form. Its always panel visible false. I set to true., see my code.
private void ChildForm _FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose();
MDI md = new MDI();
md.panel1.Visible = true;
}
am also using BringToFront, SendToBack. No use. Please assist.
The problems are:
you create a new instance of MDI form in your child form with MDI md = new MDI();
You should instead retrieve the instance of the opened MDI and set md.panel1.Visible =
true; on this instance. You can use the MdiParent property.
MDI md = (MDI)this.MdiParent;
md.panel1.Visible = true;
and you call This.Dispose before your the code that set panel visible. I am not sure that the code which is after This.Dispose will be executed...
hi friends i Solved this and i got worked now...
here the Solution..
> private void ChildForm_FormClosed(object sender,FormClosedEventArgs e)
> {
> MDI md = (MDI)this.MdiParent;
> md.panel1.Visible = true;
> }

how to show a child form in MDI container without the apperance of the controls in the Container Form in the Child Form?

in my project in the container form i use buttons to open the child forms , not Strip Menu but the buttons in the container always appears on the child form
how to privet the buttons or any other controls form being above the child form
i am using Visual Studio 2008 professional edition C# programming language
as in this Image the button suppose to be in form1 and not to be seen in Form2 (Child)
and also the other controls in the Container
sir i have best solution for
create new empty form and than set following property of this form
set in Form_load event
private void bg_Load(object sender, EventArgs e)
{
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
than after
write following code in mdi form load event
private void Main_Load(object sender, EventArgs e)
{
bg bg = new bg(); // create object of empty form my empty form name is "bg"
bg.MdiParent = this;
bg.Show();
}
what ever you want in background add into empty form....]
Enjoy
You should use ToolStrip or MenuStrip to call your child form. In your case, I assume you just drag and drop a Button into your Form1. That's why the button is floating.
But if you are persistent and still don't want to use ToolStrip and MenuStrip, you can hide the button after showing the childform.. Ex:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.MdiParent = form1;
f2.Show();
button1.Visible = false; // This will cause your button to be hidden.
}

Categories

Resources