Update Controls from an eventController

Mar 22, 2009 at 5:44 PM
OK, I have got to be missing something simple here; I am exactly 4 days old to C#. 

I am trying to use a simple grid to display text values.  When the selected row is changed by KB or mouse I need to fire off an event that will be used to update the value of several text boxes on the parent form.  I created an eventController for the OnFocusEntered event which works great.  However I cannot call any of the parent code; I receive “"cannot access a non-static member of outer type…"  After some research I found out that apparently in C# nested classes are not permitted to access parent classes.  This being the case I cannot figure out how to use any type of event controller to update external components.  Any suggestions would be greatly appreciated.
Coordinator
Mar 22, 2009 at 7:36 PM
>> “"cannot access a non-static member of outer type…" 
which code are you exactly trying to access?
Mar 22, 2009 at 10:22 PM

Forgive me if I am doing something stupid but here is a very simple example of what I am trying to do       
------------------------------------------
 public class ChangeLogGrid_FocusEnteredEvent : SourceGrid.Cells.Controllers.ControllerBase
        {

            public override void OnFocusEntered(SourceGrid.CellContext sender, EventArgs e)
            {
                    Int32 SomeIndex;
                    SomeIndex = 1;
                    //<Code to determin the Index value stored in a hidden column>           
                    UpdateFormControlls(SomeIndex);
             }
        }
-------------------------------------------

Gives me "Cannot access a non-static member of outer type 'ChangeMgr.Form1' via nested type 'ChangeMgr.Form1.ChangeLogGrid_FocusEnteredEvent"

for UpdateFormControlls(SomeIndex). 
UpdateFormControlls just pulls some data from an XML file and displays them in textboxes.

From my understanding that kind of makes since because UpdateFormControlls() is part of the Form1 Class.  So how do I perform an action on a control in Form1 from inside the nested controller which is in class ChangeLogGrid_FocusEnteredEvent.

Coordinator
Mar 23, 2009 at 5:19 PM
Ok, i probably got the problem.

So the idea why compiler is complaining is as follows : even though your class ChangeLogGrid_FocusEnteredEvent is inner class of Form1, you can not access Form1 non-static members. Why, well, because to access a non-static member, you have to have a reference for that object. Now if i am an instance of an inner class, it is not known what instance of Form1 i should access. This is what the compiler is complaining about.

Now, i am no expert or even beginner of Java, but i speculate that in java this is possible, since class ChangeLogGrid_FocusEnteredEvent automatically inherits from class Form1, with ability to access even private fields. This is not the case in c# - inner class is not inheriting from outer class. Please correct me if i am wrong.


Mar 25, 2009 at 12:01 AM
I believe that is correct.  From my research this is not a problem on the Java side.
Coordinator
Mar 25, 2009 at 6:26 AM
Ok.

But did you get what you want? Did you make your code work?
Mar 25, 2009 at 2:39 PM
No, I’ve spent some more time and tried a few things last night but unfortunately still cannot find a way to get around C#'s inner class restrictions that works.
Coordinator
Mar 25, 2009 at 7:33 PM
I would suggest this approach - move out your inner class out of Form and work with it as it would be a normal class.

This would not confuse you. Personally, in very rare i occasions i do use inner classes. There's just no need of this.
Mar 27, 2009 at 2:21 PM
Edited Mar 27, 2009 at 2:23 PM

Thanks for nudging me away from the wrong approach.  I was able to easily accomplish what I needed with the following code.

--------------------------------------------------
EntryGrid.Selection.CellGotFocus +=

new SourceGrid.ChangeActivePositionEventHandler(Selection_FocusChanged);

 

 

 

void Selection_FocusChanged(object sender, SourceGrid.ChangeActivePositionEventArgs e)
{
        
Int32 RowNumber;
        RowNumber = e.NewFocusPosition.Row;
        //<Code to determin the Index value stored in a hidden column>   
        PopulateChangeLogForm(SomeIndex));
}
--------------------------------------------