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

how can i use 'Gorkem Gencay' InputDialog calling it from another Class? Edit: Inserted all the code

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

I'm trying the following istead of using the private void Form1_Load(object sender, EventArgs e) , but don't work:

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

That ShowInputDialog method is defined as static so you need to use class name and not an object name when calling it. Assuming ShowInputDialog is defined in the Form1 class, you should invoke it as follows:

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

By the way, that method is defined as private so you have to make it public or internal .

The Class1 definition also has an error. You can't call procedural code ( frm.ShowInputDialog(ref input); ) out of procedural context. Define a method and put your dialog invocation code it this method:

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

上一篇: C#和本地数据库(访问)

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