|
-
Nov 16th, 2009, 08:54 PM
#1
Thread Starter
New Member
VB Radio button code help
Hi all,
Im new to VB and am using the 2008 express edition. So heres my issue.
Lets say i have two radio buttons to set up. when each independent RADB is selected it will show its own picture. how do i write the code for this? Appreciate any help.
-
Nov 16th, 2009, 09:33 PM
#2
Re: VB Radio button code help
You handle the CheckedChanged event for each RadioButton and, in the event handler, you test whether the RadioButton is checked and, if it is, you display your picture.
CheckedChanged is the default event for a RadioButton so you just double-click it in the designer to create the event handler.
-
Nov 16th, 2009, 10:25 PM
#3
Thread Starter
New Member
Re: VB Radio button code help
I understand how to do the double click thing. its when Im in the event handler. I am completely lost on how to link radbut1 to the picturebox, the same for radbut2. rather, i cant figure out the right code.
-
Nov 16th, 2009, 10:29 PM
#4
Re: VB Radio button code help
There is no link. It's two separate operations.
1.
test whether the RadioButton is checked
2.
if it is, you display your picture
Note the highlighted word, which should be your clue.
-
Nov 16th, 2009, 10:32 PM
#5
Re: VB Radio button code help
Think about what it is that you actually want to do before you try to write code to do it. If you try to write the code before you really even know what you're trying to achieve then you're doomed to fail. The algorithm, i.e. the steps you need to perform, come first and then the code to implement those steps comes after.
So, what is it that you want to do?
If the box is checked, display the picture.
Does that sound logical? Now, can you write code to implement that?
-
Nov 16th, 2009, 10:56 PM
#6
Thread Starter
New Member
Re: VB Radio button code help
Damn...this is kicking my ass. I guess thats expected for first timers. So here is what I came up with for radbut1;
If RadButton1.Checked Then PictureBox1.Show()
RadButton1.Checked = True
when I run it my radbut2 completely disappears and the radbut1 isnt checked.
how do I hide the image that isnt checked?
how do I get the radbut1 to show checked?
how do I get the radbut2 to stay showing?
Again, I appreciate whom ever might help.
-
Nov 16th, 2009, 11:07 PM
#7
Re: VB Radio button code help
Why are you setting the Checked property to True? If you do that then every time you uncheck it your RadioButton it will get checked again.
There's nothing in that code that would cause the other RadioButton to disappear.
Are you using two different PictureBoxes? Normally you'd just use one PictureBox that was visible all the time and set its Image property each time you wanted to change the picture.
-
Nov 16th, 2009, 11:11 PM
#8
Thread Starter
New Member
Re: VB Radio button code help
I was just trying to follow some kind of examples that were kinda close to what I needed. I am using two different picture boxes as I dont know how to use one. Sorry if im a bit ignorant in this area. i have never used this program before.
-
Nov 16th, 2009, 11:29 PM
#9
Thread Starter
New Member
Re: VB Radio button code help
was I even close with that code?
-
Nov 16th, 2009, 11:42 PM
#10
Re: VB Radio button code help
Have a look at what I said earlier:
If the box is checked, display the picture.
Now look at your first line of code:
Code:
If RadButton1.Checked Then PictureBox1.Show()
They look a pretty close match, don't they? Now, what is the second line supposed to be achieving? Each time the Checked property of the RadioButton changes, set it to True? Does that sound like something you want to do?
Last edited by jmcilhinney; Nov 16th, 2009 at 11:46 PM.
-
Nov 16th, 2009, 11:55 PM
#11
Thread Starter
New Member
Re: VB Radio button code help
Yes, I think they say the same for line one. So I think Im good on this line.
for line two im not sure what I want. do I need a second line code? i guess I would need a second line to tell it what picture to show? am I tracking with that?
I deleted my second picture box. How do I assign two pictures to the same box?
I noticed theres an error image, initial image and main. is that where I attach the two photos? how do I segregate them between the radbuttons?
-
Nov 16th, 2009, 11:59 PM
#12
Re: VB Radio button code help
You don't assign two pictures to the same box. Just like you can change the photo that's displayed in a picture frame, so you can change the Image that's displayed in a PictureBox. You need to create two Image objects to begin with, then can assign either one to the PictureBox's Image property whenever appropriate, e.g.
vb.net Code:
If Me.RadioButton1.Checked Then Me.PictureBox1.Image = image1 End If
Where exactly are the images coming from in the first place? Are they files or have you added them to your project resources?
-
Nov 17th, 2009, 12:02 AM
#13
Thread Starter
New Member
Re: VB Radio button code help
they are pics that are in my project folder.
-
Nov 17th, 2009, 12:06 AM
#14
Re: VB Radio button code help
 Originally Posted by Deepseeman
