Solved

MESSY CODE

Posted on 2009-05-13
2
345 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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now