-5

In a quick project, I have to use the If command to check if a radio button is checked or not, if it is checked I would make the Form's back color be replaced with the Radio button's forecolor. Example:

If rdoRed.Checked = True Then
Me.Backcolor = rdoRed.Forecolor

The teacher then challenged us to do this without the If/then commands, how would I come around this?

user3186159
  • 93
  • 1
  • 1
  • 4
  • 3
    My suggestion is to not cheat on your homework. – asawyer Feb 04 '14 at 02:33
  • 3
    Note the teacher challenged ***you***, not us... – davidsbro Feb 04 '14 at 02:36
  • Lol I already submitted my work, I was just looking for the answer – user3186159 Feb 04 '14 at 02:45
  • I just didn't take on the challenge of not using if/then since it was extra, but now that I'm done I want to find an answer Edit: This is the code I submitted: If rdoRed.Checked = True Then Me.Backcolor = rdoRed.Forecolor Elseif If rdoGreen.Checked = True Then Me.Backcolor = rdoGreen.Forecolor Elseif etc.etc. – user3186159 Feb 04 '14 at 02:55

2 Answers2

2

Maybe you can use Select Case here:

Select Case rdoRed.Checked
    Case True
       Me.Backcolor = rdoRed.Forecolor
    Case Else
       'Some other color 
End Select

And you can also use IIF

Me.Backcolor = IIf(rdoRed.Checked, rdoRed.Forecolor, Me.Backcolor)
davidsbro
  • 2,761
  • 4
  • 23
  • 33
GiorgiTBS
  • 369
  • 2
  • 4
-2

Try a ternary operator.

Me.Backcolor = If(rdoRed.Checked, rdoRed.Forecolor, [some other color])

Like this example: Is there a conditional ternary operator in VB.NET?

Community
  • 1
  • 1
Kenny Thompson
  • 1,494
  • 12
  • 28