Onload of form, have certain radio buttons selected
I have 2 forms. I would like to check a radio button as soon as I load another form (the button is in the second form). I tried
OnLoad(radioBu开发者_JS百科tton2.Checked)
but it hasnt worked. Any ideas?
If I understand correctly, you have a radio button on one of your forms (let's refer to it as form1
so we can distinguish it), and want to alter it upon loading another form (which I'll refer here as form2
).
There are several things you need to keep in mind:
- You need to ensure that the button is accessible from outside
form1
. If both forms are part of the same project (which I guess it's the case), then you need to ensure that the button is one ofprotected internal
,internal
, orpublic
. form1
is actually a class; but the form that shows up on your screen isn't theform1
class itself: it is an instance of the class. You will need a reference to that instance.- To reach a member of an object, you use the
.
(dot) operator. For example, if your instance ofform1
was namedmyForm1
, then you would need to typemyForm1.radioButton2
to refer to the radio button. When you omit the object reference, it defaults to the current object (this is, the object whose code is executing). So when you are typing code forform1
you can omit the reference, but to access it fromform2
, you need it (otherwise, the compiler will think that you are trying to reach something named "radioButton2" withinform2
, which probably doesn't even exist). - A radio button's
Checked
is a property (more specifically, abool
property): you can get or set the values of properties, but refering to a property without doing anything with it is normally pointless, and on most cases won't even compile. - If you want to execute code in response to some event, then you need to register an event handler. An event handler is just a function that returns
void
and takes a couple of arguments (anobject
and anEventArgs
or some subclass of it). Registering the handler can be done programaticaly, but you're probably better off doing it from the designer. To create and register an event handler, follow these steps: 5.1. Select the form on the designer, and go to the Properties window. 5.2. Switch to the events view (a small lightning icon) 5.3. Find the event you want to handle. In this case,OnLoad
, and double-click on it. 5.4. Voilà! The designer has created a method stub for your new event handler, registered it for the event, and sent you into the code view so you can fill it up.
So, assuming you have the reference to your form1
instance (I'll get into that soon), to mark the radio button you would need a statement like this:
myForm1.radioButton2.Checked = true;
This will set the Checked
property of the radioButton2
control contained on the myForm1
instance of the form1
class to true
; which effectively makes the radio button to appear checked.
Now, to the juicy part: how to get a reference to the form? That depends on how you (or the IDE, on your behalf) created it.
If form1
is the startup form of your application, and you are sticking to the default way to do things in Visual Studio, then take a look at the Program
class the Studio created for your project. There should be a function there, named Main
. At some point, you'll see a line similar to this:
Application.Run(new form1());
the new form1()
part creates a new instance of form1
, and passes it to Application.Run()
(don't bother too much now about what this call does, we are only interested on the reference). The problem is that the reference is used on the fly and not saved, but we can fix that: add something like this on the Program
class, outside Main
:
internal static form1 myForm1;
Then replace the Application.Run()
call with these two lines:
myForm1 = new form1();
Application.Run(myForm1);
The call will do the same thing, but by breaking it down into two steps, we got the ability to save the reference to the static field myForm1
.
Now, from anywhere on your program, you can use Program.myForm1
to refer to that form. So, on your OnLoad event handler in form2
, all you need to do is:
Program.myForm1.radioButton2.Checked = true;
Despite all of this, if you are "calling" your form2
from form1
, then you can save yourself some tedious work by checking the button just before passing control to the secondary form, something like this:
radioButton2.Checked = true;
// Code that shows your "form2" goes here.
On either case, remember to replace form1
and form2
with the actual names of your forms.
Hope this helps.
You should simply set someRadioButton.Checked
to true
.
There's no point in waiting for OnLoad
.
If, for some reason, you do want to wait for the Load
event, you need to add a handler for the event and put your code in the handler.
精彩评论