将文本框值从FormB发送到FormA中的datagridView

本文关键字:FormA 中的 datagridView FormB 文本 | 更新日期: 2023-09-27 18:12:34

当我点击FormA中的"validate button "到datagridView时,我必须从FormB发送2个TextBoxes的值;这就是我试图编码的内容:

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,但现在我得到一个参考问题感谢您的帮助

将文本框值从FormB发送到FormA中的datagridView

不需要创建事件。您可以在希望从现有表单发送值的第二个表单中创建属性。例如,如果你有两个窗体FormAFormB,那么FormB应该包含Value1Value2这样的属性。

//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。你可以把值处理成Form Load event

//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;
}

和Assign FormAFormB.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()方法。