从其他表单获取信息 [英] Get info from another form

查看:71
本文介绍了从其他表单获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将对话框结果与不带消息框的表单一起使用

how to use dialog result with forms not with messagebox

推荐答案

尝试:
myDialogForm mdf = new myDialogForm();
if (mdf.ShowDialog() == DialogResult.OK)
   {
   ...
   }


您将必须设置Form属性以生成确定"和取消":
在您的确定"按钮上,在设计器中将"DialogResult"设置为确定"
在取消"按钮上,将其设置为取消"

在表单上,​​将接受按钮"属性设置为确定"按钮的名称,将取消按钮"属性设置为您的取消"按钮的名称.

全部完成!

我有三个以上的多项选择,我想即使在表单2关闭时也要停止在主窗体中工作,然后继续

并以主要形式使用此选择".


DialogResult本身是一个enum,您不能根据自己的选择扩展它以进行复制.但是,通常的方法是保持确定"和取消"按钮不变,并在表单中提供属性以返回用户选择.这样,您可以拥有所需的一切!考虑一下OpenFileDialog:它具有一个确定"和一个取消"按钮,但是您随后从对话框中读取了文件名.
例如:在myDialogForm中:


You will have to set the Form properties to generate the OK and Cancel:
On your OK button, set "DialogResult" to "OK" in the designer
On your Cancel button, set it to "Cancel"

On your form, set the "Accept Button" property to the name of your OK button, and the "Cancel Button" property to the name of your Cancel button.

All done!

"i have a multiple choices more than three and i want to stop working in mainform even form 2 close then continue

and use this choice in main form"


DialogResult itself is an enum and you can''t extend it to copy with your choices. However, the normal way to do this is to keep the OK and Cancel buttons as they are, and to supply properties in you form to return the user choices. This way you can have as many as you need! Think about a OpenFileDialog: It has an "OK" and a "Cancel" button, but you then read the file name from the dialog.
For example: in myDialogForm:

public string UserName 
   {
   get { return textboxUserName.text; }
   set { textboxUsername.Text = value; }
   }
public float UserAge
   {
   get { return float.Parse(textboxUserYears.Text) + float.Parse(textboxUserMonths.Text) / 12; }
   }





myDialogForm mdf = new myDialogForm();
if (mdf.ShowDialog() == DialogResult.OK)
   {
   Console.WriteLine(mdf.UserName);
   Console.WriteLine(mdf.UserAge.ToString());
   ...
   }



使用ShowDialog会导致主窗体暂停,并等到用户选择"OK"或"Cancel"后再继续-这就是您想要的!



Using ShowDialog causes the main form to pause, and wait until the user selects "OK" or "Cancel" befopre continuing - which is what you wanted!


例如.想象我们有一个用户设置"表格,该表格允许用户配置一些应用程序值.该表单具有应用"按钮和取消"按钮

E.g. imagine we have a ''User Settings'' form that allow the user to configure some application values. The form has an ''Apply'' button and a ''Cancel'' button

public class UserSettings : Form
{
    private void applyButton_Click(object sender, EventArgs e)
    {
        ApplyUserSettings();
        this.DialogResult = DialogResult.OK;
    }
    private void cancelButton_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }
}



现在,我们可以从应用程序的其他地方使用此表单,如下所示



We could now consume this form from somewhere else in our application as follows

private void showUserSettings_Click(object sender, EventArgs e)
{
    using (UserSettings settings = new UserSettings())
    {
        if (settings.ShowDialog(this) == DialogResult.OK)
        {
            // User clicked the OK button on the UserSettings form, so do some processing
        }
    }
}


Google是您的朋友.这是当您搜索"c#如何使用dialogresult"时返回的53,000个结果中的第一个:

http://support.microsoft.com/kb/816145 [
Google is your friend. Here is the first of 53,000 results returned when you search for "c# how to use dialogresult":

http://support.microsoft.com/kb/816145[^]


这篇关于从其他表单获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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