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.
Printable View
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.
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.
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.
There is no link. It's two separate operations.
1.2.Quote:
test whether the RadioButton is checked
Note the highlighted word, which should be your clue.Quote:
if it is, you display your picture
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?Does that sound logical? Now, can you write code to implement that?Quote:
If the box is checked, display the picture.
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.
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.
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.
was I even close with that code?
Have a look at what I said earlier:Now look at your first line of code:Quote:
If the box is checked, display the picture.
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?Code:If RadButton1.Checked Then PictureBox1.Show()
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?
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.Where exactly are the images coming from in the first place? Are they files or have you added them to your project resources?vb.net Code:
If Me.RadioButton1.Checked Then Me.PictureBox1.Image = image1 End If
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.
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
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:Code:If Me.RadioButton2.Checked Then PictureBox1.Image
PictureBox1.Image = My.Resources.unhappy
but you can condense it into one line if you prefer:vb.net Code:
If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy End Ifvb.net Code:
If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy
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.
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
what am I doing wrong? im so mad that I cant get it!
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.
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