Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: win6x_registry_tweak Pin
Dave Kreskowiak23-Oct-17 9:48
mveDave Kreskowiak23-Oct-17 9:48 
GeneralRe: win6x_registry_tweak Pin
Pete O'Hanlon23-Oct-17 9:55
mvePete O'Hanlon23-Oct-17 9:55 
GeneralRe: win6x_registry_tweak Pin
Dave Kreskowiak24-Oct-17 2:56
mveDave Kreskowiak24-Oct-17 2:56 
GeneralRe: win6x_registry_tweak Pin
Pete O'Hanlon23-Oct-17 9:49
mvePete O'Hanlon23-Oct-17 9:49 
QuestionNewbie question - how to access this object Pin
Member 1347915022-Oct-17 13:10
Member 1347915022-Oct-17 13:10 
AnswerRe: Newbie question - how to access this object Pin
User 740747022-Oct-17 14:23
User 740747022-Oct-17 14:23 
AnswerRe: Newbie question - how to access this object Pin
OriginalGriff22-Oct-17 19:50
mveOriginalGriff22-Oct-17 19:50 
AnswerRe: Newbie question - how to access this object Pin
BillWoodruff23-Oct-17 4:48
professionalBillWoodruff23-Oct-17 4:48 
If I interpret your description literally, there;s a trivial solution that requires no exposure of your subclassed PictureBox to its Form context:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CPWorking
{
    public partial class PBX : PictureBox
    {
        public PBX()
        {
            InitializeComponent();

            this.BackColor = Color.Green;

            this.MouseDown += OnMouseDown;
        }

        private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
        {
            this.Parent.BackColor = Color.Red;
        }
    }
}
This exploits the fact that the Parent of a Control is a Control.

But, this is definitely not elegant, and probably not what you are looking for, so: let's make it more interesting, more like what you might do in a "real" app.

Like the approach OriginalGriff showed you, I will use a "broadcast" model, but I will implement it in a way that, imho, is more modern (uses a newer .NET facility), and, imho, more clearly expresses intent when viewed by newcomers to WinForms:
public partial class PBX : PictureBox
{
    public PBX()
    {
        InitializeComponent();
        this.MouseDown += OnMouseDown;
    }

    public Action<string, Color> PBXOnColorChanged { set; get; }

    private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
    {
        // always check to be sure "something is listening" !
        if (PBXOnColorChanged != null) PBXOnColorChanged((sender as PBX).Name, this.BackColor);
    }
}
Instead of using the "classic" syntax for a Delegate/EventHandler, this uses the newer syntax of the Action structure; do keep in mind that an Action is a Delegate. In this example we handle/respond-to the Action being invoked in the Form host context:
private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.PBXOnColorChanged += PbxOnColorChanged;
    pictureBox2.PBXOnColorChanged += PbxOnColorChanged;
    pictureBox3.PBXOnColorChanged += PbxOnColorChanged;
}

private void PbxOnColorChanged(string pbxName, Color pbxBackColor)
{
    // do something with the data in 'pbxName and 'pbxBackColor

}
Yes, you could set the Panel BackColor to Red, if that's your thing Smile | :)

But, let's make this more like a real-world app by making the Form context not just get a one-way message, but, also send back a result to the sub-classed PictureBox that will determine what happens in the MouseDown event of the PictureBox:
public partial class PBX : PictureBox
{
    public PBX()
    {
        InitializeComponent();
        this.BackColor = Color.Green;
        this.MouseDown += OnMouseDown;
    }

    public Func<string, Color, Color> PBXOnColorChanged { set; get; }

    private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
    {
        if (PBXOnColorChanged != null)
        {
            Color result = PBXOnColorChanged((sender as PBX).Name, this.BackColor);

            if (result != this.BackColor && result != Color.Empty)
            {
                this.BackColor = result;
            }
            else
            {   // switch it back to green
                this.BackColor = Color.Green;
            }
        }
    }
}
Here we use the 'Func syntax that specifies the Type of its return value/object as well as its parameter Types.

