最大化带有标签大小的窗体

时间:2013-01-12 08:32:30

标签: c# winforms label maximize

我制作了一个自定义MessageBox,并且在使用标签大小最大化msgBox时遇到了问题。如果我的标签太长太大,那么我希望我的自定义MessageBox增长更大或更小也取决于标签大小。我可以在Windows窗体选项中设置它还是我可以做什么?

我的代码:

      public static DialogResult Show(string Text, string Caption, string btnOk, string btnCancel)
      {

        MsgBox = new CustomMsgBox();
        MsgBox.label1.Text = Text;
        MsgBox.button1.Text = btnOk;
        MsgBox.button2.Text = btnCancel;
        MsgBox.AutoSize = true;
        result = DialogResult.No;
        MsgBox.ShowDialog();

        return result;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        result = DialogResult.Yes;
        MsgBox.Close();
    }

2 个答案:

答案 0 :(得分:0)

您可以在MsgBox_load(object sender, EventArgs e)中执行的操作是添加以下代码:

private void MsgBox_Load(object sender, EventArgs e)
{
    this.Width = label1.Width + label1.Location.X;
    //do other things you need in the load event.
}

这将根据label1的Width + X坐标位置设置MsgBox的width。由于标签可能在左侧,也可能不在左侧,这将显示完整的标签并相应地最大化MsgBox。

答案 1 :(得分:0)

试试这段代码。

设计师

  1. 拖放FlowLayoutPanel控件,根据需要调整flowlayout宽度
  2. 拖放Label控件
  3. 在.cs文件中

    例如,我在表单加载事件中绑定一些文本并调用resize事件

        private void CustMsgBox_Load(object sender, EventArgs e)
        {
            //Binding some long text to the label
            label1.Text = "ga,dsa,bdas,bcc,asbcc,asc,asbc,a c,asc,n asc,baskcas,c sam ckavcmas cmas ckasvmas cmas ckavcma cmaga,dsa,bdas,bcc,asbcc,asc,asbc,a c,asc,n asc,baskcas,c sam ckavcmas cmas ckasvmas cmas ckavcma cmaga,dsa,bdas,bcc,asbcc,asc,asbc,a c,asc,n asc,baskcas,c sam ckavcmas cmas ckasvmas cmas ckavcma cmaga,dsa,bdas,bcc,asbcc,asc,asbc,a c,asc,n asc,baskcas,c sam ckavcmas cmas ckasvmas cmas ckavcma cma";
            this.ResizeWindow(); // Calling this event will resize the label and window
        }
    

    将一些文本绑定到标签后,请调用以下方法

        private void ResizeWindow()
        {
            int iHeight = flowLayoutPanel1.Height;
            flowLayoutPanel1.Height = label1.Height;
            this.Height += (flowLayoutPanel1.Height - iHeight); //This will increase the height of the window accordingly
            this.Top=Convert.ToInt32((Screen.PrimaryScreen.WorkingArea.Height-this.Height)/2); //This will position the window center vertically
        }
    
相关问题