使用form.showdialog打开表单 [英] Open forms with form.showdialog

查看:69
本文介绍了使用form.showdialog打开表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想用这种方法打开表格





Hi there, i want to open a form with this method


private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2();

        f.ShowDialog(this);

    }





i得到此错误

错误1否方法'ShowDialog'的重载需要'1'参数



为什么?



我是什么尝试过:



你好,我想用这种方法打开表格







i get this error
Error 1 No overload for method 'ShowDialog' takes '1' arguments

why ?

What I have tried:

Hi there, i want to open a form with this method


private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2();

        f.ShowDialog(this);

    }





i得到此错误

错误1否方法'ShowDialog'的重载需要'1'参数



为什么?



i get this error
Error 1 No overload for method 'ShowDialog' takes '1' arguments

why ?

推荐答案

在Form2中创建一个您想要共享数据的属性



In Form2 Create a property in which you want to share the data as

public partial class Form2 : Form
 {
     public string DataToShare { get; set; }





您可以在创建实例时访问该属性并显示对话框。





and you can access the property while creating the instance and show the dialog.

private void button1_Click(object sender, EventArgs e)
      {
          Form2 f = new Form2();
          f.DataToShare = "Pass this data";
          f.ShowDialog();
      }


我不知道解决方案1中有什么不清楚。正如我所说,设置某些表单对象实例的属性没有任何关系使用显示 ShowDialog



但是,直接公开表单属性并不是很好,当然不能接受 public 访问;在最坏的情况下,它可能是内部。但是,这种简单的方法可行。只是一个表单实例应该可以访问另一个表单实例,它可以是另一个属性,也许是只写一个。



因为问题证明是非常的流行,我以前的答案往往不太清楚,可能不够清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



-SA
I have no idea what can be unclear in Solution 1. As I say, setting properties of some instance of form objects has nothing to do with Show or ShowDialog.

However, it's not very nice to directly expose form properties, and certainly it's not acceptable to give public access; in worst case, it could be internal. However, this simple approach would work. It's just that one form instance should have access to another form instance, which can be another property, perhaps write-only one.

As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


这篇关于使用form.showdialog打开表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