In the Form context:
private Color PbxOnColorChanged(string pbxName, Color pbxBackColor)
{
    Color NextColor = Color.Empty;

    switch (pbxName)
    {
        case "pictureBox1":
            if (pbxBackColor == Color.Green) NextColor = Color.Yellow;
            break;
        case "pictureBox2":
            if (pbxBackColor == Color.Green) NextColor = Color.Red;
            break;
        case "pictureBox3":
            break;
    }

    return NextColor;
}
Note the event handler returns a Color value. Note that PictureBox3 wiii not change its Color when clicked because the event handler returns Color.Empty when it is clicked.

Elegant ? I don't think so ... trivial, really, but you can see some hopefully useful ideas in this example:

1. encapsulation, loose coupling: the sub-classed PictureBox has no access to any of its context's (Form) objects, properties, fields, etc.; it publicly exposes only one access point, a kind of "socket" (Action, or Func) that consumers of the object can "inject" a reference to executable code into at run-time.

2. the Form context, by design, limits its access to the instances of thr PictureBox it uses to a very structured message passing facility. It defines the PictureBox instances it creates as private.

3. the newer Action and Func language features make the task of implementing Delegates and EventHandlers much easier, an d, imho, easier to maintain. About the only limitation I am aware of compared to Delegates is that Delegate can use the 'params feature for passing a variable length Array of arguments; Action and Func cannot.

4. structured message passing is a useful model for dynamic exchange of data and implementation of object interaction.
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)

GeneralMessage Closed Pin
23-Oct-17 5:51
User 740747023-Oct-17 5:51 
GeneralRe: Newbie question - how to access this object Pin
BillWoodruff24-Oct-17 3:46
professionalBillWoodruff24-Oct-17 3:46 
GeneralMessage Closed Pin
24-Oct-17 12:56
User 740747024-Oct-17 12:56 
GeneralRe: Newbie question - how to access this object Pin
Jim_Snyder27-Oct-17 4:56
professionalJim_Snyder27-Oct-17 4:56 
QuestionAnyone experiencing that, .NET Standard project in VS2017 hanging while compiling Pin
Super Lloyd22-Oct-17 9:42
Super Lloyd22-Oct-17 9:42 
AnswerRe: Anyone experiencing that, .NET Standard project in VS2017 hanging while compiling Pin
Richard MacCutchan22-Oct-17 11:20
mveRichard MacCutchan22-Oct-17 11:20 
QuestionRe: Anyone experiencing that, .NET Standard project in VS2017 hanging while compiling Pin
Super Lloyd22-Oct-17 12:28
Super Lloyd22-Oct-17 12:28 
AnswerRe: Anyone experiencing that, .NET Standard project in VS2017 hanging while compiling Pin
Richard MacCutchan22-Oct-17 20:52
mveRichard MacCutchan22-Oct-17 20:52 
AnswerRe: Anyone experiencing that, .NET Standard project in VS2017 hanging while compiling Pin
jschell23-Oct-17 7:34
jschell23-Oct-17 7:34 
QuestionHow to start a form without being activated? [solved] Pin
alin120-Oct-17 14:48
alin120-Oct-17 14:48 
AnswerRe: How to start a form without being activated? Pin
BillWoodruff20-Oct-17 16:30
professionalBillWoodruff20-Oct-17 16:30 
AnswerRe: How to start a form without being activated? Pin
OriginalGriff21-Oct-17 0:03
mveOriginalGriff21-Oct-17 0:03 
GeneralRe: How to start a form without being activated? Pin
BillWoodruff21-Oct-17 0:27
professionalBillWoodruff21-Oct-17 0:27 
GeneralRe: How to start a form without being activated? Pin
OriginalGriff21-Oct-17 0:41
mveOriginalGriff21-Oct-17 0:41 
GeneralRe: How to start a form without being activated? Pin
alin121-Oct-17 7:04
alin121-Oct-17 7:04 
GeneralRe: How to start a form without being activated? Pin
alin121-Oct-17 7:00
alin121-Oct-17 7:00 
AnswerRe: How to start a form without being activated? Pin
Sascha Lefèvre21-Oct-17 7:20
professionalSascha Lefèvre21-Oct-17 7:20 

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.