MESSY CODE

Evening.

I have a value stored in a database and depending on that value I need to check the check box if the value equals it. (I revtrive 1 value from the database into a textbox and I read the value of that) I have asked the question before anf they told me to databind the checkboxes but I would if the value was there for every checkbox which it is not.

As you can see from my code below it is very messy and there has to be a better way of doing it than this.

Any ideas?
If txtFlag22.Text = "1" Then
            ChkZeroPrice.CheckState = CheckState.Checked
        Else
            If txtFlag22.Text = "2" Then
                ChkNegPrice.CheckState = CheckState.Checked
            Else
                If txtFlag22.Text = "3" Then
                    ChkZeroPrice.CheckState = CheckState.Checked
                    ChkNegPrice.CheckState = CheckState.Checked
                Else
                    If txtFlag22.Text = "4" Then
                        ChkEnable.CheckState = CheckState.Checked
                    Else
                        If txtFlag22.Text = "5" Then
                            ChkEnable.CheckState = CheckState.Checked
                            ChkZeroPrice.CheckState = CheckState.Checked
                        Else
                            If txtFlag22.Text = "10" Then
                                ChkCondiment.CheckState = CheckState.Checked
                            Else
                                If txtFlag22.Text = "11" Then
                                    ChkZeroPrice.CheckState = CheckState.Checked
                                    ChkCondiment.CheckState = CheckState.Checked
                                Else
                                    If txtFlag22.Text = "12" Then
                                        ChkNegPrice.CheckState = CheckState.Checked
                                        ChkCondiment.CheckState = CheckState.Checked
                                    Else
                                        If txtFlag22.Text = "14" Then
                                            ChkCondiment.CheckState = CheckState.Checked
                                            ChkEnable.CheckState = CheckState.Checked
                                        Else
                                            If txtFlag22.Text = "20" Then
                                                ChkNoReceipt.CheckState = CheckState.Checked
                                            Else
                                                If txtFlag22.Text = "21" Then
                                                    ChkNoReceipt.CheckState = CheckState.Checked
                                                    ChkZeroPrice.CheckState = CheckState.Checked
                                                Else
                                                    If txtFlag22.Text = "22" Then
                                                        ChkNoReceipt.CheckState = CheckState.Checked
                                                        ChkNegPrice.CheckState = CheckState.Checked
                                                    Else
                                                        If txtFlag22.Text = "24" Then
                                                            ChkNoReceipt.CheckState = CheckState.Checked
                                                            ChkEnable.CheckState = CheckState.Checked
                                                        Else
                                                            If txtFlag22.Text = "30" Then
                                                                ChkNoReceipt.CheckState = CheckState.Checked
                                                                ChkCondiment.CheckState = CheckState.Checked
                                                            Else
                                                                If txtFlag22.Text = "40" Then
                                                                    ChkWeight.CheckState = CheckState.Checked
                                                                Else
                                                                    If txtFlag22.Text = "41" Then
                                                                        ChkWeight.CheckState = CheckState.Checked
                                                                        ChkZeroPrice.CheckState = CheckState.Checked
                                                                    Else
                                                                        If txtFlag22.Text = "42" Then
                                                                            ChkNegPrice.CheckState = CheckState.Checked
                                                                            ChkWeight.CheckState = CheckState.Checked
                                                                        Else
                                                                            If txtFlag22.Text = "44" Then
                                                                                ChkWeight.CheckState = CheckState.Checked
                                                                                ChkZeroPrice.CheckState = CheckState.Checked
                                                                            Else
                                                                                If txtFlag22.Text = "50" Then
                                                                                    ChkCondiment.CheckState = CheckState.Checked
                                                                                    ChkWeight.CheckState = CheckState.Checked
                                                                                Else
                                                                                    If txtFlag22.Text = "60" Then
                                                                                        ChkWeight.CheckState = CheckState.Checked
                                                                                        ChkNoReceipt.CheckState = CheckState.Checked
                                                                                    Else
                                                                                        If txtFlag22.Text = "100" Then
                                                                                            ChkWeightEntry.CheckState = CheckState.Checked
                                                                                        Else
                                                                                            If txtFlag22.Text = "101" Then
                                                                                                ChkZeroPrice.CheckState = CheckState.Checked
                                                                                                ChkWeightEntry.CheckState = CheckState.Checked
                                                                                            Else
                                                                                                If txtFlag22.Text = "102" Then
                                                                                                    ChkNegPrice.CheckState = CheckState.Checked
                                                                                                    ChkWeightEntry.CheckState = CheckState.Checked
                                                                                                Else
                                                                                                    If txtFlag22.Text = "104" Then
                                                                                                        ChkEnable.CheckState = CheckState.Checked
                                                                                                        ChkWeightEntry.CheckState = CheckState.Checked
                                                                                                    Else
                                                                                                        If txtFlag22.Text = "110" Then
                                                                                                            ChkCondiment.CheckState = CheckState.Checked
                                                                                                            ChkWeightEntry.CheckState = CheckState.Checked
                                                                                                        Else
                                                                                                            If txtFlag22.Text = "140" Then
                                                                                                                ChkWeight.CheckState = CheckState.Checked
                                                                                                                ChkWeightEntry.CheckState = CheckState.Checked
                                                                                                                If txtFlag22.Text Then
 
                                                                                                                End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If : End If

Open in new window

richard_garAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi richard_gar;

Using a select statement will be cleaner.

Fernando
Select Case txtFlag22.Text
	Case "1"
		ChkZeroPrice.CheckState = CheckState.Checked
	Case "2"
		ChkNegPrice.CheckState = CheckState.Checked
	Case "3"
		ChkZeroPrice.CheckState = CheckState.Checked
		ChkNegPrice.CheckState = CheckState.Checked
		
		....
		
	Case "110"
		ChkCondiment.CheckState = CheckState.Checked
		ChkWeightEntry.CheckState = CheckState.Checked
	Case "140"
		ChkWeight.CheckState = CheckState.Checked
		ChkWeightEntry.CheckState = CheckState.Checked
	Case Else
		' None of the above Case statements matched
End Select

Open in new window

0
 
HooKooDooKuCommented:
The other alternative is rather than organizing by Flag22 value is to organize by Chk object.  
For this solution, you still use Select Case, but you use Select Case as a short hand for a bunch of If conditions ORed together.

i.e. Rather than something that looks like this...

If F = "1" OR F = "3" OR F = "11" OR F = "41" OR F = "42" OR F = "101" Then
  'Do something
Endif

... it instead looks like this...

Select Case F
  Case "1", "3", "11", "41", "42", "101"
    'Do something
Endif

'ChkZeroPrice
Select Case txtFlag22.Text
  Case "1","3","11","21","41","42","101"
    ChkZeroPrice.CheckState = CheckState.Checked
End Select
 
'ChkNegPrice
Select Case txtFlag22.Text
  Case "2","3","12","22","42","102"
    ChkNegPrice.CheckState = CheckState.Checked
End Select
 
...
 
 
 
'If other values of the text file results in doing something different,
'then the code would look something like this for a single Chk
 
'ChkZeroPrice
Select Case txtFlag22.Text
  Case "1","3","11","21","41","42","101"
    ChkZeroPrice.CheckState = CheckState.Checked
    ChkZeroPrice.Enabled = TRUE
  Case "2","4","12" 
    ChkZeroPrice.Enabled = FALSE
  Case Else
    ChkZeroPrice.Visible = FALSE
End Select

Open in new window

0
All Courses

From novice to tech pro — start learning today.