Click here to Skip to main content
15,887,485 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Getting Responce From Form1 Pin
DaveyM6921-Aug-10 6:14
professionalDaveyM6921-Aug-10 6:14 
GeneralRe: C# Getting Responce From Form1 Pin
Pete O'Hanlon21-Aug-10 7:59
mvePete O'Hanlon21-Aug-10 7:59 
GeneralRe: C# Getting Responce From Form1 Pin
C.CoderCreator21-Aug-10 14:00
C.CoderCreator21-Aug-10 14:00 
GeneralRe: C# Getting Responce From Form1 Pin
Pete O'Hanlon21-Aug-10 21:13
mvePete O'Hanlon21-Aug-10 21:13 
GeneralRe: C# Getting Responce From Form1 Pin
C.CoderCreator21-Aug-10 21:51
C.CoderCreator21-Aug-10 21:51 
GeneralRe: C# Getting Responce From Form1 Pin
Pete O'Hanlon22-Aug-10 3:17
mvePete O'Hanlon22-Aug-10 3:17 
GeneralRe: C# Getting Responce From Form1 Pin
DaveyM6922-Aug-10 4:32
professionalDaveyM6922-Aug-10 4:32 
GeneralRe: C# Getting Responce From Form1 Pin
DaveyM6922-Aug-10 5:14
professionalDaveyM6922-Aug-10 5:14 
OK, here are three demo projects that show you ways this can be done.
First is the correct way.
The second should NOT be used but is better than what you are attempting!
The third shows you how do do it your way - this is terrible so DO NOT use it!

First - Correct
C#
// Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class Form1 : Form
    {
        // This would normally reside in the designer
        private Label label;

        public Form1()
        {
            InitializeComponent();
            CreateLabel();
            Form2 form2 = new Form2();
            form2.RequestDisable += new EventHandler(form2_RequestDisable);
            form2.Show();
        }

        // This stuff would normally reside inside InitializeComponent in the designer
        private void CreateLabel()
        {
            label = new Label();
            label.Location = new Point(12, 12);
            label.Text = "Disable Me";
            Controls.Add(label);
        }
        private void form2_RequestDisable(object sender, EventArgs e)
        {
            label.Enabled = false;
        }
    }
}

C#
// Form2.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class Form2 : Form
    {
        public event EventHandler RequestDisable;

        // This would normally reside in the designer
        private Button buttonDisable;

        public Form2()
        {
            InitializeComponent();
            CreateButtonDisable();
        }

        private void buttonDisable_Click(object sender, EventArgs e)
        {
            OnRequestDisable(EventArgs.Empty);
        }
        // This stuff would normally reside inside InitializeComponent in the designer
        private void CreateButtonDisable()
        {
            buttonDisable = new Button();
            buttonDisable.Location = new Point(12, 12);
            buttonDisable.Text = "&Disable";
            buttonDisable.Click += new EventHandler(buttonDisable_Click);
            Controls.Add(buttonDisable);
            AcceptButton = buttonDisable;
        }
        protected virtual void OnRequestDisable(EventArgs e)
        {
            EventHandler eh = RequestDisable;
            if (eh != null)
                eh(this, e);
        }
    }
}

Second - bad!
C#
// Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class Form1 : Form
    {
        // This would normally reside in the designer
        private Label label;

        public Form1()
        {
            InitializeComponent();
            CreateLabel();
            Form2 form2 = new Form2(label);
            form2.Show();
        }

        // This stuff would normally reside inside InitializeComponent in the designer
        private void CreateLabel()
        {
            label = new Label();
            label.Location = new Point(12, 12);
            label.Text = "Disable Me";
            Controls.Add(label);
        }
    }
}

C#
// Form2.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class Form2 : Form
    {
        // This would normally reside in the designer
        private Button buttonDisable;

        private Control controlToDisable;

        public Form2(Control controlToDisable)
        {
            InitializeComponent();
            CreateButtonDisable();
            this.controlToDisable = controlToDisable;
        }

        private void buttonDisable_Click(object sender, EventArgs e)
        {
            controlToDisable.Enabled = false;
        }
        // This stuff would normally reside inside InitializeComponent in the designer
        private void CreateButtonDisable()
        {
            buttonDisable = new Button();
            buttonDisable.Location = new Point(12, 12);
            buttonDisable.Text = "&Disable";
            buttonDisable.Click += new EventHandler(buttonDisable_Click);
            Controls.Add(buttonDisable);
            AcceptButton = buttonDisable;
        }
    }
}

