关于c#:如何从其他类调用输入对话框(Gorkem Gencay)

How to call Input Dialog (Gorkem Gencay) from other class

如何使用"gorkem gencay"inputDialog从其他类调用它?编辑:插入所有代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
        }
    }
}

我正在尝试使用私有的void form1_加载(对象发送器,eventargs e),但不起作用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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方法被定义为静态的,因此在调用它时需要使用类名而不是对象名。假设ShowInputDialog是在Form1类中定义的,则应按如下方式调用它:

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

顺便说一下,这个方法被定义为private,所以您必须使它成为publicinternal

Class1定义也有错误。不能从过程上下文中调用过程代码(frm.ShowInputDialog(ref input);)。定义一个方法,并将对话框调用代码设置为以下方法:

1
2
3
4
5
6
7
8
class Class1
{
    public static void TestDialogCall()
    {
        string input ="hede";
        Form1.ShowInputDialog(ref input);
    }
}