How To Resize Controls When Form Changes Size In C# | Windows Forms | Visual Studio 2022

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024

ความคิดเห็น • 92

  • @МаксимМутовин-ъ2г
    @МаксимМутовин-ъ2г 2 ปีที่แล้ว +70

    i find a mistake
    in method resizeControl not
    int newX = (int)(r.Width * xRatio);
    int newY = (int)(r.Height * yRatio);
    true its:
    int newX = (int)(r.Location.X * xRatio);
    int newY = (int)(r.Location.Y * yRatio);

  • @alphasauroxviix4235
    @alphasauroxviix4235 2 ปีที่แล้ว +12

    high quality content, ty! Your channel deserves to be so much bigger, keep it up.

    • @ShaunHalverson
      @ShaunHalverson  2 ปีที่แล้ว +1

      Thank you! Comments like this are the best :)

  • @hallythejester1234
    @hallythejester1234 7 หลายเดือนก่อน +1

    Would you mind remaking this tutorial with a bunch of controls of many different types. I tried it your way, precisely, but regardless of what I set the anchor property of a control to inside visual studio, the second the form got smaller than it’s initial size all the controls decided to throw a party on top of one another in the upper left hand corner. Even if the distance between the bottom of the form and the controls that were set to anchor at the bottom prior to resizing vs the distance between the bottom of the form and the bottom of the control set to anchor at the bottom were not even *remotely* similar. You’d think the y coordinate of the controls set to anchor at the bottom would be less after resizing than they were prior, right? Nope. Bigger.
    The (maybe) good news is I have managed (I think) to eliminate the need all the rectangle variables in the form load and the partial class except for the form size.
    I just declared/initialize the form size variable in the class and in the load method the way you said. Then I eliminated the resize method and put all the dirty work in the in form resize method. Like this:
    Form1_Resize(//the normal stuff here)
    {
    Int xRatio= this.width/ o.f.s.Width;
    //tried it as a float but still don’t work your way)
    int yRatio = this. Height/ o.f.s.height;
    Int initialX;
    Int initialY;
    int initialWidth;
    Int initialHeight;
    foreach(Control con in panel1.controls)
    {
    Con.width = initialWidth;
    Con.height= initialHeight;
    Con.Location.x = initialX;
    Con.Location.y = initialY;
    Con.Location = new Point(initialX*xRatio, initialY * yRatio);
    Con.Size = new Size(initialWidth * xRatio, initialHeight* yRatio);
    }
    }
    PS: before anyone tells me that I can’t blame him for my own code not working….I did it exactly the way he said in the video originally. The behavior of the controls were exactly the same as it is rn, at least when it comes to shrinking the form. At least this way I don’t have like a gazillion extra rectangle variables.

  • @cappatalistpigeon3246
    @cappatalistpigeon3246 9 หลายเดือนก่อน +1

    Thank you so much tremendous video I spent so long looking how to do this so strange windows forms don’t have it built in already

  • @pierrejoubert616
    @pierrejoubert616 11 หลายเดือนก่อน

    This is extremely helpful man. Thank you for the thorough walkthrough!!

  • @pgbollwerk
    @pgbollwerk ปีที่แล้ว

    Thanks for this.
    I'm complete new to C#.
    I've dipped my toe into C# because I want to create a "launcher" window on startup where a user picks one of 2 applications. (for a home arcade setup, which won't have a keyboard or mouse - only a joystick/buttons which pass key presses)
    I've managed to create a static sized form window that works with both buttons.
    But now I want to see if I can make the window maximized and resize the buttons properly, regardless of the actual screen resolution.
    In my scenario, the window will not be resizable, but simply always maximized.
    Hopefully your tips here will guide me down that road.

  • @soggychip3784
    @soggychip3784 2 ปีที่แล้ว

    thank you I've spent ages trying to find this. definetly subbing

  • @HelianGamePiano
    @HelianGamePiano 2 ปีที่แล้ว +5

    Thank you for making this creative idea ! Not to say anything bad about this idea but I guess it is not really optimal and usable in reality ? Because I see people only make windowed mode or full-screen mode for apps. I also think this code really consumes memories as it literally render everything visible before our eyes in real time, and let's say we don't have just 1 button but thousand of actions at the moment.
    How do you think about my opinion ? Still, thanks for sharing this :D

    • @WDFQofficial
      @WDFQofficial 2 ปีที่แล้ว +1

      It'd be generous if you'd share any ways that are easier than this and more usable/optimal?

    • @gamingforfun1295
      @gamingforfun1295 ปีที่แล้ว

      I think that since the action is only called on form resize that it actually isn't too intensive? It only runs while the form is being actively resized

    • @ncueBenson
      @ncueBenson ปีที่แล้ว

      @@gamingforfun1295 resize will be called multiple times while you modify windows size .

    • @ncueBenson
      @ncueBenson ปีที่แล้ว

      Maybe we can use ResizeEnd to avoid it occur.

  • @apkirkpatrick8180
    @apkirkpatrick8180 ปีที่แล้ว

    Good job Shaun!

  • @buraktuzun9690
    @buraktuzun9690 ปีที่แล้ว

    Thank you so much Shaun. This video is very helpful.

  • @468theking
    @468theking 2 ปีที่แล้ว +5

    it doesn't work!!
    control vanish if form resized!!

  • @MineBlox_Gaming
    @MineBlox_Gaming ปีที่แล้ว

    Thanks it also works on visual studio 2019 cause 2019 and 2022 don't have much difference also good content keep it up!
    But how do I use it for multiple buttons and object? Or even for all object in the form?

  • @ElectroGamesYT
    @ElectroGamesYT 2 ปีที่แล้ว +1

    Does this work with TextBoxes and MenuStrips? It does not seem to be working. The text box just disappears and nothing happens to the MenuStrip.

    • @ShaunHalverson
      @ShaunHalverson  2 ปีที่แล้ว

      Have you tried setting autosize to false for those?

    • @ElectroGamesYT
      @ElectroGamesYT 2 ปีที่แล้ว

      @@ShaunHalverson They are false on default.

    • @ShaunHalverson
      @ShaunHalverson  2 ปีที่แล้ว

      ​@@ElectroGamesYT Alright let's start with the textbox first. Is it set to multiline?

  • @seanyalcin8781
    @seanyalcin8781 2 ปีที่แล้ว +3

    High quality tutorial really liked it. My button only becomes visible after I resize the form and not when it initially loads why is this?

    • @ShaunHalverson
      @ShaunHalverson  2 ปีที่แล้ว +1

      Thanks for the kind words and glad you enjoyed it, that is a weird bug I don’t think I have heard of before. Feel free to add me on discord and I can take another look if you’d like

    • @seanyalcin8781
      @seanyalcin8781 2 ปีที่แล้ว +1

      @@ShaunHalverson Where can I find your discord?

    • @hiluu
      @hiluu ปีที่แล้ว

      i have the same problem too. Did you fix it?

  • @jasonport6691
    @jasonport6691 ปีที่แล้ว

    Great video, I am creating a borderless form application which has forms loaded within a panel on the mainform, I wish to drag and drop between 2 different screen resolutions, however the control is not resizing with the form when dragged from the smaller on to the larger one. If i close the childform on the larger screen and reopen it, it is correct size.

  • @martintrost815
    @martintrost815 ปีที่แล้ว

    The reason why we use rectangle is because all controls are some sort of rectangle! Nice.

  • @zyz780
    @zyz780 2 ปีที่แล้ว +4

    Very helpful video, but I got a problem, my controls will jump to another location when I first resize my form.

    • @pierresdecor365
      @pierresdecor365 2 ปีที่แล้ว +6

      you neew to make the following corrections: int newX = (int)(r.Location.X * xRatio);
      int newY = (int)(r.Location.Y* yRatio); instead of int newX = (int)(r.Width * xRatio);
      int newY = (int)(r.Height* yRatio);

    • @mysticdrk
      @mysticdrk 2 ปีที่แล้ว +1

      @@pierresdecor365 thank you, had the same problem :)

  • @MrAnime-404
    @MrAnime-404 10 หลายเดือนก่อน +1

    Is it possible to select all elements in my application without having to write a 'buttonOriginalRectangle' for each element? I have 7 buttons, 1 picture box, a menu strip, and a label, and I want them to dynamically resize perfectly.

    • @adibobish9766
      @adibobish9766 9 หลายเดือนก่อน

      You could also use a panel and set every control's parent as the panel

  • @frantisekvanecek1693
    @frantisekvanecek1693 ปีที่แล้ว

    Thanks for video. But I have problem and I cannot find solution. It works on computer where is visual studio. When I copy release build to the other computer where is not VS, the resize function causes the controls to not be displayed at all. If I comment resize function controls are showen. Do you have some idea?

  • @hintsoftware
    @hintsoftware 2 หลายเดือนก่อน

    Font size should also be resized accordingly ?

  • @blackflame_
    @blackflame_ 2 ปีที่แล้ว +5

    Now the form controls aren't even visible on the screen lol

  • @AravindKumar-lj7kx
    @AravindKumar-lj7kx ปีที่แล้ว

    Great explanation...could you create a video to increase the text size which is inside the button.

    • @andrei_1603
      @andrei_1603 ปีที่แล้ว

      change the font size of the button

  • @greese
    @greese ปีที่แล้ว

    How would I use this method to resize the text on the button or a textBox?

  • @EGLOVEzicgo
    @EGLOVEzicgo 2 ปีที่แล้ว

    Is picturebox possible? There are no autosize and mode items in the properties window

  • @sadravin1
    @sadravin1 8 หลายเดือนก่อน +1

    Unfortunately, your solution is also not true scaling. It is a good dirty method to fix a one off issue, but lacks text scaling and alignment anchoring. Thanks anyways.

    • @VulcanRavenPL
      @VulcanRavenPL 5 หลายเดือนก่อน

      so write solution in your comment would like to see how you would do it

  • @ntrainride
    @ntrainride 2 ปีที่แล้ว

    okay. but o. s.s that allow real time proportional sizing of all objects in a window. all objects shrink. everything in the window remains. just that reducing the size of a window makes the complete image shrink. guess it's hard to articulate.

  • @lapisbrainprint9607
    @lapisbrainprint9607 2 ปีที่แล้ว

    Thanks for the video. I am so close to actually making it work for me and yet so far. For some odd reason my "button1" cannot be called in my "resizeControl" function because it is of class Forms::button(Forms.button for C#) instead of Forms::Control (Forms.Control for C#) and it messes up my project. I would just copy ur entire project but I am trying to implement it on c++...

    • @PiterparkerYT
      @PiterparkerYT 9 หลายเดือนก่อน

      i ran into same issue. to fix it just change Control c to Control^ c
      also make sure to change c. to c-> after that

  • @Явижусвоимиглазами
    @Явижусвоимиглазами 2 ปีที่แล้ว +1

    Thanks very helpful video

  • @adnangharbi8982
    @adnangharbi8982 2 ปีที่แล้ว +1

    Greate Work thank you so much

  • @christianduran-bm4mv
    @christianduran-bm4mv ปีที่แล้ว +1

    Only works if you have one control. If you add controls they all go to the same position or stack on top of each other. This is more confusing than leaving it and just losing it when resizing.

    • @buraktuzun9690
      @buraktuzun9690 ปีที่แล้ว +3

      Change newX and newY lines like this
      int newX = (int)(r.Location.X* xRatio);
      int newY = (int)(r.Location.Y* yRatio);
      you should use r.Location, not r.Width or r.Height

    • @amirzamani6239
      @amirzamani6239 ปีที่แล้ว

      shit bro thanks that helped me
      @@buraktuzun9690

  • @TheRealLeoblox
    @TheRealLeoblox 2 ปีที่แล้ว

    Could you please show me how to do multiple buttons instead of just one???

  • @martintrost815
    @martintrost815 ปีที่แล้ว +1

    I think line 34 and 35 maybe need to be edited, don't they? They should be changed to int newX = (int)(r.X * xRatio); int newY = (int)(r.Y * yRatio);

    • @danielmisra2912
      @danielmisra2912 2 หลายเดือนก่อน

      THANK YOU SO MUCH , SAVED ME!!!!

  • @longca2228
    @longca2228 ปีที่แล้ว

    I like the video at all

  • @m.s7839
    @m.s7839 ปีที่แล้ว

    No tutorial seems to help me with what i need. I want to make the form scale according to the resolution like you resize a image in photoshop/paint.

  • @bistbist1615
    @bistbist1615 ปีที่แล้ว

    I am using .net frame work in windows forms app but my form won't load

  • @aksvinss
    @aksvinss 2 ปีที่แล้ว

    Can I get help to do this in c++?

  • @leventegaal9531
    @leventegaal9531 2 ปีที่แล้ว +1

    It works in uwp?

    • @ShaunHalverson
      @ShaunHalverson  2 ปีที่แล้ว

      Not totally sure, you would have to try that out. I know for a fact it works in C# though :D

  • @film30dnimroku
    @film30dnimroku 2 ปีที่แล้ว +1

    Thank you

  • @WDFQofficial
    @WDFQofficial 2 ปีที่แล้ว

    I tried this with my picture box. I'm sure I've got everything correctly but every time I resize my window it just dissapears. Any ideas why?
    Edit: got it all sorted. Found solution in comments.

  • @hxndrik
    @hxndrik 6 หลายเดือนก่อน

    there must be a better way of doing this, no way i need to add code for each form? what about just using tablelayoutpanels?

  • @rokiedebian2692
    @rokiedebian2692 ปีที่แล้ว

    thats a lot of coding for one button but what if you have a lot of components in the UI

  • @魏勤龙
    @魏勤龙 2 ปีที่แล้ว

    EDITED? orignal code has bug ^ ^; newx newy is not correct

  • @enderspider5001
    @enderspider5001 9 หลายเดือนก่อน

    OMG THIS GUY SPEAKS NORMAL ENGLISH HOLY SHIT

  • @NoMan-991
    @NoMan-991 7 หลายเดือนก่อน

    Thank you from Russian

  • @Alexandru-IoanVacaru-r8f
    @Alexandru-IoanVacaru-r8f ปีที่แล้ว

    int newX = (int)(r.X * xRatio);
    int newY = (int)(r.Y * yRatio);
    In the video you don't update the locations correctly. You used the width and height.

  • @amiryazdanparast9827
    @amiryazdanparast9827 2 หลายเดือนก่อน

    🙏🌹

  • @sjt62349
    @sjt62349 10 หลายเดือนก่อน

    Nice job, I get the concept, BUT no one has a form with one button. So this video does not really help much. ---sjt

  • @NightTimeStories-y7h
    @NightTimeStories-y7h ปีที่แล้ว

    It should be
    int newX = (int)(r.X * xRatio);
    int newY = (int)(r.Y * yRatio);

  • @IamJasiek
    @IamJasiek 2 ปีที่แล้ว

    not working

  • @PASHKULI
    @PASHKULI ปีที่แล้ว

    text stays the same size… half-job

  • @m_r-ock6508
    @m_r-ock6508 ปีที่แล้ว

    autocomplete carried the whole video lol

  • @MobiusNull
    @MobiusNull 2 ปีที่แล้ว

    speak less buddy