从外部函数c#修改表单的属性

时间:2016-12-30 13:21:26

标签: c# .net forms function reference

我目前正在开发一个C#Windows Forms项目。

我有3个Windows窗体 - FormA,FormB,FormC和一个名为Tools的外部类文件。

我想要的是创建一个函数,可以在调用时修改每个表单上的属性。我觉得 - 这应该被传递和使用,作为我职能中的论据。

这是Tools.cs代码:

    public class Tools
    {
       public static void FullScreenMode(/*Should Pass a form's instance?*/) 
       {
           FormBorderStyle = FormBorderStyle.None;
           WindowState = FormWindowState.Maximized;
           Screen screen = Screen.FromPoint(Cursor.Position);
           this.Location = screen.Bounds.Location;
       }
    }

直到现在,当我构建项目时,我收到以下错误

  

错误5'System.Windows.Forms.FormBorderStyle'是'type',但用作'变量'C:\ Users \ AGDS \ Dropbox \ UniPi \ 5th \ User Experience \ Smart City \ SmartCity \ SmartCity \ Tools.cs 72 13 SmartCity

     

错误6当前上下文中不存在名称“WindowState”C:\ Users \ AGDS \ Dropbox \ UniPi \ 5th \ User Experience \ Smart City \ SmartCity \ SmartCity \ Tools.cs 73 13 SmartCity

     

错误7关键字'this'在静态属性,静态方法或静态字段初始值设定项C:\ Users \ AGDS \ Dropbox \ UniPi \ 5th \ User Experience \ Smart City \ SmartCity \ SmartCity \ Tools.cs中无效75 13 SmartCity

3 个答案:

答案 0 :(得分:0)

          public class Tools
              {
             public static void FullScreenMode(Form fr) 
            {
                   fr.FormBorderStyle = FormBorderStyle.None;
                   fr.WindowState = FormWindowState.Maximized;
                    fr.Screen screen = Screen.FromPoint(Cursor.Position);
                    fr.Location = screen.Bounds.Location;


              }
                   }

答案 1 :(得分:-1)

如果您喜欢帮助型方法 - 请尝试下一个代码

public static class Tools
{
    public static void FullScreenMode(this Form form)
    {
        form.FormBorderStyle = FormBorderStyle.None;
        form.WindowState = FormWindowState.Maximized;
        Screen screen = Screen.FromPoint(Cursor.Position);
        form.Location = screen.Bounds.Location;
    }
}

并使用它:

  • 内部形式:
private void button1_Click(object sender, EventArgs e)
{
    this.FullScreenMode();
}
  • 外部形式(即其他形式):
// some way to access other form...
public Form OtherForm { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    OtherForm.FullScreenMode();
}

答案 2 :(得分:-2)

您无法在静态方法中使用

FormBorderStyle WindowState 是一种类型,您无法为其赋值。您应该将Forms引用传递给此函数以完成工作。

public class Tools
{
    public static void FullScreenMode(Form @this)
    {
        @this.FormBorderStyle = FormBorderStyle.None;
        @this.WindowState = FormWindowState.Maximized;
        Screen screen = Screen.FromPoint(Cursor.Position);
        @this.Location = screen.Bounds.Location;
    }
}