Skip to content

WinForms

This page is about the WinFormsHelpers class available in PeyrSharp.UiHelpers. This page is about all of its methods.

Compatibility

WinFormsHelpers are part of the PeyrSharp.UiHelpers module, which is compatible with all of these frameworks and platforms:

Package/PlatformWindowsmacOSLinux + others
UiHelpers
Framework.NET 6.NET 7.NET 8
UiHelpers

Methods

CenterControl(control, form)

Definition

Centers horizontally and vertically a Control on a Form.

Arguments

TypeNameMeaning
ControlcontrolThe control to center.
FormformThe form where the control needs to be centered.

Usage

c#
using PeyrSharp.UiHelpers;
using System;
using System.Windows.Forms;

public partial class MyForm : Form
{
    private void HandleButton(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        WinFormsHelpers.CenterControl(button, this);
    }
}

CenterControl(control, form, controlAlignment)

Definition

Centers a Control on a Form, with a specific ControlAlignment.

Arguments

TypeNameMeaning
ControlcontrolThe control to center.
FormformThe form where the control needs to be centered.
ControlAlignmentcontrolAlignmentThe alignment of the control.

Usage

c#
using PeyrSharp.Enums;
using PeyrSharp.UiHelpers;
using System;
using System.Windows.Forms;

public partial class MyForm : Form
{
    private void HandleButton(object sender, EventArgs e)
    {
        Button button = (Button)sender;

        // Center the button vertically
        WinFormsHelpers.CenterControl(button, this, ControlAlignment.Vertical);
    }
}

CenterForm(form)

Definition

Centers horizontally and vertically a Form on the screen.

Arguments

TypeNameMeaning
FormformThe form to center.

Usage

c#
using PeyrSharp.UiHelpers;
using System;
using System.Windows.Forms;

public partial class MyForm : Form
{
    private void HandleButton(object sender, EventArgs e)
    {
        // Put the current form in the middle of the screen
        WinFormsHelpers.CenterForm(this);
    }
}

Released under the MIT License.