Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Happy New Year. Hope everyone enjoyed the holidays !

I installed an application I developed in Visual Studio C# on some new laptops with wider higher resolution screens and sent them into the field. Everything works fine with the following exception. A key component to this application is the entry of time spent on customer sites. It is entered in 15 minute increments. To aid the employee in entering the time I use radio controls and display the implied time in a graphic bar below the controls. Here is a poor representation to the screen (since I cant paste an image here). Image the 'X' are a graphic colored rectangle. The following is a correct representation of 3:00 to 4:15.

1   2   3   4   5   6   7
o   o   o   o   o   o   o
        XXXXXX


When the same data is entered on one of the new laptops the rectangles no longer align with the correct radio controls.

1   2   3   4   5   6   7
o   o   o   o   o   o   o
   XXXXXX



The time is split into 24 hours in 15 minute increments. The color of the rectangle is controlled by an entry in an array initialized earlier in the code. The Radio controls are located 20 units apart according the x,y coordinates in the radio control location property. So I make the width of the rectangle = 5 (20/4 fifteen minute segments) and calculate the left position.

So the question is: Why does this work on the old laptops, works on my development PC regardless of what resolution I set the screen, but does NOT align properly on the new laptops?

C#
Rectangle r = new Rectangle(2, 2, 20, 10); //left,top,width,heigth
int width = 5;
int top = 2;
int height = 10;
Bitmap bmp = new Bitmap(hourlyPictureBox.Width, hourlyPictureBox.Height);
Graphics g = Graphics.FromImage(bmp);
g.PageUnit = GraphicsUnit.Pixel;
for (i = 0; i <= 100; i++)
   {
    r = new Rectangle(4 + (i * width), top, width, height); //left,top,width,heigth
   if (slot[i] == 0)
    {
          g.FillRectangle(Brushes.White, r);
     }
   if (slot[i] == 1)
    {
          g.FillRectangle(Brushes.red, r);
     }
      ........so on and so forth for all the colors
   }

hourlyPictureBox.Image = bmp;
Hopefully a simple question for someone who knows what they are doing. :=)
Thank you in advance for your help in this matter.
Rob

[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 12-Jan-15 8:33am
v2

You are doing wrong thing in principle: using manual positioning. You should use a very different approach: using System.Windows.Forms.Control.Dock property with different values except Dock.None:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle%28v=vs.110%29.aspx[^].

Also, it comes with the property System.Windows.Forms.Control.Padding:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.padding%28v=vs.110%29.aspx[^].

In this approach, all the Location property of all controls become irrelevant, everything is aligned automatically. You will need a lot more instances of Panel, including those use just for spacing. In your case, you will need to put all radio buttons in several vertical panels. You will have two kinds of rectangular areas: under the radio buttons and between them. For both cases, you will need a separate panel, one with the radio button, one for the space in between. In both kinds of panel, you will have sub-panels colored as your rectangles. Here is how:
   ┌───┬───┬───┬───┬───┐
...│ 2 │   │ 3 │   │ 4 │...
   │ o │   │ o │   │ o │
   ├───┼───┼───┼───┼───┤
   └───┴───┴───┴───┴───┘
Rectangular shapes denotes Panel instances, arranged, for example, using DockStyle.Left. Bottom panels can be colored by using BackColor property. (Note that there are not visible borders on the panels, the picture above is just for explanation.) They are child panels placed on the bigger panels. So the on the picture, 10 Panel objects are shown, one more could be their parent panel.

And so on…

—SA
 
Share this answer
 
v6
Comments
CHill60 26-Jan-15 12:53pm    
5 from me - definitely a nicer solution than the one from the OP. I've said as much in my "solution" which is only a response to the OP's solution
Sergey Alexandrovich Kryukov 26-Jan-15 16:00pm    
Thank you. It's apparent to me that absolute "manual" positioning greatly compromise compatibility, and even worse maintainability.
—SA
Could well be that the Lappie has different display, font size, zoom factor, etc. - these are all adjustable to cope with the smaller display dimensions.

How I would do it:
Take the Left property of the first button, and subtract it from the Left plus Width properties of the last. Divide by two. This gives you distance to the centre from the left edge of the first button.
1   2   3   4   5   6   7
o   o   o   o   o   o   o
            ^

Reduce that by half of the number of blocks you want to paint * (block width + gap width))
Add that to the Left property of the first button, and you have the Left property for the first block.
1   2   3   4   5   6   7
o   o   o   o   o   o   o
      X X X X X X X
      ^
