I have a class derived from a TableLayoutPanel and if I have more than 3 rows or columns I don't show them directly, I add(I let the object add) scrollbars so I can reach the other cells.
I have code that works perfectly,but...

public void CalculateSize()
        {
            const int cMaxCol = 3;
            const int cMaxRow = 3;
            const int cGap = 9;

            int ColScr = 16;    //size of scrollbar
            int RowScr = 16;    //size of scrollbar
            int CC = 0;
            int RC = 0;

            if (ColumnCount > cMaxCol || RowCount > cMaxRow)
            {
                this.AutoScroll = true;
                if (ColumnCount > cMaxCol)
                {
                    CC = cMaxCol;
                    RC = this.RowCount;
                    ColScr = 0;
                }
                else
                {
                    CC = this.ColumnCount;
                    RC = cMaxRow;
                    RowScr = 0;
                }
            }
            else
            {
                this.AutoScroll = false;
                CC = this.ColumnCount;
                RC = this.RowCount;
            }
            this.Size = new Size((cCellWidth + cGap) * CC + ColScr, (cCellHeigth + cGap) * RC + RowScr);  
        }

Question : Why do I have to use if (ColumnCount > cMaxCol)twice? Can this be done more elegantly?
Googled for it but all I got was or-operator explanations ad nauseam.
Anyone any ideas?

Recommended Answers

All 7 Replies

Hello, ddanbe.
I was playing with your code ... I'm not sure, that this is what are you looking for:

this.AutoScroll = true;
if (ColumnCount > cMaxCol)
    {      
      CC = cMaxCol;
      RC = this.RowCount;
      ColScr = 0;
    }
    else if (RowCount > cMaxRow)
    {
      CC = this.ColumnCount;
      RC = cMaxRow;
      RowScr = 0;

    }
    else
    {
      this.AutoScroll = false;
      CC = this.ColumnCount;
      RC = this.RowCount;
    }

Danny,

You know I'm new and you're way beyond me, however; I don't see how you could change it other than to use 'less than' along with 'and' for your first statement or to use case statements . . .

your nested if just determines which or statement was true basically

(please correct me if I'm wrong so I can learn better)

hmm, didn't see the first reply post - must refresh qucker.

can you please explain for me - does the last else statement always ring true if the they are both negative?

Thank you Antenka, you've been a great help!
I have it all worked out now.
The only thing I forgot was if both row and col are greater then 3! So here is my final code(thanks to you)

public void CalculateSize()
        {
            const int cMaxCol = 3;
            const int cMaxRow = 3;
            const int cGap = 9;

            int ColScr = 16;    //size of scrollbar
            int RowScr = 16;    //size of scrollbar
            
            int CC = this.ColumnCount;
            int RC = this.RowCount;
            
            this.AutoScroll = true;
            if (ColumnCount > cMaxCol && RowCount > cMaxRow)
            {
                CC = cMaxCol;
                RC = cMaxRow;
            }
            else if (ColumnCount > cMaxCol)
            {
                CC = cMaxCol;
                RC = this.RowCount;
                ColScr = 0;
            }
            else if (RowCount > cMaxRow)
            {
                CC = this.ColumnCount;
                RC = cMaxRow;
                RowScr = 0;
            }
            else
            {
                this.AutoScroll = false;
            }

            this.Size = new Size((cCellWidth + cGap) * CC + ColScr, (cCellHeigth + cGap) * RC + RowScr);  
        }

can you please explain for me - does the last else statement always ring true if the they are both negative?

you will get in the else part if both ColumnCount and RowCount are less than 3. I don't need scrollbars then.

public void CalculateSize()
        {
            const int cMaxCol = 3;
            const int cMaxRow = 3;
            const int cGap = 9;

            bool highColCount = ColumnCount > cMaxCol;
            bool highRowCount = RowCount > cMaxRow;
            int CC = highColCount ? cMaxCol : ColumnCount; // or Math.Min
            int RC = highRowCount ? cMaxRow : RowCount;

            int ColScr = highColCount && !highRowCount ? 0 : 16;
            int RowScr = highRowCount && !highColCount ? 0 : 16;
            this.AutoScroll = highRowCount || highColCount;

            this.Size = new Size((cCellWidth + cGap) * CC + ColScr, (cCellHeigth + cGap) * RC + RowScr);  
        }
commented: mmm ... beautifully :) +1
commented: Respect! +4
commented: Very^10 great! +6

:) Good luck.

Marvelous! Rashakil Fol you are really extraordinary.
The reason I sent in my first code was I felt it was clumsy to say the least. Would in the end have come up with some spagetti that would work, but I would not like. Antenka gave some usefull hints and I got it working, but your code is so much cleaner and concise.
Changed lines 12 and 13 of your code into
int ColScr = highRowCount ? 16 : 0;
int RowScr = highColCount ? 16 : 0;

have to add 16 to the rows if the cols exeed 3, the same for the cols. The names of the vars ColScr and RowScr are badly chosen!
Thanks very much for making me see things really clear now.

The ? notation is a great way of tidying things up when you're setting variables dependant on a question. Even for simple things such as

if (balance <0) { color = red; } else {  color = black; }

looks much simpler as

color = (balance<0)?red:black;

Especially if you have a lot of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.