Click here to Skip to main content
15,891,748 members
Home / Discussions / C#
   

C#

 
AnswerRe: Deploy POS.NET files Pin
ZurdoDev20-Jul-15 8:47
professionalZurdoDev20-Jul-15 8:47 
QuestionCreating a SELECT Query Based on Textbox Data Pin
John L. DeVito19-Jul-15 4:59
professionalJohn L. DeVito19-Jul-15 4:59 
GeneralRe: Creating a SELECT Query Based on Textbox Data Pin
PIEBALDconsult19-Jul-15 5:06
mvePIEBALDconsult19-Jul-15 5:06 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
Wendelius19-Jul-15 5:25
mentorWendelius19-Jul-15 5:25 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
Dave Kreskowiak19-Jul-15 6:29
mveDave Kreskowiak19-Jul-15 6:29 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
F-ES Sitecore19-Jul-15 22:29
professionalF-ES Sitecore19-Jul-15 22:29 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
Richard Deeming20-Jul-15 6:58
mveRichard Deeming20-Jul-15 6:58 
Questionan unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
BillWoodruff19-Jul-15 4:05
professionalBillWoodruff19-Jul-15 4:05 
I'm am puzzled by this:

1. I have a Dictionary whose Keys are Enumeration-Values, and whose Values are of Type Action<Control1, Control2>

2. At run-time I am calling a method that adds a LocationChanged EventHandler which is constructed as a lambda on-the-fly ... to Control1. This LocationChanged EventHandler will execute the Action described in #1 which will change the other Control, Control2.

When Control2 refers to a Form, and, at run-time, that Form has been closed using its Close Button: my impression, up to now, has been that references to that Form would be garbage collected, so if Control1 (another Form) was moved, and the LocationChanged event was triggered which attempted to modify Control1 (closed Form), I would expect a null reference error.

But, there is no run-time error thrown; I infer from that there must be valid references to the closed Form "still around."

I think I better illustrate this with a dumbed-down bare-bones code example: a WinForms project with a MainForm 'Form1, and second Form, 'Form2:
C#
enum SomeEnum
{
    whatever1,
    whatever2
}

private Dictionary<SomeEnum, Action<Control, Control>> EnumToAction;

private void Form1_Load(object sender, EventArgs e)
{
    EnumToAction = new Dictionary<SomeEnum, Action<Control, Control>>
    {
        {
            SomeEnum.whatever1, (control, control1) =>
            {
                control1.Top = control.Top;
            }
        },
        {
            SomeEnum.whatever2, (control, control1) =>
            {
                control1.Left = control.Right;
            }
        }
    };

    Form2 f2 = new Form2();
    f2.Show();

    SetAction(this, f2, SomeEnum.whatever1);
}

private void SetAction(Control bControl, Control dControl, SomeEnum someenum)
{
    bControl.LocationChanged += (sender, args) =>
    {
        EnumToAction[someenum](bControl, dControl);
    };

    // execute once to set initial alignment
    EnumToAction[someenum](bControl, dControl);
}
Running the above code, then closing 'Form2 by clicking its Form Close Button, and then moving 'Form1 does not then throw an error which makes me believe that the reference to 'Form2 in that created-lambda EventHandler must still be valid.

I guess I could keep a Dictionary of Control, EventHandler around to deal with this ... to explicitly remove EventHandlers ... but, then, you've got different types of EventHandlers (differences in parameters) to deal with.

Ideas ?

thanks, Bill
«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 19-Jul-15 10:40am.

AnswerRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
Alan N19-Jul-15 9:44
Alan N19-Jul-15 9:44 
GeneralRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
BillWoodruff19-Jul-15 11:46
professionalBillWoodruff19-Jul-15 11:46 
GeneralRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
Member 1113646119-Jul-15 10:01
Member 1113646119-Jul-15 10:01 
GeneralRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
BillWoodruff19-Jul-15 11:49
professionalBillWoodruff19-Jul-15 11:49 
AnswerRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
Richard Deeming20-Jul-15 6:43
mveRichard Deeming20-Jul-15 6:43 
QuestionApplication is Crashing Pin
Jassim Rahma19-Jul-15 1:10
Jassim Rahma19-Jul-15 1:10 
AnswerRe: Application is Crashing Pin
OriginalGriff19-Jul-15 1:36
mveOriginalGriff19-Jul-15 1:36 
AnswerRe: Application is Crashing Pin
Dave Kreskowiak19-Jul-15 3:54
mveDave Kreskowiak19-Jul-15 3:54 
QuestionHow to get google place predictions with latitude, longitude and address based on Textbox entry in c# Pin
sr15918-Jul-15 19:05
sr15918-Jul-15 19:05 
AnswerRe: How to get google place predictions with latitude, longitude and address based on Textbox entry in c# Pin
OriginalGriff18-Jul-15 20:17
mveOriginalGriff18-Jul-15 20:17 
QuestionCar tracker Pin
lolinga218-Jul-15 8:58
lolinga218-Jul-15 8:58 
AnswerRe: Car tracker Pin
Dave Kreskowiak18-Jul-15 9:36
mveDave Kreskowiak18-Jul-15 9:36 
GeneralRe: Car tracker Pin
lolinga218-Jul-15 9:42
lolinga218-Jul-15 9:42 
GeneralRe: Car tracker Pin
Mycroft Holmes18-Jul-15 14:35
professionalMycroft Holmes18-Jul-15 14:35 
QuestionError Converting String to Int Pin
Broodje Games18-Jul-15 2:26
Broodje Games18-Jul-15 2:26 
AnswerRe: Error Converting String to Int Pin
OriginalGriff18-Jul-15 2:31
mveOriginalGriff18-Jul-15 2:31 
AnswerRe: Error Converting String to Int Pin
miki-bgd18-Jul-15 5:12
miki-bgd18-Jul-15 5:12 

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.