how to Access buttons from another form in c# - c#

I have two forms. I have a button in mainForm. When i clicked button1 the main form will show and the button1 will be Enabled false. Now i want to Enable it to True when my ChildForm gets Close. PLease help me .
My code For Enable= False is
CstmersFrm cstFm = new CstmersFrm();
cstFm.MdiParent = this;
cstFm.Show();
cstFm.BringToFront();
btnCstmr.Enabled = false;
Iam trying to Enable= True is In cstmersFrm _Closed Event
mFrm = (mainForm)this.MdiParent;
mFrm.btnCstmr.Enabled = true;

You need to register to the child form Closed event on the main form:
Form child = new Form();
child.MdiParent=this;
child.Show();
child.FormClosed+=child_FormClosed;
and then set the button to Enabled:
void child_FormClosed(object sender, FormClosedEventArgs e)
{
btnCstmr.Enabled = true;
}

Related

Enable/Disable another form WITHOUT INSTANTIATING NEW ONE

Here's my question.. From form 1, i want when the user clicked on update items, Form 2 will open but form 1 will still remain open. At this point, I want the Form 1 to be enabled false and I've done it. But I want when the form 2 closes, the form 1 enable should be true again. I cannot do this... Here's my code:
In Form 1:
private void btnEditItem_Click(object sender, EventArgs e){
Form3 form3 = new Form3();
form3.Show();
this.Enabled = false;
}
In Form 2(which is for update items, after updating):
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 form1 = new Form1();
form1Here.Enabled = true;
}
In this situation, it will open another Form1. The question is, how am i suppose to "ENABLE BACK AGAIN THE FORM 1 WITHOUT OPENING IT?" HELP ME PLEASE PROVIDE SOME SAMPLE IF POSSIBLE. THANKS
Attach a handler to your form.
Form3 form3 = new Form3();
form3.FormClosed += new FormClosedEventHandler(frm3_FormClosed);
form3.Show();
private void frm3_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
}
Or you can use ShowDialog
Opens a window and returns only when the newly opened window is closed.
From your description it seems you want a modal Dialog Box.
From MSDN Modal and Modeless Dialog Boxes:
A modal dialog box must be closed (hidden or unloaded) before you can
continue working with the rest of the application
This means that if Form 1 opens Form 2 as a modal dialog box, form 1 will remain visible, but it is disabled, you can't do anything with it until you've closed form 2. In your words: "form 1 is disabled and when form 2 closes form 1 is enabled again".
The code:
private void btnEditItem_Click(object sender, EventArgs e)
{
using (Form3 form3 = new Form3()}
{
var dlgResult = form3.ShowDialog(); // show form 3 as a modal dialog box
// halt this procedure until form3 is closed
// handle the result of form3:
if (dlgResult == DialogResult.OK)
{
var x = form3.SomePropery;
ProcessDialogOutput(x);
}
}
}
The using statement is not necessary, but it is neat programming, because Form3 implements IDisposable

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;
}

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 close a tab when a form embedded in it closed?

I have my form embedded on top of the tabpage. When I closed the form which is on top of the tabpage, how do I also make the tabpage close as well?
The code when I put my form on the tabpage is more less like this:
client c = new client(car_name, owner); //here client is another winform class
c.TopLevel = false;
c.Visible = true;
c.BackColor = Color.Ivory;
c.FormBorderStyle = FormBorderStyle.None;
c.Dock = DockStyle.Fill;
tabControl1.TabPages[tab_index].Controls.Add(c);
Use the FormClosing event :
private void ClientForm_FormClosing(object sender, FormClosedEventArgs e)
{
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}

Windows Forms C#

I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.

Categories

Resources