AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Swift responsive layout4/8/2023 ![]() Also to remember, this is one time operation only - at the loading of the form. However, with today's faster processors that would still be aired in the blink of an eye. Loading delay might be experienced if the form has thousands of controls on it. Points of InterestĪs mentioned earlier, there are other approaches like resorting to WPF, using anchoring/docking, etc. Also, there might be need to introduce more overloaded methods for different control types. ![]() Similar code changes might apply according to need and the nature of controls. This article is meant to introduce a technique, not to be the ultimate tool for everything developer needs to calibrate according to the need. Hence running the code as is would cause in runtime exceptions if such cases are not handled explicitly. E.g., a picturebox doesn't have the font-size property. A practical form would contain other controls which might not have all these properties. However that is not the case for real scenario. If not, some calibration or a little design-time change should be fine.Īlso, this is a generic approach for a simple form which assumes that all the controls of the form have these properties - width, height, left, top and font-size. ![]() Some claibration factors might be required to adjust a little of what we obtain from the technique (like we did for vertical-centre positioning).Īlso, it is suggested that the developer should see the outlook of the form at different resolutions to confirm that all the controls are visible and positioned correctly on the screen as intended. In effect, the form would look the same at different resolutions by contracting/expanding and re-positioning controls to an optimum level. The following is a snap taken at 1360x768: The following snap is taken at 1920x1080 resolution: The snaps below are taken at three different resolutions. ResponsiveObj.GetMetrics(( int)), FontStyle.Regular) Ĭtl.Width = ResponsiveObj.GetMetrics(Ctl.Width, " Width") Ĭtl.Height = ResponsiveObj.GetMetrics(Ctl.Height, " Height") Ĭtl.Top = ResponsiveObj.GetMetrics(Ctl.Top, " Top") Ĭtl.Left = ResponsiveObj.GetMetrics(Ctl.Left, " Left") įor demonstration purposes, following is a very simple form that contains a data grid, a label, a text box and a button. foreach (Control Ctl in this.Controls)Ĭtl.Font = new Font(FontFamily.GenericSansSerif, Top = Screen.GetBounds( this).Height / 2 - Height / 2 - 30 // 30 is a calibration factor. Left = Screen.GetBounds( this).Width / 2 - Width / 2 // Form centering. Height = ResponsiveObj.GetMetrics(Height, " Height") Width = ResponsiveObj.GetMetrics(Width, " Width") // Form width and height set up. The purposes of the members are self-explanatory by the names.Ĭopy Code private void ResponsiveForm_Load( object sender, EventArgs e) There is a class Responsive.cs which has 5 member variables as below. Using the Code The Responsive Class - Responsive.cs Then all the controls of the form are passed to this class object for scaling and resizing. It obtains this factor by dividing the current resolution by the design-time resolution. Now whenever the application is run, it obtains a multiplication factor which is actually a scaling factor. It has two hard-coded constants that preserve the design-time screen resolution. So, I thought it might benefit others just to have an alternate way of applying responsiveness to WinForm applications. Hence, I made changes in the code to make it responsive. I felt the need to make it responsive to reach out to people who might have different resolutions. But when I tried to play it in a laptop, I found that the board fell outside the screen. I designed it in a machine that had resolution of 1920x1080. I designed a game which is available here. While there are suggestions on using WPF, or using docking and anchoring of controls, using panels, etc., this article provides a different way for applying responsiveness to a WinForm application. For WinForms, we need to explicitly take care of resizing and repositioning controls based on resolution. By responsiveness, we do hereby mean usability in different screen resolutions. ![]() It is not so straightforward to create a responsive WinForms application.
0 Comments
Read More
Leave a Reply. |