用户自定义控件没有提供ShowDialog方法,要想实现,必须派生自Form类,也就是说你想弹出对话框,用户自定义控件式不可以的,但是你可以新建一个Form,然后将自定义控件拖动放入Form里面,就实现了
public static class UserControlExtends
{
public static DialogResult ShowDialog(this UserControl uc)
{
var frm = new Form()
{
ClientSize = new Size(uc.Width, uc.Height),
FormBorderStyle = FormBorderStyle.FixedToolWindow,
StartPosition = FormStartPosition.CenterScreen
};
frm.Controls.Add(uc);
return frm.ShowDialog();
}
public static DialogResult ShowDialog(this UserControl uc, int height, int width)
{
var frm = new Form()
{
ClientSize = new Size(width, height),
FormBorderStyle = FormBorderStyle.FixedToolWindow,
StartPosition = FormStartPosition.CenterScreen
};
frm.Controls.Add(uc);
return frm.ShowDialog();
}
public static DialogResult ShowDialog(this UserControl uc, Form frm)
{
frm.Controls.Add(uc);
return frm.ShowDialog();
}
}