Question radio button

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
I have 3 radio buttons rbUrl1,2,3 inside a group box gbUrl and i was wondering if this code can be simplified into 1 line?

VB.NET:
                If rbUrl1.Checked Then
                    llWebsite.Text = tbWebsite1.Text
                ElseIf rbUrl2.Checked Then
                    llWebsite.Text = tbWebsite2.Text
                Else
                    llWebsite.Text = tbWebsite3.Text
                End If
 
Yes and no. It could be done in one line but that line would not be simple. You should aim for your code to be concise, which means a combination of brief and clear. Don't overdo the brevity and sacrifice clarity in the process. The code you have is probably the best way to do it.

Alternatively, you could always handle each CheckedChanged event for the RadioButtons and assign the appropriate TextBox to a variable in each. Later on you can then simply get the TextBox from that variable without having to test any RadioButtons at all.
 
But how do you make a universal statement that is just a single line. Say its not just 3 buttons, but 1000 buttons. 1 line looks a lot better then?

VB.NET:
Dim currRb as integer
.
.

Select Case currRb
Case 1
                    llWebsite.Text = constant1.tostring
Case 2
                    llWebsite.Text = constant2.tostring
Case 3
                    llWebsite.Text = constant3.tostring
End Select
How do i get that 1 in currRb into the end of constant and make it "constant" + "1" ? so i can just have llwebsite.text = (constant + currRb) . tostring ....
 
You seem to be assuming that you're going to be able to replace that code with a single line that is going to be short and simple. You aren't. Any single line that you write is going to be long and complex. Writing a single, long, complex line of code is very bad.

There are all sorts of ways that you could achieve the same end but none of them are going to involve a single line of code without that code containing a reference to every single RadioButton control. You have no choice but to set up some sort of relationship between the RadioButtons and the values they correspond to. There are various ways to do that but none of them is going to involve a single line of code.
 
well maybe not a single line but less lines. i need to swap the contents of the textboxes each of the radiobutton is related to... before that i need to get the data from a 2 dimension array where the 1st dimension is govern by a set of constants. i have to first get the correct constants with the case statement.

VB.NET:
                Dim urlPrev, urlCurr, urlNext As Integer
                Dim webPrev, webCurr, webNext As Integer
                Select Case currRb
                    Case 1
                        urlCurr = mData.Url1
                        urlNext = mData.Url2
                        webCurr = mData.Webpage1
                        webNext = mData.Webpage2
                    Case 2
                        urlPrev = mData.Url1
                        urlCurr = mData.Url2
                        urlNext = mData.Url3
                        webPrev = mData.Webpage1
                        webCurr = mData.Webpage2
                        webNext = mData.Webpage3
                    Case 3
                        urlPrev = mData.Url2
                        urlCurr = mData.Url3
                        webPrev = mData.Webpage2
                        webCurr = mData.Webpage3
                End Select
then use that constant in the array to get the value to swap.this is all inside a menu click.
VB.NET:
   Select Case menuItem.Text
                    Case "Move Up"
                        tempUrl = m(urlCurr, index)
                        tempWebpage = m(webCurr, index)

                        m(urlCurr, index) = m(urlPrev, index)
                        m(webCurr, index) = m(webPrev, index)

                        m(urlPrev, index) = tempUrl
                        m(webPrev, index) = tempWebpage
                    Case "Move Down"
                        tempUrl = m(urlCurr, index)
                        tempWebpage = m(webCurr, index)

                        m(urlCurr, index) = m(urlNext, index)
                        m(webCurr, index) = m(webNext, index)

                        m(urlNext, index) = tempUrl
                        m(webNext, index) = tempWebpage
                    Case "Clear"
                        m(urlCurr, index) = ""
                        m(webCurr, index) = ""
                End Select
cant this be simplified? since when currRb is 1, urlCurr use url1 and when currRb is 2 urlCurr use url2, currRb is 3 , urlCurr use url3.
 
I think you should look at creating arrays or collections beforehand. Then you can get the index of the checked RadioButton like so:
VB.NET:
Dim checkedIndex = myRadioButtonArray.IndexOf(myRadioButtonArray.First(Function(rb) rb.Checked))
You can then use that index to get the other required values from other arrays or collections.
 
I already have the checked radiobutton. The currRb is the radio button's number. This is actually in a contextmenustrip when i right click one of the radio button. Then i use
VB.NET:
                Dim currRb As Integer = CInt(cmsRb.SourceControl.Name.ToString.Substring(cmsRb.SourceControl.Name.ToString.Length - 1))

to determine if its rb1, 2 or 3

what i want to know is how to get from 1 to the urlCurr = mData.Url1
and 2 to urlCurr = mData.Url2

A universal way to get 'x' to urlCurr = mData.Url'x'
and 'x-1' to urlCurr = mData.Url'x-1'
 
what i want to know is how to get from 1 to the urlCurr = mData.Url1
and 2 to urlCurr = mData.Url2

A universal way to get 'x' to urlCurr = mData.Url'x'
and 'x-1' to urlCurr = mData.Url'x-1'

That would be, as I've already said, using an array or collection. You're asking how to use an index to get a single value from a list of multiple values. Isn't that exactly what arrays and collections are for?
 
What i want to know is can you paste together a variable name?

like if i have

VB.NET:
Dim A1 as integer
Dim A2 as integer
Dim B as integer
Dim C as integer
say they are names you cannot change. so no array.

VB.NET:
if C = 1 then
  B = A1
elseif C=2 then
  B = A2
end if
this is the result i want to achive but need it to be a single B = statement instead of 2 separate B = statement. if its just a string i could have done
VB.NET:
C = 1
B = ("A" + C.ToString)      'B = A1
but since A1 is a variable can you somehow paste "A" with the C value and turn it into the variable A1 or A2 then just use one B = statement with it?
 
Last edited:
What i want to know is can you paste together a variable name?

You can't. Variables are identifiers, not strings, so you can't treat them like strings. If you want to join together a couple of strings and have the result identify something then you'd have to use a Dictionary where the keys are strings. Again, this requires you to construct a collection, i.e. the Dictionary, in code.
 
Back
Top