2016-12-30 12 views
0

現在、C#Windowsフォームプロジェクトを作成中です。外部関数からフォームのプロパティを変更するC#

私は3つのWindowsフォーム - FormA、FormB、FormC、およびツールと呼ばれる外部クラスファイルを持っています。

私が望むのは、呼び出されたときに各フォームのプロパティを変更できる関数を作成することです。私は、これが私の機能の議論として渡され、使われるべきだと感じています。

これは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; 
     } 
    } 

私はプロジェクトをビルドするとき、今、私は次のエラー

Error 5 'System.Windows.Forms.FormBorderStyle' is a 'type' but is used like a 'variable' C:\Users\AGDS\Dropbox\UniPi\5th\User Experience\Smart City\SmartCity\SmartCity\Tools.cs 72 13 SmartCity

Error 6 The name 'WindowState' does not exist in the current context C:\Users\AGDS\Dropbox\UniPi\5th\User Experience\Smart City\SmartCity\SmartCity\Tools.cs 73 13 SmartCity

Error 7 Keyword 'this' is not valid in a static property, static method, or static field initializer C:\Users\AGDS\Dropbox\UniPi\5th\User Experience\Smart City\SmartCity\SmartCity\Tools.cs 75 13 SmartCity

+0

あなたが見つけたエラーが登場:this.FormBorderStyle –

答えて

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

を得るまであなたはヘルパー型アプローチのような場合 - 次してみてくださいコード

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

あなたが静的方法でこのを使用することはできません。

FormBorderStyleのWindowStateあなたはそれに値を割り当てることはできません、タイプです。ジョブを完了させるには、この関数にFormsのリファレンスを渡す必要があります。エラーが変数としてFormBorderStyleを使用している、提案として、あなたはそれを使用する必要があります、ので

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; 
    } 
} 
+0

あなたは間違っている、彼は全く分離されたクラスでそれをやっています。スタティックはここでは関係ありません。 – MadOX

+0

私の答えに何が問題なのですか? – Prabu

+0

スローされたエラーをチェックします。どのように静的なmethid @ Madoxでこれを使うことができますか? – Prabu

関連する問題