C#application.openform错误 [英] C# application.openform error

查看:189
本文介绍了C#application.openform错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我是C#的新手,我创建了一个测试Windows窗体程序。

我有2个表格,我需要将文本框的值从Form2传递给Form1 DataGridView。所有表格都是开放的(正在运行)。我使用Application.Openforms遇到错误。



请参阅下面的错误消息和我的代码。 (对不起我的语法)。



错误消息(Form1 f1 = Application.OpenForms [Form1];)

========= ================================================== ========

无法隐式转换类型''System.Windows.Forms.Form'''''gridviewsample1.Form1''。

显式转换是否存在(你错过演员吗?)



FORM2代码

============== ================================================== ====

使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;



命名空间gridviewsample1

{

公共部分类Form2:表格

{

public Form2()

{

InitializeComponent();

}

private void btnOK_Click(object sender,EventArgs e)

{

Form1 f1 = Application.OpenForms [ Form1中];



foreach(f1.dataGridView1.SelectedRows中的DataGridView行)

{



row.Cells [1] .Value = this.txtstring1.Text;

row.Cells [0] .Value = this.txtstring2.Text;

}

}

}

}

Hi Experts,

Im a newbie in C#, i have created a test windows form program.
I have 2 Forms, i need to pass the value of textbox from Form2 to Form1 DataGridView. All form are open (running). I encountered error using Application.Openforms.

Please see the error message and my code below. (sorry for my grammar).

Error Message on (Form1 f1 = Application.OpenForms["Form1"];)
===================================================================
Cannot Implicitly Convert Type ''System.Windows.Forms.Form'' To ''gridviewsample1.Form1''.
An explicit conversion exists (are you missing a cast?)

FORM2 code
====================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace gridviewsample1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
Form1 f1 = Application.OpenForms["Form1"];

foreach (DataGridView row in f1.dataGridView1.SelectedRows)
{

row.Cells[1].Value = this.txtstring1.Text;
row.Cells[0].Value = this.txtstring2.Text;
}
}
}
}

推荐答案





试试这个:

Hi,

Try this:
Form1 f1 = Application.OpenForms["Form1"] as Form1;



希望这会有所帮助。


Hope this helps.


您好b $ b

去获取并设置proprty,或在Form1中声明一个public static vairable并在Form2中调用它。 as - 例如: - string str = Form1.variable,



如果您想直接分配到数据gridview,可以这样做 -



Dtgrdview.rows [0] .cells [0] .value = Form1.variable;
Hi
Go fot the get and set proprty, or declare a public static vairable in Form1 and call this in Form2. as - eg :- string str = Form1.variable,

If you directly want to assign to data gridview the can do like this -

Dtgrdview.rows[0].cells[0].value = Form1.variable;


这篇关于C#application.openform错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