-2

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);
    }
}
Community
  • 1
  • 1
Massimo D. N.
  • 316
  • 1
  • 6
  • 17
  • why don't you create some public property inside of Form1, also you should have a constructor that perhaps takes input as a param when creating the new instance of Form1, you will need an InitializeComponent(); inside the constructor, this should be no different than if you were to do it with windows calling frm.ShowDialoag() – MethodMan May 12 '17 at 12:21
  • I'm new to programming. Can you write some code? Thanks – Massimo D. N. May 12 '17 at 12:47
  • wow...you'e asking someone to write code.. good luck.. I would suggest a new career path since you are too lazy to use google and try something on your own.. `I am new to programming is never an excuse` – MethodMan May 12 '17 at 13:04
  • I'm trying to learn by my self and also english is not my language. Thanks anyway – Massimo D. N. May 12 '17 at 13:11
  • use google and search for `C# Basics Tutorials` – MethodMan May 12 '17 at 13:14

1 Answers1

1

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);
    }
}
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40