将FormBox值从FormB发送到FormA中的datagridView

时间:2014-07-11 13:23:45

标签: c# winforms datagridview textbox

当我将“验证按钮”clic到FormA中的datagridView时,我必须从FormB发送2个TextBox的值;这是我尝试编码的内容:

FormB:

namespace RibbonDemo.Fichier
{
    public partial class NvFamillImmo : Form
    {
        public event BetweenFormEventHandler BetweenForm;
        SqlDataAdapter dr;
        DataSet ds = new DataSet();
        string req;

        public NvFamillImmo()
        {
            InitializeComponent();
            affich();
        }

        private void button2_Click(object sender, EventArgs e)  //the validate buton
        {

            if (BetweenForm != null)
                BetweenForm(textBox1.Text, textBox2.Text);

        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.amortissementFiscalTableAdapter.FillBy(this.mainDataSet.amortissementFiscal);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }
    }
}

这是FormA:

namespace RibbonDemo.Fichier
{
    public delegate void BetweenFormEventHandler(string txtbox1value, string txtbox2value);
    public partial class FammileImm : Form
    {

        private NvFamillImmo nvFamillImmo;
        public FammileImm()
        {
            InitializeComponent();

        }


        private void button2_Click(object sender, EventArgs e) 
        {

            NvFamillImmo frm2 = new NvFamillImmo();
            frm2.BetweenForm += frm2_BetweenForm;
            frm2.ShowDialog();
        }


        void frm2_BetweenForm(string txtbox1value, string txtbox2value)
        {

        //dataGridView1.Refresh();

        String str1 = nvFamillImmo.textBox1.Text.ToString();
        this.dataGridView1.Rows[0].Cells[0].Value = str1;

        }
    }
}
编辑:我填写方法frm2_BetwwenForm,但现在我在参考中遇到了问题 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

无需为此创建活动。您可以在第二种形式中创建要从现有表单发送值的属性。例如,如果您有两种形式 FormA FormB ,则 FormB 应包含Value1Value2等属性。< / p>

//FormB
public class FormB :Form
{
    public string Value1{get; set;}
    public string Value2{get; set;}
}

现在,您可以从 FormA 中为两个属性指定值。

//FormA
public void button1_click(object sender, EventArgs e)
{
    FormB myForm = new FormB();
    myForm.Value1 = textBox1.Text;
    myForm.Value2 = textBox1.Text;
    myForm.Show();
}

然后,您可以将两个文本框值都放入 FormB 。您可以将值处理为表单加载事件

//FormB
public void FormB_Load(object sender, EventArgs e)
{
    string fromTextBox1 = this.Value1;
    string formTextBox2 = this.Value2;
}

如果 FormB 已加载并希望从 FormA 发送值,则创建方法UpdateValues()并修改属性以调用该方法。

//FormB
string _value1 = string.Empty;
public string Value1
{
    get { return _value1; }
    set {
        _value1 = value;
        UpdateValues();
    }
}
string _value2 = string.Empty;
public string Value1
{
    get { return _value2; }
    set {
        _value2 = value;
        UpdateValues();
    }
}
private void UpdateValues()
{
    string fromTextBox1 = this.Value1;
    string fromTextBox2 = this.Value2;
}

并从 FormA 中分配FormB.Value1FormB.Value2属性中的值。

//FormA
FormB myForm = new FormB();
public void button1_click(object sender, EventArgs e)
{        
    if (myForm != null && myForm.IsDisposed == false)
    {
        myForm.Value1 = textBox1.Text;
        myForm.Value2 = textBox1.Text;        
    }
}

当从 FormA 更新值时,将调用 UpdateValues()方法。