2017-05-12 7 views
-2

'Gorkem Gencay' InputDialogを別のクラスから呼び出す方法を教えてください。 編集:他のクラスの入力ダイアログ(Gorkem Gencay)の呼び出し方法

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace InputDialog 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public static DialogResult ShowInputDialog(ref string input) 
     { 
      System.Drawing.Size size = new System.Drawing.Size(200, 70); 
      Form inputBox = new Form(); 

      inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 
      inputBox.ClientSize = size; 
      inputBox.Text = "Name"; 

      System.Windows.Forms.TextBox textBox = new TextBox(); 
      textBox.Size = new System.Drawing.Size(size.Width - 10, 23); 
      textBox.Location = new System.Drawing.Point(5, 5); 
      textBox.Text = input; 
      inputBox.StartPosition = FormStartPosition.CenterParent; 
      inputBox.Controls.Add(textBox); 

      Button okButton = new Button(); 
      okButton.DialogResult = System.Windows.Forms.DialogResult.OK; 
      okButton.Name = "okButton"; 
      okButton.Size = new System.Drawing.Size(75, 23); 
      okButton.Text = "&OK"; 
      okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39); 
      inputBox.Controls.Add(okButton); 

      Button cancelButton = new Button(); 
      cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; 
      cancelButton.Name = "cancelButton"; 
      cancelButton.Size = new System.Drawing.Size(75, 23); 
      cancelButton.Text = "&Cancel"; 
      cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39); 
      inputBox.Controls.Add(cancelButton); 

      inputBox.AcceptButton = okButton; 
      inputBox.CancelButton = cancelButton; 

      DialogResult result = inputBox.ShowDialog(); 
      input = textBox.Text; 
      return result; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      string input = "hede"; 
      ShowInputDialog(ref input); 
     } 
    } 
} 

すべてのコードを挿入した私はプライベート無効Form1_Load(オブジェクト送信者、EventArgsの電子)を使用して、次のisteadをしようとしているが、動作しません:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace InputDialog 
{ 
    class Class1 
    { 
     Form1 frm = new Form1(); 
     string input = "hede"; 
     frm.ShowInputDialog(ref input); 
    } 
} 
+0

は、なぜあなたはまた、あなたが新しいインスタンスを作成するときに、おそらくのparamとして入力を取るコンストラクタを持つ必要があり、Form1の内部でいくつかのパブリックプロパティを作成しないでくださいForm1の場合は、InitializeComponent()が必要です。コンストラクタ内では、frm.ShowDialoag()を呼び出すウィンドウでこれを行う場合と変わらないはずです。 – MethodMan

+0

プログラミングが初めてです。コードを書くことができますか?ありがとう –

+0

うわー...コードを書こうと頼んでいる人..幸運..私は新しいキャリアパスを提案している。あなたがGoogleを使って何かを試してみるのは怠惰だから。言い訳にならないよ。 – MethodMan

答えて

1

そのShowInputDialogメソッドは静的として定義されているため、クラス名を使用する必要があり、呼び出すときはオブジェクト名ではありません。 ShowInputDialogForm1クラスで定義されている次のように、あなたはそれを呼び出す必要がありますと仮定:

string input = "hede"; 
Form1.ShowInputDialog(ref input); 

ちなみに、その方法はprivateとして定義されているので、あなたはそれpublicまたはinternalようにする必要があります。

Class1の定義にも誤りがあります。手続き型コンテキストから手続き型コード(frm.ShowInputDialog(ref input);)を呼び出すことはできません。メソッドを定義して、ダイアログ呼び出しコードを入れて、この方法:

class Class1 
{ 
    public static void TestDialogCall() 
    { 
     string input = "hede"; 
     Form1.ShowInputDialog(ref input); 
    } 
} 
+0

このようにして、Form1.ShowInputDialog(ref input);赤色の下線が引かれています。エラー:「ShowInputDialog」という名前が実際のコンテキストに存在しません –

+1

あなたのケースでは 'ShowInputDialog'が定義されていますか? –

+0

Form1の内部:public static DialogResult ShowInputDialog(参照文字列入力) –

関連する問題