Winforms Visual Studio 2012 Windows窗体应用程序

Winforms Visual Studio 2012 Windows窗体应用程序,winforms,visual-studio,Winforms,Visual Studio,我正在Visual Studio 2012上开发Windows窗体应用程序。我有两张表格 将项目添加到数据库1 将项目添加到DB2 这两个表单都调用了第三个表单SUBMIT。现在,根据调用此表单的位置,它必须将信息提交到不同的数据库。SUBMIT表单中的所有内容都完全相同,只是数据插入到了不同的数据库中 有没有办法找出表单的调用来源?这是一种新的表单应用程序 谢谢如果您使用ShowDialog()方法打开提交表单,您将能够通过所有者属性确定打开提交表单的表单。例如: public partial

我正在Visual Studio 2012上开发Windows窗体应用程序。我有两张表格

将项目添加到数据库1

将项目添加到DB2

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

有没有办法找出表单的调用来源?这是一种新的表单应用程序


谢谢

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

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;
    }
}
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;
    }
}
或者,您可以在提交表单上公开可以从父表单填充的公共属性。例如:

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;
    }
}
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,我是否只需执行
messagebox.show(this.owner.ToString())
?它不断抛出一个关于对象引用如何未设置为对象实例的错误。首先,确保使用的是MessageBox.Show()而不是MessageBox.Show()(注意大写)。其次,如果要使用Owner属性,请确保使用ShowDialog()加载表单。哦,是的,输入错误就在这里。Intellisense让我知道了。再次谢谢你。你帮了大忙。