Solved

MESSY CODE

Posted on 2009-05-13
2
346 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:richard_gar
2 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 24376524
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
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 24377008
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 41
start a process from a service 3 23
Help with preventing downloading a zip file 10 37
Sending receiving text messages in vb.net 15 38
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question