Third - Worst!
C#
// Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class Form1 : Form
    {
        // This would normally reside in the designer
        private static Label label;

        public Form1()
        {
            InitializeComponent();
            CreateLabel();
            Form2 form2 = new Form2();
            form2.Show();
        }

        public static Label Label
        {
            get { return label; }
        }

        // This stuff would normally reside inside InitializeComponent in the designer
        private void CreateLabel()
        {
            label = new Label();
            label.Location = new Point(12, 12);
            label.Text = "Disable Me";
            Controls.Add(label);
        }
    }
}

C#
// Form2.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class Form2 : Form
    {
        // This would normally reside in the designer
        private Button buttonDisable;

        public Form2()
        {
            InitializeComponent();
            CreateButtonDisable();
        }

        private void buttonDisable_Click(object sender, EventArgs e)
        {
            Form1.Label.Enabled = false;
        }
        // This stuff would normally reside inside InitializeComponent in the designer
        private void CreateButtonDisable()
        {
            buttonDisable = new Button();
            buttonDisable.Location = new Point(12, 12);
            buttonDisable.Text = "&Disable";
            buttonDisable.Click += new EventHandler(buttonDisable_Click);
            Controls.Add(buttonDisable);
            AcceptButton = buttonDisable;
        }
    }
}

Dave

If this helped, please vote & accept answer!


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

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

GeneralRe: C# Getting Responce From Form1 Pin
Pete O'Hanlon22-Aug-10 5:28
mvePete O'Hanlon22-Aug-10 5:28 
GeneralRe: C# Getting Responce From Form1 Pin
DaveyM6922-Aug-10 5:45
professionalDaveyM6922-Aug-10 5:45 
QuestionWindows service cannot able to start Pin
sekannak21-Aug-10 1:18
sekannak21-Aug-10 1:18 
AnswerRe: Windows service cannot able to start Pin
OriginalGriff21-Aug-10 2:11
mveOriginalGriff21-Aug-10 2:11 
AnswerRe: Windows service cannot able to start Pin
PIEBALDconsult21-Aug-10 4:24
mvePIEBALDconsult21-Aug-10 4:24 
QuestionItems in ComboBox Pin
igalep13221-Aug-10 0:56
igalep13221-Aug-10 0:56 
AnswerRe: Items in ComboBox Pin
Dave Kerr21-Aug-10 1:17
mentorDave Kerr21-Aug-10 1:17 
GeneralRe: Items in ComboBox Pin
igalep13221-Aug-10 1:22
igalep13221-Aug-10 1:22 
QuestionPrintPreview WTF? Pin
Dr.Walt Fair, PE20-Aug-10 16:02
professionalDr.Walt Fair, PE20-Aug-10 16:02 
QuestionEvent Handling in Windows Services [modified] Pin
See_Sharp20-Aug-10 7:27
See_Sharp20-Aug-10 7:27 
AnswerRe: Event Handling in Windows Services PinPopular
Eddy Vluggen20-Aug-10 7:48
professionalEddy Vluggen20-Aug-10 7:48 
GeneralRe: Event Handling in Windows Services Pin
See_Sharp20-Aug-10 8:14
See_Sharp20-Aug-10 8:14 
GeneralRe: Event Handling in Windows Services Pin
Eddy Vluggen20-Aug-10 8:28
professionalEddy Vluggen20-Aug-10 8:28 
Questionwebpage popping out from iframe [modified] Pin
Hiren solanki20-Aug-10 3:36
Hiren solanki20-Aug-10 3:36 
AnswerRe: webpage popping out from iframe Pin
Łukasz Nowakowski20-Aug-10 3:47
Łukasz Nowakowski20-Aug-10 3:47 
AnswerRe: webpage popping out from iframe Pin
Ennis Ray Lynch, Jr.20-Aug-10 3:49
Ennis Ray Lynch, Jr.20-Aug-10 3:49 
AnswerRe: webpage popping out from iframe Pin
R. Giskard Reventlov20-Aug-10 3:50
R. Giskard Reventlov20-Aug-10 3:50 

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.