【问题标题】:C# - Form2 value to Form1C# - Form2 到 Form1 的值
【发布时间】:2016-06-14 12:42:20
【问题描述】:

我在将 form2(citacao) 中输入的值传递给 form1(principal) 时遇到问题。

Principal.cs (form1)

richEditControl1.Document.AppendText(citacao.valor_edit[0]);

Citacao.cs (form2)

public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
    valor_edit[0] = memoEdit1.Text;
    valor_edit[1] = comboBox1.SelectedItem.ToString();
    valor_edit[2] = textEdit1.Text;

} 

但是当我点击按钮时没有任何反应,值没有插入到我喜欢的richedit中。

我已经在表单上有这个(将 DataGrid 传递给 ComboBox)

Form1(主体)

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    citacao cita = new citacao(this);
    cita.Show();
}

form2(citação)

public citacao(principal gridForm)
{
    InitializeComponent();
    frm1 = gridForm;
}

// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
    comboBox1.Items.Clear();
    foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
    {
        comboBox1.Items.Add(row.Cells[0].Value.ToString());
    }
    comboBox1.SelectedIndex = 0;
}

【问题讨论】:

  • 这一行:richEditControl1.Document.AppendText(citacao.valor_edit[0]); 何时执行?值什么时候必须出现在 Form1 中?
  • 将在 form2 关闭时运行。当尝试编译Error 3 Argument 1: cannot convert from 'string[]' to 'string' C:\Users\RCA\Desktop\ABTN_Format\ABNTFacil\ABNTFacil\principal.cs 93 50 ABNTFacilError 1 An object reference is required for the non-static field, method, or property 'ABNTFacil.citacao.valor_edit' C:\Users\RCA\Desktop\ABTN_Format\ABNTFacil\ABNTFacil\principal.cs 93 50 ABNTFacil。我也试过这个,但不工作citacao cita = new citacao(this); richEditControl1.Document.AppendText(cita.valor_edit);
  • 能否多发些代码,断线的代码之间很难跳转,想弄清楚程序流程
  • 所以您在表格 1 中有表格 2 的结束活动,对吗?
  • 你的错误来自richEditControl1.Document.AppendText(citacao.valor_edit[0]); 这一行,因为你使用了类名(citacao),所以编译器会寻找一个名为valor_edit的静态变量,但它不是静态的

标签: c# winforms


【解决方案1】:

让我们看看我是否理解你的情况:)

将表格1中的变量声明为类变量

private citacao cita;

然后在按钮按下事件中初始化它

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    cita = new citacao(this);
    // subscribe to the closing event
    cita.FormClosing += form_FormClosing;
    cita.Show();
}

// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
    // BUT! you have to use the variable name!
    richEditControl1.Document.AppendText(cita.valor_edit[0]);    
}

编辑:

查看完整代码后确定: 请删除按钮3!以及整个代码:

private void button3_Click(object sender, EventArgs e)
{
    cita = new citacao(this);
    richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}

函数AppendText 可能需要一个string 作为参数,然后你给出整个数组! 如果您订阅 Form1 / principal 中的关闭事件并实现 事件,一旦表格 2 从屏幕上消失,您的数据将自动传输:)

【讨论】:

  • 错误 3 非静态字段、方法或属性 'ABNTFacil.citacao.valor_edit' C:\Users\RCA\Desktop\ABTN_Format\ABNTFacil\ABNTFacil\principal 需要对象引用。 cs 105 50 ABNTFacil 错误 2 参数 1:无法从 'string[]' 转换为 'string' C:\Users\RCA\Desktop\ABTN_Format\ABNTFacil\ABNTFacil\principal.cs 105 50 ABNTFacil 之前的相同错误 - 在这里你可以看到所有代码....citação.cs (pastebin.com/TfTMtaZc), principal.cs (pastebin.com/cuQWiRTN)
  • @CarlosIyu 我编辑了我的答案。感谢您提供对整个代码的访问权限,信息使解决问题变得更加容易:)
  • @CarlosIyu 很高兴我能帮上忙。事件确实值得深入研究。 :)
猜你喜欢
  • 1970-01-01
  • 2018-11-04
  • 2012-08-10
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多