如何使用'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);
}
}
你爲什麼不創建一些公共財產Form1的裏面,你也應該有一個構造函數創建新實例時,也許需要輸入一個PARAM Form1,你需要一個InitializeComponent();在構造函數裏面,這應該與如果你使用調用frm.ShowDialoag()的窗口來做同樣的事情。 – MethodMan
我是編程新手。你能寫一些代碼嗎?謝謝 –
哇...你要求別人寫代碼..祝你好運..我會建議一個新的職業路徑,因爲你懶得使用谷歌,並嘗試自己的東西..「我是新編程是從來沒有藉口' – MethodMan