they are pics that are in my project folder.
Your project folder doesn't exist when you deploy your app so that's no good. They need to either be resources or else files in a specific location, e.g. the same folder as the EXE. Do they need to be edited at all after deployment? If not then they should be resources. In that case you can add them on the Resources page of the project properties and access them in code via My.Resources.
-
Nov 17th, 2009, 12:25 AM
#15
Thread Starter
New Member
Re: VB Radio button code help
figured out how to add to my resources. this is the code that I have input;
If Me.RadioButton2.Checked Then PictureBox1.Image
PictureBox1.Image = My.Resources.unhappy
this keeps returning errors...help
Also, my darn radbut2 keeps disappearing after I run the debug! why? Im so frustrated. my moms getting on me too. arrgghhh1
-
Nov 17th, 2009, 12:32 AM
#16
Re: VB Radio button code help
Code:
If Me.RadioButton2.Checked Then PictureBox1.Image
PictureBox1.Image = My.Resources.unhappy
PictureBox.Image is a property, not a method. You can't just call it; you have to either get its value and use it or else assign a new value to it. I'd do it like like this:
vb.net Code:
If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy End If
but you can condense it into one line if you prefer:
vb.net Code:
If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy
-
Nov 17th, 2009, 12:32 AM
#17
Member
Re: VB Radio button code help
If your getting errors your not posting your whole code.
Aaaaaaannnd.. dont listen to a damn word I have to say. JMC will usually point you in the right direction.
Last edited by Nicelydone; Nov 17th, 2009 at 12:34 AM.
Reason: Responded too quickly.
NicelyDone: The Visual Studio 2008 Newb 
There are 10 types of people in this world...Those who know binary and those who don't. 
*cough* Asimov *cough*
I used to be a pro at HTML...Wait, nooooo! Epic fail
-
Nov 17th, 2009, 12:37 AM
#18
Thread Starter
New Member
Re: VB Radio button code help
I input your recommendation and debugged it. no errors but no pictures showed up and my radbut2 disappeared again. this is my code for the radiobuttons;
Private Sub RadButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.CheckedChanged
If Me.RadButton1.Checked Then PictureBox1.Image = My.Resources.happy1
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy
End Sub
-
Nov 17th, 2009, 12:39 AM
#19
Thread Starter
New Member
Re: VB Radio button code help
what am I doing wrong? im so mad that I cant get it!
-
Nov 17th, 2009, 12:40 AM
#20
Re: VB Radio button code help
I would suggest that you start a new project and see if the same thing happens. Either there's something you're not telling us or else either your project or your IDE are corrupted in some way.
-
Nov 17th, 2009, 12:48 AM
#21
Thread Starter
New Member
Re: VB Radio button code help
got it. moms kicking me off the computer and I need to go to bed. I will start over in the am. So heres a couple of questions;
in the picturebox, are there supposed to be any specially assigned settings or pics?
I noticed a visible tab, is that supposed to be true or false?
here is my whole stupid assignment codes. if you guys have the time, maybe you can find my stupid mistakes.
thank you for you patience and help.
Public Class SendAMessageToAFriendByEddieCulver16Nov09
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'end the application by closing the window
Me.Close()
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFriendA.CheckedChanged
txtFriendA.Text = txtMessage.Text
chkFriendA.Enabled = False
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFriendB.CheckedChanged
txtFriendB.Text = txtMessage.Text
chkFriendB.Enabled = False
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFriendC.CheckedChanged
txtFriendC.Text = txtMessage.Text
chkFriendC.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Clear the input and output text boxes
txtMessage.Text = ""
txtFriendA.Text = "" 'not necessary, see comments below
txtFriendB.Text = ""
txtFriendC.Text = ""
'Set back color to white
radwhte.Checked = True 'sets BackColor to white for all output text boxes
'Reset and enable the "send to" check boxes
chkFriendA.Checked = False
chkFriendB.Checked = False
chkFriendC.Checked = False
chkFriendA.Enabled = True
chkFriendB.Enabled = True
chkFriendC.Enabled = True
'Set focus
txtMessage.Focus() 'puts the focus on the txtMessage text box
End Sub
Private Sub RadGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadGreen.CheckedChanged
txtFriendA.BackColor = Color.Green
txtFriendB.BackColor = Color.Green
txtFriendC.BackColor = Color.Green
End Sub
Private Sub RadRed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadRed.CheckedChanged
txtFriendA.BackColor = Color.Red
txtFriendB.BackColor = Color.Red
txtFriendC.BackColor = Color.Red
End Sub
Private Sub radwhte_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radwhte.CheckedChanged
txtFriendA.BackColor = Color.White
txtFriendB.BackColor = Color.White
txtFriendC.BackColor = Color.White
End Sub
Private Sub RadButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.CheckedChanged
If Me.RadButton1.Checked Then PictureBox1.Image = My.Resources.happy1
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy
End Sub
End Class
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
|