加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用Form.ShowDialog?

发布时间:2020-12-15 06:33:52 所属栏目:百科 来源:网络整理
导读: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
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();

        }

当我点击按钮,我看到新的窗体和textBox1在新窗体,我可以输入textBox1的东西,但我没有看到任何一个OK或CANCEL按钮.我应该在新的表格设计师手册中添加它们吗?那么怎么用呢?

这是我的新表单中的代码,我想做的是在新的FormBox1中键入一些内容,并将textBox1中的文本传递给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
            {

            }
        }
    }
}

那么Form.ShowDialog的OK和CANCEL按钮在哪里?

解决方法

您将需要自己添加,您可以将按钮添加到窗体并设置其 DialogResult属性.这将返回DialogResult并关闭窗体,而无需连接任何代码.以下是使用方法返回Form2上的TextBox的值的示例(Form2上有两个按钮,其DialogResults设置为Cancel并且Ok).

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

窗体2

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读