13

デザイナーにいくつかのコントロールが追加されたWindowsフォームアプリケーションがあります。Form1.cs内からテキストボックスを有効にする何か(LIKE)を変更したい場合は、単に次を使用します。

textBox1.Enabled = true;

しかし今、私はclass1.csと呼ばれる別のクラスを持っています。

静的関数class1.csからtextBox1を有効にするにはどうすればよいですか?

注:私はこれを行うことについてまったく無知なので、コードを試しませんでした。

4

11 に答える 11

19

編集:多くの編集。

public partial class Form1 : Form
{
    // Static form. Null if no form created yet.
    private static Form1 form = null;

    private delegate void EnableDelegate(bool enable);

    public Form1()
    {
        InitializeComponent();
        form = this;
    }

    // Static method, call the non-static version if the form exist.
    public static void EnableStaticTextBox(bool enable)
    {
        if (form != null)
            form.EnableTextBox(enable);
    }

    private void EnableTextBox(bool enable)
    {
        // If this returns true, it means it was called from an external thread.
        if (InvokeRequired)
        {
            // Create a delegate of this method and let the form run it.
            this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable });
            return; // Important
        }

        // Set textBox
        textBox1.Enabled = enable;
    }
}
于 2012-10-19T22:45:16.957 に答える
15

これは単なる別の方法です:

TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
于 2016-08-11T13:44:11.590 に答える
4

Form のインスタンスをクラスに渡すことができます

 MyForm frm = new MyForm();

 MyClass c = new MyClass(frm);

その後、クラスはそのインスタンスを取得してテキストボックスにアクセスできます

 public class MyClass
 {

   public MyClass(MyForm f)
   {
      f.TextBox1.Enabled = false;
   }
 }

デザインがよく見えない

フォームでクラスを呼び出し、返された値に基づいてテキストボックスを操作することをお勧めします

//MyForm Class

 MyClass c = new MyClass();
 c.DoSomethings();
 if(c.getResult() == requiredValue)
   textBox1.enabled = true;
 else
   textBox1.enabled = false;

//MyForm Class ends here

アップデート

public class Class1
{
   public static int SomeFunction()
   {
      int result = 1;
      return result;
   }

   public static void SomeFunction(out int result)
   {
      result = 1;
   }
}

使用法

if(Class1.SomeFunction() == 1)
   textBox1.Enabled = true;
else
   textBox1.Enabled = false;

また

int result = 0;
Class1.SomeFunction(out result);

if(result == 1)
   textBox1.Enabled = true;
else
   textBox1.Enabled = false;
于 2012-10-19T22:50:12.280 に答える
3

class1 に Textbox を有効にするイベントを持たせることができます。

public class Class1
{
  public event Action<object, EventArgs> subscribe ;
  private void raiseEvent()
  {
     var handler = subscribe ;
     if(handler!=null)
     {
        handler(this,EventArgs.Empty);//Raise the enable event.
     }
  }
}

TextBox を含むクラスが何らかの形でサブスクライブできるようにします。TextBox ラッパー クラス内

 public class TextBoxWrapper
       public void EnablePropertyNotification(object sender, EventArgs args) 
       {
          TextBox1.Enabled = true ; //Enables textbox when event is raised.
       }
       public TextBoxWrapper()
       {
         class1Instance.subscribe+=EnablePropertyNotification ;
       }
于 2012-10-19T22:50:08.603 に答える
3

class1の UI コントロールを実際に変更するべきではありませんが、代わりに class1 にメソッドまたはプロパティを作成してFormテキストボックスを有効にするかどうかを指定します。

例:

// I changed the name class1 to MySettings
public class MySettings
{
    public bool ShouldTextBoxBeEnabled()
    {
        // Do some logic here.
        return true;
    }

    // More generic
    public static bool SetTextBoxState(TextBox textBox)
    {
        // Do some logic here.
        textBox.Enabled = true;
    }

    // Or static property (method if you like)
    public static StaticShouldTextBoxBeEnabled { get { return true; } }
}

次に、フォームで:

MySettings settings = new MySettings();
textBox1.Enabled = settings.ShouldTextBoxBeEnabled();

// Or static way
textBox1.Enabled = MySettings.StaticShouldTextBoxBeEnabled;

// Or this way you can send in all textboxes you want to do the logic on.
MySettings.SetTextBoxState(textBox1);
于 2012-10-19T22:50:20.563 に答える
-1

これはあなたがすべきことです:私は私のフォームクラスで以下のコードを書きました:

public static Form1 form = null;

    private delegate void SetImageDelegate(Image image);

    public Form1()
    {
        InitializeComponent();
        form = this;
    }

    public static void SetStaticImage(Image image)
    {
        if (form != null)
            form.pic1.Image = image;
    }

    private void setImage(Image img)
    {
        // If this returns true, it means it was called from an external thread.
        if (InvokeRequired)
        {
            // Create a delegate of this method and let the form run it.
            this.Invoke(new SetImageDelegate(setImage), new object[] { img });
            return; // Important
        }

        // Set textBox
        pic1.Image = img;
    }

以下のコードは別のクラスにある必要があります。

Form1 frm= Form1.form;
frm.pic1.Image = image;

private static Form1 form = null;に変更したことに注意してくださいpublic static Form1 form = null;

幸運を祈ります... Hassan Eskandariによって書かれました:)

于 2014-12-01T08:33:46.623 に答える