Skip to main content
A newer version of this page is available. .

BaseButton.CalcBestFit(Graphics) Method

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

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v18.2.dll

Declaration

public virtual Size CalcBestFit(
    Graphics g
)

Parameters

Name Type Description
g Graphics

A System.Drawing.Graphics object used to paint.

Returns

Type Description
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 sample code declares a CreateSimpleButton method that creates and initializes a new SimpleButton control. The method has two parameters specifying the coordinates of the button’s top left corner. The BaseButton.CalcBestFit method is used to calculate the size of the button’s contents (an image and text) in order to set a proper button size.

The result of the sample code execution is displayed below.

SimpleButton - CalcBestFit

using DevExpress.XtraEditors;
// ...
private void CreateSimpleButton(int left, int top){
   // Creating and initializing a new SimpleButton control
   SimpleButton simpleButton = new SimpleButton();
   Controls.Add(simpleButton);
   simpleButton.Text = "Show Settings Page";
   simpleButton.ImageList = imageList1;
   simpleButton.ImageIndex = 0;
   simpleButton.Size = simpleButton.CalcBestFit(simpleButton.CreateGraphics());
   simpleButton.Location = new Point(left, top);
}
// Creating SimpleButton control at the specific location
CreateSimpleButton(30, 30);
See Also