|
-
April 7th, 2009, 05:50 PM
#1
Having trouble access controls from another class... HELP PLZ!!!
Hello everyone, I have a problem. I have a main class, and a fuel class... in the main class i have 3 radio buttons, a combo box, and a button. Once one of the radio buttons is clicked, I check the index of the combobox. If the index is >=0 then the button is enabled. Here is my snipped of code (in the fuel class)...
public void enableParkingButton()
{
frmMain m = new frmMain();
if (m.rbtnParkingDaily.Checked == true || m.rbtnParkingWeekly.Checked == true || m.rbtnParkingMonthly.Checked == true)
{
q.btnPurchasePass.Enabled = true;
}
}
Basically once the program gets to that first if statement, it completely ignores it. I have been struggling with this for a few days now, and can't really do anything else until i figure this out.
thanx
By the way, I am using visual studio 2008 express edition.
Last edited by Boy_Narf; April 7th, 2009 at 09:41 PM.
-
April 7th, 2009, 09:43 PM
#2
Re: Having trouble access controls from another class... HELP PLZ!!!
Assuming that your main class is a form, you normally would not have access to any of that forms objects from another class. This is because they would be protected. However, if the class is within the form class (a child class) you can pass a reference to the form.
The problem is that
frmMain m = new frmMain();
in your example is not the same form that is displayed. It's a new copy that only exists in the enableParkingButton method.
Example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Checked = true;
Class1 tc = new Class1();
tc.testclass(this);
}
class Class1
{
public void testclass(Form1 formref)
{
Form1 m = formref;
bool testchecked = m.radioButton1.Checked;
}
}
}
Although you might have a reason for using another class I would recommend that you work with any or all of your form controls from within your main class.
Example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Checked = true;
testclass();
}
public void testclass()
{
bool testchecked = radioButton1.Checked;
}
}
Hope that helps. Good luck.
-
April 7th, 2009, 09:58 PM
#3
Re: Having trouble access controls from another class... HELP PLZ!!!
so, the best decision would be to make my fuel class a child within my main class?
I am slightly confused on how to fix this problem, as I am relatively new to C# and communicating between classes.
-
April 7th, 2009, 10:55 PM
#4
Re: Having trouble access controls from another class... HELP PLZ!!!
ok so i have tried making the parking class inherit from class Main, however it is still not working.
I do not have to create an instance of class Main anymore, but it still wont let me access any of the class Main controls.
for example... (this code is in the parking class which inherits from main class)
public void sendParkingToSummary()
{
String s;
String t = "";
String x = "";
if (rbtnParkingDaily.Checked == true)
{
t = "Daily";
}
else if (rbtnParkingWeekly.Checked == true)
{
t = "Weekly";
}
else if (rbtnParkingMonthly.Checked == true)
{
t = "Monthly";
}
x = txtParkingRate.Text;
s = c.currentFirst + " " + c.currentLast + " | Parking" + " | " + t + " | " + x + " | " + "??/??/??";
lbSummary.Items.Add(s).ToString();
//MessageBox.Show(s);
}
even though the radio buttons are checked, it still doesn't recognize them. Does inheriting from another class automatically create a new instance of that class?
thanx...
-
April 7th, 2009, 11:43 PM
#5
Re: Having trouble access controls from another class... HELP PLZ!!!
I can't really make a recomentation because I don't know the purpose of your fuel class.
I assume that the main class is a Windows Form class. The controls on that form belong to the that (the main) class and should be managed from within that class.
The decision to create and use additional classes is up to you, but when doing so you should be done to encapsulate functionality or members in a logical way. With that said, controls or objects that belong to the form should be managed from within the form class.
In the first example I provided, I showed how to access form controls from a different class because that seemed to be the direction you wanted to go. However, the second example is what I would consider the prefered method. In the second example, there is no second class. Just a method call.
Here's an example (single class with) an event handler method that is triggered whenever the state of the radio button is changed. Based on the checked state, the button on the form will either be enabled or disabled.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
button1.Enabled = true;
else
button1.Enabled = false;
}
}
Adding additional classes for support of this class depends on what you need to do and how you want to do it.
Example:
You might add an additional event handler for a button press to the form class above.
private void button1_Click(object sender, EventArgs e)
{
OtherClass Fuel = new OtherClass();
Fuel.DoSomething(radioButton1.Checked);
}
---------------------- Here's the other class.
namespace Workspace
{
class OtherClass
{
public void DoSomething(bool check)
{
//Do something with the parameter here!!
}
}
}
------------------------
This is a very brief example, but as a general rule if you are going to use a form control to determin how to manipulate another form control it should all be done within the form class. If you are going to write form data out to a file or do something else with it that no longer relates to the form controls, then passing the values to another class might be a better choice.
If you must access form controls from a different class then that class should either be a child of the form class or be derived from the form class. A class with no relation probably would not allow access to any of a forms controls (compile error) because a form class is usually protected.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|