2017-05-12 33 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); 
     } 
    } 
} 

我想下面istead使用私人無效Form1_Load的(對象發件人,EventArgs的),但不工作:

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

你爲什麼不創建一些公共財產Form1的裏面,你也應該有一個構造函數創建新實例時,也許需要輸入一個PARAM Form1,你需要一個InitializeComponent();在構造函數裏面,這應該與如果你使用調用frm.ShowDialoag()的窗口來做同樣的事情。 – MethodMan

+0

我是編程新手。你能寫一些代碼嗎?謝謝 –

+0

哇...你要求別人寫代碼..祝你好運..我會建議一個新的職業路徑,因爲你懶得使用谷歌,並嘗試自己的東西..「我是新編程是從來沒有藉口' – MethodMan

回答

1

ShowInputDialog方法被定義爲靜態,因此在調用它時需要使用類名而不是對象名。假設ShowInputDialogForm1類中定義的,你應該按照以下方式調用它:

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

順便說一句,被定義的方法private,所以你必須使它publicinternal

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(ref string input) –