如何从其他课程调用输入对话框(Gorkem Gencay)

我如何使用'Gorkem Gencay'InputDialog从另一个类中调用它? 编辑:插入所有的代码

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 e) ,但不工作的以下方法:

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);
    }
}

ShowInputDialog方法被定义为静态的,因此您在调用它时需要使用类名称而不是对象名称。 假设ShowInputDialogForm1类中定义,您应该按如下方式调用它:

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

顺便说一下,该方法被定义为private因此您必须将其publicinternal public

Class1定义也有错误。 您不能从程序上下文中调用过程代码( frm.ShowInputDialog(ref input); )。 定义一个方法并将你的对话框调用代码放入这个方法中:

class Class1
{
    public static void TestDialogCall()
    {
        string input = "hede";
        Form1.ShowInputDialog(ref input);
    }
}
链接地址: http://www.djcxy.com/p/71171.html

上一篇: How to call Input Dialog (Gorkem Gencay) from other class

下一篇: Display Error Message when dataGridView Row match textbox