Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
AnswerRe: Trying to make a DataGridView field a color if a condition is met. Pin
Mathi Mani22-May-15 12:20
Mathi Mani22-May-15 12:20 
GeneralRe: Trying to make a DataGridView field a color if a condition is met. Pin
Norris Chappell22-May-15 12:25
Norris Chappell22-May-15 12:25 
GeneralRe: Trying to make a DataGridView field a color if a condition is met. Pin
Norris Chappell22-May-15 12:47
Norris Chappell22-May-15 12:47 
GeneralRe: Trying to make a DataGridView field a color if a condition is met. Pin
Mathi Mani22-May-15 12:49
Mathi Mani22-May-15 12:49 
GeneralRe: Trying to make a DataGridView field a color if a condition is met. Pin
Norris Chappell22-May-15 12:52
Norris Chappell22-May-15 12:52 
AnswerRe: Trying to make a DataGridView field a color if a condition is met. Pin
Mathi Mani22-May-15 13:00
Mathi Mani22-May-15 13:00 
GeneralRe: Trying to make a DataGridView field a color if a condition is met. Pin
Norris Chappell23-May-15 11:11
Norris Chappell23-May-15 11:11 
Questionway around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 6:49
professionalBillWoodruff22-May-15 6:49 
I. Consider this scenario in WinForms:

1. you decide you want to have a custom UserControl/Component implement an Interface for the usual reasons you use Interfaces.

2. since you cannot use Fields in an Interface, you implement Properties ... that's the way Interfaces work in C# / .NET.

3. in the custom UserControl/Component that inherits the Interface, you must, of course, implement those Properties the Interface specifies as 'Public ... that's the way Interfaces work in C# / .NET.

4. when you then place/drag-drop an instance of the custom UserControl/Component on a Form:

a. if you examine the Designer.cs file for the Form: you will see that Public Properties of whatever Type declared in the UserControl/Component are initialized to null ... that's the way a WinForm works.

II. How is this an issue ?

1. in the case that you wish to set the values of the Interface-specified Properties in the UserControl/Component when they are ... internally ... initialized:

a. any initialization you've done will be erased by the execution of the code in the Designer.cs file as described in 4.a.

b. you might think you can use some Event of the UserControl/Component ... like Load, or even Layout ... to initialize the Properties: you would be wrong; the code in the Designer.cs file is executed last. And, forget using 'Layout ... design-time side-effects that are "freaky."

You might think you could use the Form's ControlAdded event: sorry, that won't work.

III. why not just call the code that initializes the Properties in the UserControl/Component in the Form after the Form is initialized ... for example, in the Form's Load event ?

a. you'll have to trust me here when I say that there is a compelling reason not to do that.

IV. What is the current work-around

Fairly simple ... if you know how to use a custom ApplicationContext, and understand what that requires.

So I switched to that. Since in that application-model I create all the Forms inside a static controller-class, then it's straightforward to invoke initialization in the right sequence.

V. What I have not considered yet:

a. in the Form's Load event doing a recursive search of all Controls, and calling initialization code for each instance of the custom UserControl/Component.

VI. other thoughts

1. while I think I understand why Interfaces cannot contain Fields, why they can't have specifiers like 'ref and 'out (thanks to Eric Lippert's essays), I sometimes wonder why a property in an Interface could not have an access modifier, and why the implementation of a Property in a Class that inherits from an Interface cannot implement that Property with a 'private modifier.

I hope that when I grow up I will understand these things Smile | :)

So, here's a little code illustration of the above:
C#
// Interface
public interface IHasSomething
{
	Dictionary<int, string> IntToString { set; get; }
}

// Component definition that subscribes to the Interface
// usual 'using statements omitted ...
namespace SomeNameSpace
{
    public partial class SomeComponent : TextBox, IHasSomething
    {
        // this is called at run-time when you create a new instance in code
        public SomeComponent() { InitializeComponent(); }

        // this is called when you design-time place the Component in a ContainerControl
        public SomeComponent(IContainer container)
        {
            container.Add(this);
            InitializeComponent();

            this.AutoSize = true;

            // you might think you could initialize this here
            IntToString = new Dictionary<int, string>();
        }

        // implements the Interface
        [Browsable(false)]  // don't show in design-time Property Browser
        public Dictionary<int, string> IntToString { set; get; }
    }
}
Now assume you've compiled, and drag-dropped an instance of the custom Component onto a Form; that Form has a Button which when clicked tries to access the Dictionary implemented in the custom Component:
C#
// Form that uses an instance of the custom Component
// usual 'using statements omitted ...
namespace SomeNameSpace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // fail here: Dictionary has been nulled out in Designer.cs
            someComponent1.IntToString.Add(1,"hello");
        }
    }
}
And, here you can see where the Dictionary was nulled out in the Designer.cs code:
C#
// 
// someComponent1
// 
this.someComponent1.IntToString = null;

«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.


modified 23-May-15 4:02am.

AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Sascha Lefèvre22-May-15 7:04
professionalSascha Lefèvre22-May-15 7:04 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:12
professionalBillWoodruff22-May-15 21:12 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Sascha Lefèvre24-May-15 3:33
professionalSascha Lefèvre24-May-15 3:33 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff24-May-15 22:42
professionalBillWoodruff24-May-15 22:42 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Sascha Lefèvre25-May-15 4:21
professionalSascha Lefèvre25-May-15 4:21 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff25-May-15 7:54
professionalBillWoodruff25-May-15 7:54 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Richard Deeming22-May-15 7:11
mveRichard Deeming22-May-15 7:11 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:27
professionalBillWoodruff22-May-15 21:27 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff24-May-15 22:46
professionalBillWoodruff24-May-15 22:46 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Pete O'Hanlon22-May-15 8:06
mvePete O'Hanlon22-May-15 8:06 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:33
professionalBillWoodruff22-May-15 21:33 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Pete O'Hanlon22-May-15 21:47
mvePete O'Hanlon22-May-15 21:47 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Alan N22-May-15 14:15
Alan N22-May-15 14:15 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:35
professionalBillWoodruff22-May-15 21:35 
QuestionHow to get the directory where DDL is installed Pin
Antonio Guedes22-May-15 6:14
Antonio Guedes22-May-15 6:14 
AnswerRe: How to get the directory where DDL is installed Pin
Eddy Vluggen22-May-15 6:56
professionalEddy Vluggen22-May-15 6:56 
GeneralRe: How to get the directory where DDL is installed Pin
Antonio Guedes22-May-15 10:06
Antonio Guedes22-May-15 10:06 

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.