【问题标题】:Visual Studio 2012 Windows Form applicationVisual Studio 2012 Windows 窗体应用程序
【发布时间】:2017-02-07 21:39:59
【问题描述】:

我正在 Visual Studio 2012 上开发 Windows 窗体应用程序。我有 2 个窗体。

Add_Item_to_DB1
Add_Item_to_DB2
这两种形式都称为第三种形式SUBMIT。现在,根据调用此表单的位置,它必须将信息提交到不同的数据库。 SUBMIT 表单中的其他所有内容都完全相同,只是将数据插入到不同的数据库中。

有没有办法找出从哪里调用表单?对表单应用程序有点陌生。

谢谢

【问题讨论】:

    标签: winforms visual-studio


    【解决方案1】:

    如果您使用ShowDialog() 方法打开提交表单,您将能够通过Owner 属性确定打开提交表单的表单。例如:

    public partial class Add_Owner_To_Db_1 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            var submitForm = new SUBMIT();
    
            submitForm.ShowDialog(this);
        }
    }
    
    public partial class SUBMIT : Form
    {
        private void SUBMIT_Load(object sender, EventArgs e)
        {
            //label1.Text will equal "Add_Owner_To_Db_1"
            label1.Text = this.Owner.Text;
        }
    }
    

    或者,您可以在 SUBMIT 表单上公开一个可以从父表单填充的公共属性。例如:

    public partial class Add_Owner_To_Db_1 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            var submitForm = new SUBMIT();
    
            submitForm.ParentName = "Add_Owner_To_Db_1";
    
            submitForm.Show();
        }
    }
    
    public partial class SUBMIT : Form
    {
        public string ParentName { get; set; }
    
        private void SUBMIT_Load(object sender, EventArgs e)
        {
            //label1.Text will equal "Add_Owner_To_Db_1"
            label1.Text = ParentName;
        }
    }
    

    HTH

    【讨论】:

    • 谢谢。这正是我想知道的。
    • 如果我想和所有者一起显示一个消息框,我只需要messageBox.show(this.Owner.ToString())吗?它不断抛出一个关于对象引用未设置为对象实例的错误。
    • 首先,确保您使用的是 MessageBox.Show() 而不是 messageBox.show()(注意大写)。其次,如果您想使用 Owner 属性,请确保您使用 ShowDialog() 加载表单。
    • 哦,是的,错字就在这里。 Intellisense 让我了解了这一点。再次感谢你。你很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多