Back to Devexpress

BaseButton.CalcBestFit(Graphics) Method

windowsforms-devexpress-dot-xtraeditors-dot-basebutton-dot-calcbestfit-x28-system-dot-drawing-dot-graphics-x29.md

latest3.7 KB
Original Source

BaseButton.CalcBestFit(Graphics) Method

Calculates the button’s size needed to fit its content.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public virtual Size CalcBestFit(
    Graphics g
)
vb
Public Overridable Function CalcBestFit(
    g As Graphics
) As Size

Parameters

NameTypeDescription
gGraphics

A System.Drawing.Graphics object used to paint.

|

Returns

TypeDescription
Size

A System.Drawing.Size object representing the button’s size that will best fit its content.

|

Remarks

This method returns the minimum button size required to fit the button’s content including the offset from each button’s border.

Basically, the CalcBestFit method is used internally by editors to specify the size of buttons they display. You can also use this method when creating buttons at runtime to guarantee that the buttons’ contents are completely visible.

Example

The following code creates a SimpleButton at a specific location and subscribes to the button’s Click event. The BaseButton.CalcBestFit method is used to calculate the button size that fits the button’s contents.

csharp
using DevExpress.XtraEditors;
// ...
CreateSimpleButton(30, 30);
// ...
private void CreateSimpleButton(int left, int top) {
    SimpleButton simpleButton = new SimpleButton();
    Controls.Add(simpleButton);
    simpleButton.Text = "Show Settings Page";
    simpleButton.ImageOptions.ImageList = imageCollection1;
    simpleButton.ImageOptions.ImageIndex = 0;
    simpleButton.Padding = new Padding(10);
    using(var graphics = simpleButton.CreateGraphics())
        simpleButton.Size = simpleButton.CalcBestFit(graphics);
    simpleButton.Location = new Point(left, top);
    simpleButton.Click += SimpleButton_Click;
}

private void SimpleButton_Click(object sender, EventArgs e) {
    // ...
}
vb
Imports DevExpress.XtraEditors
' ...
CreateSimpleButton(30, 30)
' ...
Private Sub CreateSimpleButton(ByVal left As Integer, ByVal top As Integer)
    Dim simpleButton As New SimpleButton()
    Controls.Add(simpleButton)
    simpleButton.Text = "Show Settings Page"
    simpleButton.ImageOptions.ImageList = ImageCollection1
    simpleButton.ImageOptions.ImageIndex = 0
    simpleButton.Padding = New Padding(10)
    Using graphics = simpleButton.CreateGraphics()
        simpleButton.Size = simpleButton.CalcBestFit(graphics)
    End Using
    simpleButton.Location = New Point(left, top)
    AddHandler simpleButton.Click, AddressOf SimpleButton_Click
End Sub

Private Sub SimpleButton_Click()
    '...
End Sub

See Also

Text

ImageOptions

BaseButton Class

BaseButton Members

DevExpress.XtraEditors Namespace