c# - accessing a non-static member error while using form/controls from static method -
in project of mine have code following in class i've written named keyboardhook...
private static intptr keyboardhookid = intptr.zero; public static intptr hookcallback(int ncode, intptr wparam, intptr lparam) { if (ncode >= 0 && wparam == (intptr)wm_keydown) { int vkcode = marshal.readint32(lparam); //update ui here... } return callnexthookex(keyboardhookid, ncode, wparam, lparam); }
i'd update ui statement like
form1.label1.text = vkcode.tostring();
...but causes problem c# compiler; namely accessing non-static member.
need create reference form1 object,ie.
application.run(new form1());
what do?
in solutionexplorer open program.cs , change code there following
static class program { public static form1 mainform = null; /// <summary> /// main entry point application. /// </summary> [stathread] static void main() { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); mainform = new form1(); application.run(mainform); } }
here instance of form1 class created , started application.run command. note mainform initialized null in line 3 , not:
public static form1 mainform = new form1();
this because application.setcompatibletextrenderingdefault() method must called before first iwin32window object created in application, or invalidoperationexception thrown.
Comments
Post a Comment