Click here to Skip to main content
15,904,653 members
Home / Discussions / C#
   

C#

 
AnswerRe: File being used by another process? I don't think so! Pin
Luc Pattyn1-May-11 9:21
sitebuilderLuc Pattyn1-May-11 9:21 
GeneralRe: File being used by another process? I don't think so! Pin
SimpleData1-May-11 9:25
SimpleData1-May-11 9:25 
AnswerRe: File being used by another process? I don't think so! Pin
Luc Pattyn1-May-11 9:52
sitebuilderLuc Pattyn1-May-11 9:52 
GeneralRe: File being used by another process? I don't think so! Pin
lukeer1-May-11 21:22
lukeer1-May-11 21:22 
AnswerRe: File being used by another process? I don't think so! Pin
I Believe In GOD1-May-11 9:12
I Believe In GOD1-May-11 9:12 
AnswerRe: File being used by another process? I don't think so! Pin
BobJanova3-May-11 1:33
BobJanova3-May-11 1:33 
Questionsaving values in forms of c# Pin
aeman1-May-11 2:03
aeman1-May-11 2:03 
AnswerRe: saving values in forms of c# Pin
DaveyM691-May-11 2:12
professionalDaveyM691-May-11 2:12 
GeneralRe: saving values in forms of c# Pin
aeman1-May-11 2:27
aeman1-May-11 2:27 
GeneralRe: saving values in forms of c# Pin
DaveyM691-May-11 4:34
professionalDaveyM691-May-11 4:34 
AnswerRe: saving values in forms of c# Pin
I Believe In GOD1-May-11 2:28
I Believe In GOD1-May-11 2:28 
GeneralRe: saving values in forms of c# Pin
aeman1-May-11 2:56
aeman1-May-11 2:56 
GeneralRe: saving values in forms of c# Pin
I Believe In GOD1-May-11 2:58
I Believe In GOD1-May-11 2:58 
GeneralRe: saving values in forms of c# Pin
Pete O'Hanlon2-May-11 6:27
mvePete O'Hanlon2-May-11 6:27 
GeneralRe: saving values in forms of c# Pin
DaveyM691-May-11 4:24
professionalDaveyM691-May-11 4:24 
The solution given works as you have seen BUT it is not generally recommended as now your Form2 is coupled to Form1, coupling is generally not a good idea.

A better solution is for Form1 to control everything, and Form2 to raise an event to inform Form1 that the button has been clicked:

C#
// Form1
public partial class Form1 : Form
{
    private Form2 form2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (form2 == null)
        {
            // create instance and subscribe to events
            form2 = new Form2();
            form2.ButtonClicked += new EventHandler(form2_ButtonClicked);
            form2.FormClosing += new FormClosingEventHandler(form2_FormClosing);
        }
        form2.Show();
        Hide();
        button2.BackColor = Color.Green;
    }

    private void form2_ButtonClicked(object sender, EventArgs e)
    {
        Show();
        form2.Hide();
    }
    private void form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        // unsubscribe and clear reference
        form2.ButtonClicked -= form2_ButtonClicked;
        form2.FormClosing -= form2_FormClosing;
        form2 = null;
        
        if (!Visible) // no forms visible so close to end application
            Close();
    }
}

C#
// Form2
public partial class Form2 : Form
{
    public event EventHandler ButtonClicked;

    public Form2()
    {
        InitializeComponent();
    }

    protected virtual void OnButtonClicked(EventArgs e)
    {
        EventHandler eh = ButtonClicked;
        if (eh != null)
            eh(this, e);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OnButtonClicked(EventArgs.Empty);
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: saving values in forms of c# Pin
Prasanta_Prince1-May-11 8:52
Prasanta_Prince1-May-11 8:52 
QuestionMatlab-to-C# Pin
harrykan30-Apr-11 23:39
harrykan30-Apr-11 23:39 
AnswerRe: Matlab-to-C# Pin
I Believe In GOD1-May-11 1:14
I Believe In GOD1-May-11 1:14 
AnswerRe: Matlab-to-C# Pin
Dave Kreskowiak1-May-11 3:36
mveDave Kreskowiak1-May-11 3:36 
QuestionHow to use MF_BYPOSITION parameter in InsertMenu Function Pin
Loithuxua30-Apr-11 16:37
Loithuxua30-Apr-11 16:37 
AnswerRe: How to use MF_BYPOSITION parameter in InsertMenu Function Pin
OriginalGriff30-Apr-11 20:29
mveOriginalGriff30-Apr-11 20:29 
GeneralRe: How to use MF_BYPOSITION parameter in InsertMenu Function Pin
David198730-Apr-11 21:00
David198730-Apr-11 21:00 
GeneralRe: How to use MF_BYPOSITION parameter in InsertMenu Function Pin
OriginalGriff30-Apr-11 21:04
mveOriginalGriff30-Apr-11 21:04 
GeneralRe: How to use MF_BYPOSITION parameter in InsertMenu Function Pin
Dave Kreskowiak1-May-11 3:38
mveDave Kreskowiak1-May-11 3:38 
AnswerRe: How to use MF_BYPOSITION parameter in InsertMenu Function Pin
I Believe In GOD1-May-11 1:42
I Believe In GOD1-May-11 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.