Solved

MESSY CODE

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

13 Experts available now in Live!

Get 1:1 Help Now