Because this is all in the same coordinate system, it should be device independent.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Jan-15 18:14pm    
This is still "manual positioning", using "Location". There can be big problems with it. First of all, label size. The accuracy of knowing its width is very low. The picture will jiggle horizontally. Therefore, you should better use panels and centering.

Please see my answer.

—SA
Well.....after a couple of hours of frustration I found out that using floating # does not help. Everything had to work out to an integer so things would line up on each 15 minute increment.

I also never figured out how to loop thru and reference the radio buttons, so a bunch of hard coded crap fixed my problem. Code is ugly, but works.

Thank you for heading me in the right direction to know what to look at. Devil is in the details.





C#
if ((radioButton54.Left) - (radioButton30.Left) > 600)
            {
                width = 7;
                offset = 12;
                radioButton2.Left = radioButton1.Left + (width * 4);
                radioButton3.Left = radioButton2.Left + (width * 4);
                radioButton4.Left = radioButton3.Left + (width * 4);
                radioButton5.Left = radioButton4.Left + (width * 4);
                radioButton6.Left = radioButton5.Left + (width * 4);
                radioButton7.Left = radioButton6.Left + (width * 4);
                radioButton8.Left = radioButton7.Left + (width * 4);
                radioButton9.Left = radioButton8.Left + (width * 4);
                radioButton10.Left = radioButton9.Left + (width * 4);
                radioButton11.Left = radioButton10.Left + (width * 4);
                radioButton12.Left = radioButton11.Left + (width * 4);
                radioButton13.Left = radioButton12.Left + (width * 4);
                radioButton14.Left = radioButton13.Left + (width * 4);
                radioButton15.Left = radioButton14.Left + (width * 4);
                radioButton16.Left = radioButton15.Left + (width * 4);
                radioButton17.Left = radioButton16.Left + (width * 4);
                radioButton18.Left = radioButton17.Left + (width * 4);
                radioButton19.Left = radioButton18.Left + (width * 4);
                radioButton20.Left = radioButton19.Left + (width * 4);
                radioButton21.Left = radioButton20.Left + (width * 4);
                radioButton22.Left = radioButton21.Left + (width * 4);
                radioButton23.Left = radioButton22.Left + (width * 4);
                radioButton24.Left = radioButton23.Left + (width * 4);
                radioButton25.Left = radioButton24.Left + (width * 4);
                st1.Left = radioButton1.Left - 4;
                st2.Left = radioButton2.Left - 4;
                st3.Left = radioButton3.Left - 4;
                st4.Left = radioButton4.Left - 4;
                st5.Left = radioButton5.Left - 4;
                st6.Left = radioButton6.Left - 4;
                st7.Left = radioButton7.Left - 4;
                st8.Left = radioButton8.Left - 4;
                st9.Left = radioButton9.Left - 4;
                st10.Left = radioButton10.Left - 4;
                st11.Left = radioButton11.Left - 4;
                st12.Left = radioButton12.Left - 4;
                st13.Left = radioButton13.Left - 4;
                st14.Left = radioButton14.Left - 4;
                st15.Left = radioButton15.Left - 4;
                st16.Left = radioButton16.Left - 4;
                st17.Left = radioButton17.Left - 4;
                st18.Left = radioButton18.Left - 4;
                st19.Left = radioButton19.Left - 4;
                st20.Left = radioButton20.Left - 4;
                st21.Left = radioButton21.Left - 4;
                st22.Left = radioButton22.Left - 4;
                st23.Left = radioButton23.Left - 4;
                st24.Left = radioButton24.Left - 4;
                pm13.Left = radioButton13.Left - 4;
                pm14.Left = radioButton14.Left - 4;
                pm15.Left = radioButton15.Left - 4;
                pm16.Left = radioButton16.Left - 4;
                pm17.Left = radioButton17.Left - 4;
                pm18.Left = radioButton18.Left - 4;
                pm19.Left = radioButton19.Left - 4;
                pm20.Left = radioButton20.Left - 4;
                pm21.Left = radioButton21.Left - 4;
                pm22.Left = radioButton22.Left - 4;
                pm23.Left = radioButton23.Left - 4;
                pm24.Left = radioButton24.Left - 4;
                
                radioButton31.Left = radioButton30.Left + (width * 4);
                radioButton32.Left = radioButton31.Left + (width * 4);
                radioButton33.Left = radioButton32.Left + (width * 4);
                radioButton34.Left = radioButton33.Left + (width * 4);
                radioButton35.Left = radioButton34.Left + (width * 4);
                radioButton36.Left = radioButton35.Left + (width * 4);
                radioButton37.Left = radioButton36.Left + (width * 4);
                radioButton38.Left = radioButton37.Left + (width * 4);
                radioButton39.Left = radioButton38.Left + (width * 4);
                radioButton40.Left = radioButton39.Left + (width * 4);
                radioButton41.Left = radioButton40.Left + (width * 4);
                radioButton42.Left = radioButton41.Left + (width * 4);
                radioButton43.Left = radioButton42.Left + (width * 4);
                radioButton44.Left = radioButton43.Left + (width * 4);
                radioButton45.Left = radioButton44.Left + (width * 4);
                radioButton46.Left = radioButton45.Left + (width * 4);
                radioButton47.Left = radioButton46.Left + (width * 4);
                radioButton48.Left = radioButton47.Left + (width * 4);
                radioButton49.Left = radioButton48.Left + (width * 4);
                radioButton50.Left = radioButton49.Left + (width * 4);
                radioButton51.Left = radioButton50.Left + (width * 4);
                radioButton52.Left = radioButton51.Left + (width * 4);
                radioButton53.Left = radioButton52.Left + (width * 4);
                radioButton54.Left = radioButton53.Left + (width * 4);
                et30.Left = radioButton30.Left-4;
                et31.Left = radioButton31.Left - 4;
                et32.Left = radioButton32.Left - 4;
                et33.Left = radioButton33.Left - 4;
                et34.Left = radioButton34.Left - 4;
                et35.Left = radioButton35.Left - 4;
                et36.Left = radioButton36.Left - 4;
                et37.Left = radioButton37.Left - 4;
                et38.Left = radioButton38.Left - 4;
                et39.Left = radioButton39.Left - 4;
                et40.Left = radioButton40.Left - 4;
                et41.Left = radioButton41.Left - 4;
                et42.Left = radioButton42.Left - 4;
                et43.Left = radioButton43.Left - 4;
                et44.Left = radioButton44.Left - 4;
                et45.Left = radioButton45.Left - 4;
                et46.Left = radioButton46.Left - 4;
                et47.Left = radioButton47.Left - 4;
                et48.Left = radioButton48.Left - 4;
                et49.Left = radioButton49.Left - 4;
                et50.Left = radioButton50.Left - 4;
                et51.Left = radioButton51.Left - 4;
                et52.Left = radioButton52.Left - 4;
                et53.Left = radioButton53.Left - 4;
                et54.Left = radioButton54.Left - 4;
            }


            int top = 2;
            int height = 10;
            Bitmap bmp = new Bitmap(hourlyPictureBox.Width, hourlyPictureBox.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.PageUnit = GraphicsUnit.Pixel;//??

            for (i = 0; i <= 100; i++)
            {

                int w = width;
                int z = offset  + (i * width);
                //r = new Rectangle(4 + (i * width)), top, width, height); //left,top,width,heigth
                r = new Rectangle(z, top, w, height); //left,top,width,heigth
                if (slot[i] == 0)
                {
                    g.FillRectangle(Brushes.White, r);
                }
  ......so on and so forth
            }
            hourlyPictureBox.Image = bmp;
            g.Dispose();
 
Share this answer
 
v2
I still think you should have pursued the solution offered by Sergey but you could tidy up your code a bit by iterating through the radiobox controls.

Here is one way of doing it (also ugly but better than what you have at the moment)
C#
var currentPoint = this.radioButton1.Left;
var offset = this.radioButton1.Height + 30;
for (var i = 2; i < 24; i++)
{
    var rbThis = (RadioButton)this.Controls.Find("RadioButton" + i.ToString(), true)[0];
    rbThis.Top = currentPoint + offset;
    currentPoint = rbThis.Top;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900