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(radioButton2.Checked)
but it hasnt worked. Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If I understand correctly, you have a radio button on one of your forms (let’s refer to it as
form1so we can distinguish it), and want to alter it upon loading another form (which I’ll refer here asform2).There are several things you need to keep in mind:
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.form1is actually a class; but the form that shows up on your screen isn’t theform1class itself: it is an instance of the class. You will need a reference to that instance..(dot) operator. For example, if your instance ofform1was namedmyForm1, then you would need to typemyForm1.radioButton2to 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 forform1you 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).Checkedis a property (more specifically, aboolproperty): 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.voidand takes a couple of arguments (anobjectand anEventArgsor 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
form1instance (I’ll get into that soon), to mark the radio button you would need a statement like this:This will set the
Checkedproperty of theradioButton2control contained on themyForm1instance of theform1class totrue; 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
form1is 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 theProgramclass the Studio created for your project. There should be a function there, namedMain. At some point, you’ll see a line similar to this:the
new form1()part creates a new instance ofform1, and passes it toApplication.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 theProgramclass, outsideMain:Then replace the
Application.Run()call with these two lines: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.myForm1to refer to that form. So, on your OnLoad event handler inform2, all you need to do is:Despite all of this, if you are “calling” your
form2fromform1, then you can save yourself some tedious work by checking the button just before passing control to the secondary form, something like this:On either case, remember to replace
form1andform2with the actual names of your forms.Hope this helps.