Results 1 to 10 of 10

Thread: RectangleShape problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    RectangleShape problem

    Hi all,

    Im new to programming - so Ive a steep hill to climb :^)

    I have a form where Ive added a rectangle using the rectangleshape from the toolbox.

    When I start debugging the rectangle is bigger than the form at runtime?

    If I use another item from the toolbox and use the same size as the rectangle - it fits

    Probably a simple fix but its now doing my head in.

    See attached

    Regards

    Mick
    Attached Images Attached Images

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: RectangleShape problem

    Lots of people won't download attachments. It's generally best to post the code wrapped in CODE tags (the # button), though, in this case, it may be that there are images that may seem relevant.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: RectangleShape problem

    Quote Originally Posted by Shaggy Hiker View Post
    Lots of people won't download attachments. It's generally best to post the code wrapped in CODE tags (the # button), though, in this case, it may be that there are images that may seem relevant.
    there's no code just pictures :^)

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: RectangleShape problem

    I would recommend just adding code to draw a rectangle to the Form's paint event rather than depend on the shape controls, which were added kind of as an afterthought and aren't that reliable and if someone is running the community version of VS2015, I think it is a separate download to get the shape control libraries. I think even for the free version of VS 2010 it wasn't installed as part of the basic install.
    Code:
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(Pens.Black, 20, 20, 800, 500)
        End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: RectangleShape problem

    Quote Originally Posted by passel View Post
    I would recommend just adding code to draw a rectangle to the Form's paint event rather than depend on the shape controls, which were added kind of as an afterthought and aren't that reliable and if someone is running the community version of VS2015, I think it is a separate download to get the shape control libraries. I think even for the free version of VS 2010 it wasn't installed as part of the basic install.
    Code:
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(Pens.Black, 20, 20, 800, 500)
        End Sub
    Even with code I get the same problem :^(

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: RectangleShape problem

    I assume you changed the numbers to be appropriate to the size rectangle you wanted, and the last two parameters are width and height of the rectangle, not the lower right coordinates of the rectangle.

    There could be some run-time dpi based scaling going on. If you make the form smaller so that you can run it and see the form and compare it to the size of the form in the IDE (Integrated Design Environment), does the form and controls on the form appear the same as they are in the IDE or are they a different size.

    Also, try this test which works fine on the machine I'm currently on.
    I'll add a button to the form at a certain spot and size, then change the rectangle code to draw the rectangle around it with a 10 pixel border.
    On this machine I see a flat button with a blue border surrounded by a black rectangle evenly space around the perimeter.
    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim b As New Button
            b.SetBounds(100, 100, 100, 50)
            Me.Controls.Add(b)
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(Pens.Black, 90, 90, 120, 70)
        End Sub
    Name:  RectangleAroundButton.png
Views: 339
Size:  422 Bytes
    That indicates to me that the button size and the drawing coordinates are using the same scale on this machine.
    Last edited by passel; Sep 22nd, 2017 at 11:18 AM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: RectangleShape problem

    Quote Originally Posted by passel View Post
    I assume you changed the numbers to be appropriate to the size rectangle you wanted, and the last two parameters are width and height of the rectangle, not the lower right coordinates of the rectangle.

    There could be some run-time dpi based scaling going on. If you make the form smaller so that you can run it and see the form and compare it to the size of the form in the IDE (Integrated Design Environment), does the form and controls on the form appear the same as they are in the IDE or are they a different size.

    Also, try this test which works fine on the machine I'm currently on.
    I'll add a button to the form at a certain spot and size, then change the rectangle code to draw the rectangle around it with a 10 pixel border.
    On this machine I see a flat button with a blue border surrounded by a black rectangle evenly space around the perimeter.
    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim b As New Button
            b.SetBounds(100, 100, 100, 50)
            Me.Controls.Add(b)
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(Pens.Black, 90, 90, 120, 70)
        End Sub
    Name:  RectangleAroundButton.png
Views: 339
Size:  422 Bytes
    That indicates to me that the button size and the drawing coordinates are using the same scale on this machine.
    aha - you have pointed me in the right direction when you say dpi scaling. :^)

    When I checked the display settings there was a scaling of 125%

    I removed the scaling and put it back to 100% and rebooted the PC

    and viola it worked - thanks very much - hours down the suwannee river with that one :^)

    so in this case how could you rectify it with code if there was a scaling?

    The mointor is 4k - and the text is a wee bit small :^) hence the scaling

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: RectangleShape problem

    Fortunately I haven't had to deal with it yet so unfortunately I don't have remedies.
    The one thing I do hear repeatedly is to learn to use WPF projects instead of WinForm projects since WPF is dpi aware and you won't have those issues.
    If you're new to programming, then perhaps it is best to start doing WPF projects and go up that learning curve, rather than going up the winform curve and be biased toward it and have another transition to wpf later.
    By the way, I'm well entrenched in the winform way and have very little wpf exposure so wouldn't be of any help in that arena.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: RectangleShape problem

    Quote Originally Posted by passel View Post
    Fortunately I haven't had to deal with it yet so unfortunately I don't have remedies.
    The one thing I do hear repeatedly is to learn to use WPF projects instead of WinForm projects since WPF is dpi aware and you won't have those issues.
    If you're new to programming, then perhaps it is best to start doing WPF projects and go up that learning curve, rather than going up the winform curve and be biased toward it and have another transition to wpf later.
    By the way, I'm well entrenched in the winform way and have very little wpf exposure so wouldn't be of any help in that arena.
    Thanks for your help - its appreciated

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: RectangleShape problem

    In terms of DPI scaling it is very, very hard to rectify the problems with Windows Forms and this is something people are increasingly encountering as monitors with higher resolutions are more common.

    Windows Forms is a framework developed in the early 2000s based on GDI+, which is itself based on GDI which was developed in the 1980s for Windows 3.1. For almost all of its 30-year history, there were only 3 common monitor sizes and one "resolution", which really means "dot pitch". Microsoft has tried since 2003 to introduce an automatic scaling algorithm that handles different display sizes, but so far they've only managed to make it worse.

    So in Windows Forms today, there are at least 2 different scaling algorithms that might be used based on your .NET version. THEN there are at least 4 different scaling algorithms Windows might apply IN ADDITION TO the .NET scaling algorithm, mostly based on which version of Windows you use. Getting it "right" if you want to support many resolutions generally means throwing away the designer and hand-writing your layouts using a device-independent unit instead of pixels.

    Or you can forget Windows Forms exists and learn WPF instead. It was designed from 2006-2010 when MS saw this problem coming and it already uses device-independent units. Lots of people are fond of saying it's "harder to learn". It's more that it's "harder to explain, and unfamiliar". I've been doing projects for iOS, Android, and the web lately, and I guarantee you WPF is still an "easy" framework to learn at a basic level.

    Personally, I prefer writing my GUI apps with NodeJS these days. HTML UI works on every platform. WPF works on Windows and nothing else.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width