Click here to Skip to main content
15,896,915 members
Home / Discussions / C#
   

C#

 
GeneralRe: Threading for multicore applications Pin
Steven Solberg21-Aug-10 7:25
Steven Solberg21-Aug-10 7:25 
GeneralRe: Threading for multicore applications Pin
harold aptroot21-Aug-10 7:27
harold aptroot21-Aug-10 7:27 
GeneralRe: Threading for multicore applications Pin
Steven Solberg21-Aug-10 7:47
Steven Solberg21-Aug-10 7:47 
GeneralRe: Threading for multicore applications Pin
harold aptroot21-Aug-10 7:57
harold aptroot21-Aug-10 7:57 
GeneralRe: Threading for multicore applications Pin
Steven Solberg21-Aug-10 8:03
Steven Solberg21-Aug-10 8:03 
GeneralRe: Threading for multicore applications Pin
harold aptroot21-Aug-10 8:28
harold aptroot21-Aug-10 8:28 
AnswerRe: Threading for multicore applications Pin
Luc Pattyn21-Aug-10 10:02
sitebuilderLuc Pattyn21-Aug-10 10:02 
GeneralRe: Threading for multicore applications Pin
Steven Solberg22-Aug-10 8:17
Steven Solberg22-Aug-10 8:17 
GeneralRe: Threading for multicore applications Pin
Luc Pattyn22-Aug-10 8:47
sitebuilderLuc Pattyn22-Aug-10 8:47 
GeneralRe: Threading for multicore applications Pin
Steven Solberg22-Aug-10 8:57
Steven Solberg22-Aug-10 8:57 
QuestionC# Getting Responce From Form1 Pin
C.CoderCreator21-Aug-10 2:24
C.CoderCreator21-Aug-10 2:24 
AnswerRe: C# Getting Responce From Form1 Pin
Abhinav S21-Aug-10 2:51
Abhinav S21-Aug-10 2:51 
GeneralRe: C# Getting Responce From Form1 Pin
C.CoderCreator21-Aug-10 2:55
C.CoderCreator21-Aug-10 2:55 
AnswerRe: C# Getting Responce From Form1 Pin
venomation21-Aug-10 3:16
venomation21-Aug-10 3:16 
GeneralRe: C# Getting Responce From Form1 Pin
C.CoderCreator21-Aug-10 3:24
C.CoderCreator21-Aug-10 3:24 
GeneralRe: C# Getting Responce From Form1 Pin
venomation21-Aug-10 3:33
venomation21-Aug-10 3:33 
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 

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.