如何从另一个窗体调整窗体的大小

时间:2014-09-24 15:28:10

标签: c# winforms visual-studio-2013

我有两个窗体。第一个(marquee)是默认的,在运行解决方案时运行,另一个以这种方式调用:

config configForm = new config();
configForm.Show();

我在configForm上有一个名为txtWid的文本框组件,另一个名为txtHeight。我也有一个按钮申请。在marquee形式中,我得到了一个函数:

public void ApplySettings()
{
    int width = Convert.ToInt32(configForm.txtWidth.Text);
    int height = Convert.ToInt32(configForm.txtHeight.Text);

    if (width > 0 && height > 0) { this.Size = new Size(width, height); }

}

我不知道如何从configForm调用该方法。如果我创建一个新的选取框表单并使用marqueeNew.ApplySettings(),则宽度结果为0并显示错误。

如何将configForm中的属性更改为marquee表单?我可以用相反的方式做到这一点,但这对我没有帮助!

3 个答案:

答案 0 :(得分:2)

将此方法与class属性一起放入新的get; set;

类似的东西:

public class MyClassName
{
    public int Height {get; set;} 
    public int Width {get; set;} 
}

在form1.load事件开始后,使用MyClassName.Height = this.Height;

设置这些值

然后,在第二种形式的加载事件中,设置值。

this.Height = MyClassName.Height

否则,手动设置边框设置为无(因此无法由用户调整大小)

答案 1 :(得分:1)

最简单的方法是使用Width和Height参数在选取框窗体上创建PUBLIC方法。这样,你可以在没有控制引用的情况下传递它们,或者在路上获得额外的属性。

*更新 - 您可以通过在子表单上添加公共属性,在子表单上创建对主表单的引用。这样,您可以避免两种形式的所有者/父母关系产生任何副作用。

例如,在你的configForm中:( MarqueeForm是你的表格类的类型)

public MarqueeForm {get;设置;}

在configForm.Show();之前,添加: configForm.MarqueeForm = this;

在你的configForm中,你应该能够引用属性MarqueeForm.ApplySettings(X,Y);

我没有完整的代码可用,因此您可能会遇到一些数据类型问题,但这应该指向正确的方向。

 public void ApplySettings(int Height, int Width)
 {
     if (Width > 0 && Height > 0) { this.Size = new Size(Width, Height); }
 }

 myForm.ApplySettings(100,200);

答案 2 :(得分:0)

public static class Forms
{
    public Marquee marqueeForm;
}

然后,当您创建新表单时,您可以执行以下操作:

...
Forms.marqueeForm = new Marquee(...);
...

然后,从您的配置中,您可以执行以下操作

Forms.marqueeForm.ApplySettings(this.txtWidth.Text, this.txtHeight.Text);
单击“应用”按钮后

。或者,更直接地,点击"应用"在配置表单上可以直接修改marqueeForm的维度。只需将ApplySettings()方法更改为ApplySettings(string widthStr, string heightStr)或某种类型。