Daniel Lip
Daniel Lip

Reputation: 11321

How do I use Form.ShowDialog?

private void button2_Click(object sender, EventArgs e)
        {
            ChangeLink cl = new ChangeLink();
            // Show testDialog as a modal dialog and determine if DialogResult = OK.
            if (cl.ShowDialog() == DialogResult.OK)
            {
                // Read the contents of testDialog's TextBox. 
               // cl.AcceptButton.DialogResult = DialogResult.OK;
                this.label4.Text = cl.textBox1Text;
            }
            else
            {
                this.label4.Text = "Cancelled";
            }
            cl.Dispose();

        }

When I click the button, I see the new Form and the textBox1 in the new Form. And I can type in the textBox1 something, but I can't see anywhere an OK or CANCEL buttons. Should I add them manualy in the new Form designer? And how to use them then?

This is the code in my new Form what I wanted to do is to type something in the new Form textBox1 and pass the text in the textBox1 to Form1 label4.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GatherLinks
{
    public partial class ChangeLink : Form
    {
        public ChangeLink()
        {
            InitializeComponent();


        }

        public string textBox1Text
        {
            get
            {
                return textBox1Text = textBox1.Text;
            }
            set
            {
               
            }
        }
    }
}

So where are the OK and CANCEL buttons of the Form.ShowDialog?

Upvotes: 16

Views: 120674

Answers (5)

Olle Hudga
Olle Hudga

Reputation: 121

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.dialogresult?view=net-5.0

provides the best way showing how "OK" & "Cancel" buttons are linked You need to manually add 2 buttons eg., button1 & button2

    // Set the accept button of the form to button1.
      form1.AcceptButton = button1;
     // Set the cancel button of the form to button2.
       form1.CancelButton = button2;

Acceptbutton, button1 is deemed to be the OK button. CancelButton, button2 is deemed to be the Cancel button.

Frankly, none of the answers explicitly mention this. So, I had to pitch in with this answer.

Further more, you can see MSDN documentation,

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.dialogresult?view=net-5.0

Upvotes: 2

Lorenz Lo Sauer
Lorenz Lo Sauer

Reputation: 24690

Given that your only tag is C#, and you expect an OK and CANCEL button, it seems to me that you are actually looking for the MessageBox function. Creating and disposing a Form just for the sake of showing a messagebox dialog is uncessary.

if (MessageBox.Show("boxtext", "boxcaption" MessageBoxButtons.OKCancel) == DialogResult.OK) 
{
// Read the contents of testDialog's TextBox. 
// cl.AcceptButton.DialogResult = DialogResult.OK;
this.label4.Text = cl.textBox1Text;
}else
{
    this.label4.Text = "Cancelled";
}

MessageBox is a wrapper for the same-named WIN32 API Function:

int WINAPI MessageBox(
  _In_opt_  HWND hWnd,
  _In_opt_  LPCTSTR lpText,
  _In_opt_  LPCTSTR lpCaption,
  _In_      UINT uType
);

Note: If you already have a window-handle / Form make sure to pass it as the first parameter to MessageBox.

Upvotes: 6

Mark Hall
Mark Hall

Reputation: 54532

You will need to add them yourself, you can add the buttons to your Form and set their DialogResult Property. This will return the DialogResult and close the Form without you having to wire up any code. Here is an example using a Method to return the Value of The TextBox on Form2(There are two Buttons on Form2 with their DialogResults set to Cancel and Ok respectivly).

Form1

public partial class Form1 : Form
{
    Form2 frm2;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm2 = new Form2();
        DialogResult dr = frm2.ShowDialog(this);
        if (dr == DialogResult.Cancel)
        {
            frm2.Close();
        }
        else if (dr == DialogResult.OK)
        {
            textBox1.Text = frm2.getText();
            frm2.Close();
        }
    }
}

Form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string getText()
    {
        return textBox1.Text;
    }
}

Upvotes: 30

corn3lius
corn3lius

Reputation: 4985

if you create an form from the basic form class you need to define a button that returns the DialogResult in the properties of the button.

Those are most useful in the FileDialog , MessageBox etc.. where the class is a MS defined form.

Upvotes: 0

Fredou
Fredou

Reputation: 20090

what you want is the inputbox of visualbasic namespace and yes you can use it in c#

Microsoft.VisualBasic.Interaction.InputBox

Upvotes: 0

Related Questions