wa22guy Posted October 25, 2004 Share Posted October 25, 2004 frmMain has: public int m_Option = 1; I'm trying to access m_Option from frmMain. Am I doing this right? I don't think the "frmMain frmMain = new frmMain();" line is correct. private void frmOptions_Load(object sender, System.EventArgs e) { ? ? ?frmMain frmMain = new frmMain(); ? ? ? ? ? ?if (frmMain.m_Option == 0) ? ? ?{ ? ? ? ?rdoFolder.Checked = true; ? ? ?} } Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 25, 2004 Author Share Posted October 25, 2004 I get an exception when the frmOptions loads but it says it can find the line of code that caused the problem. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted October 26, 2004 Share Posted October 26, 2004 I get an exception when the frmOptions loads but it says it can find the line of code that caused the problem. 584805636[/snapback] Slap a try/catch around that. Also, you need to show the form before you can access child controls. The handle for the form has not yet been created. Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 (edited) Slap a try/catch around that.Also, you need to show the form before you can access child controls. The handle for the form has not yet been created. 584806025[/snapback] Just figured it out... I needed to put some of my code in the constructor of frmMain instead of the frmMain_Load() event. Problem solved :) Edited October 26, 2004 by wa22guy Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 Ok, I still am running into problems with this. When I set "frmMain.m_Option = 1" on the Options form, m_Option still equals 0 on the frmMain. Grr... why is the variable not transferring from my second form to my first form after a change? Link to comment Share on other sites More sharing options...
0 wacko412 Posted October 26, 2004 Share Posted October 26, 2004 don't you need an else statement that way it knows what to do if the option is not 0 ?? Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 don't you need an else statement that way it knows what to do if the option is not 0 ?? 584807250[/snapback] Yeah, I have it, I just didnt list it above. Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted October 26, 2004 MVC Share Posted October 26, 2004 So as I am to understand it, you called your options dialog from your main dialog and you are trying to set/get the m_Option member of your main dialog to 1 from your options dialog? If so, you are creating a new instance of your main dialog from within your options dialog which is a waste of time because that will only get/set the member of the new instance, not your current main dialog object. Instead you need to pass the instance varible of your main form to your options dialog and then set/get the member value. // in Main Form, options button click command frmOptions optionsDlg = new frmOptions(); frmOptions.SetMainFormObject(this); optionsDlg.ShowDialog(); // in your Options Dialog private frmMain m_MainFormObject; ..... public void SetMainFormObject(frmMainFormObject) { this.m_MainFormObject = frmMainFormObject; } ..... MyOption.Checked = (this.m_MainFormObject.m_Option == 1); ..... this.m_MainFormObject.m_Option = 1; If I have misunderstood what you are trying to do, just ignore me. note: I wrote that quickly so there are bound to be some mistakes. Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 So as I am to understand it, you called your options dialog from your main dialog and you are trying to set/get the m_Option member of your main dialog to 1 from your options dialog? If so, you are creating a new instance of your main dialog from within your options dialog which is a waste of time because that will only get/set the member of the new instance, not your current main dialog object. Instead you need to pass the instance varible of your main form to your options dialog and then set/get the member value. // in Main Form, options button click command frmOptions optionsDlg = new frmOptions(); frmOptions.SetMainFormObject(this); optionsDlg.ShowDialog(); // in your Options Dialog private frmMain m_MainFormObject; ..... public void SetMainFormObject(frmMainFormObject) { ?this.m_MainFormObject = frmMainFormObject; } ..... MyOption.Checked = (this.m_MainFormObject.m_Option == 1); ..... this.m_MainFormObject.m_Option = 1; If I have misunderstood what you are trying to do, just ignore me. note: I wrote that quickly so there are bound to be some mistakes. 584807349[/snapback] You are the man. That's exactly what I wanted! Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 So as I am to understand it, you called your options dialog from your main dialog and you are trying to set/get the m_Option member of your main dialog to 1 from your options dialog? If so, you are creating a new instance of your main dialog from within your options dialog which is a waste of time because that will only get/set the member of the new instance, not your current main dialog object. Instead you need to pass the instance varible of your main form to your options dialog and then set/get the member value. // in Main Form, options button click command frmOptions optionsDlg = new frmOptions(); frmOptions.SetMainFormObject(this); optionsDlg.ShowDialog(); // in your Options Dialog private frmMain m_MainFormObject; ..... public void SetMainFormObject(frmMainFormObject) { ?this.m_MainFormObject = frmMainFormObject; } ..... MyOption.Checked = (this.m_MainFormObject.m_Option == 1); ..... this.m_MainFormObject.m_Option = 1; If I have misunderstood what you are trying to do, just ignore me. note: I wrote that quickly so there are bound to be some mistakes. 584807349[/snapback] On: public void SetMainFormObject(frmMainFormObject) { this.m_MainFormObject = frmMainFormObject; } Whats the type of frmMainFormObject supposed to be? Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted October 26, 2004 MVC Share Posted October 26, 2004 On: public void SetMainFormObject(frmMainFormObject) { this.m_MainFormObject = frmMainFormObject; } Whats the type of frmMainFormObject supposed to be? 584807437[/snapback] The same type as your main form, in your case, frmMain Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 The same type as your main form, in your case, frmMain 584807521[/snapback] Hmmm... I'm getting "An object is required for the nonstatic field, method, or property" error on frmDisplay.SetMainFormObject(this); // frmMain private void pctInternet_Click(object sender, System.EventArgs e) { frmDisplay displayDlg = new frmDisplay(); frmDisplay.SetMainFormObject(this); frmDisplay.ShowDialog(); } // frmDisplay private frmMain m_MainFormObject; --- public void SetMainFormObject(frmMain frmMainFormObject) { this.m_MainFormObject = frmMainFormObject; } Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted October 26, 2004 MVC Share Posted October 26, 2004 Hmmm... I'm getting "An object is required for the nonstatic field, method, or property" error on frmDisplay.SetMainFormObject(this); // frmMain private void pctInternet_Click(object sender, System.EventArgs e) { frmDisplay displayDlg = new frmDisplay(); frmDisplay.SetMainFormObject(this); frmDisplay.ShowDialog(); } // frmDisplay private frmMain m_MainFormObject; --- public void SetMainFormObject(frmMain frmMainFormObject) { this.m_MainFormObject = frmMainFormObject; } 584807546[/snapback] You are getting that error because you are trying to call the function on the class, not on the instance of the class. Observe: private void pctInternet_Click(object sender, System.EventArgs e) { frmDisplay displayDlg = new frmDisplay(); displayDlg.SetMainFormObject(this); displayDlg.ShowDialog(); } I am to blame for that sorry, it was a mistake I made in the first sample code I provided. Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 Thanks, it's exactly what I wanted. It works perfect! You're awesome :) Link to comment Share on other sites More sharing options...
Question
wa22guy
frmMain has:
I'm trying to access m_Option from frmMain. Am I doing this right? I don't think the "frmMain frmMain = new frmMain();" line is correct.
private void frmOptions_Load(object sender, System.EventArgs e) { ? ? ?frmMain frmMain = new frmMain(); ? ? ? ? ? ?if (frmMain.m_Option == 0) ? ? ?{ ? ? ? ?rdoFolder.Checked = true; ? ? ?} }Link to comment
Share on other sites
13 answers to this question
Recommended Posts