我正在开发一个复杂的功能区应用程序,到目前为止一切顺利,但现在我有一个小问题,我必须在双倍的TextBox
之后将DataGridView的值传递给Form
点击DataGridView
。
答案 0 :(得分:0)
实际上你有很多选择。这一切都取决于您的应用程序的代码/体系结构,但通常您要寻找的是委托。 这是代表们的一篇介绍性的文章:link。 here是一些基本示例的链接,它们将向您介绍Actions和Lambdas。
或者,如果这两个表单可以互相看到(如果您在一个父表单中有两个表单的引用),则可以在源表单上创建一个事件并在目标表单上订阅它,并传递该表中的数据方式。
来源表单:
•定义代理
public delegate void RibbonDataHandler(string);
•定义事件
public event RibbonDataHandler RibbonData;
•定义执行事件的方法
protected virtual void OnRibbonData( string value )
{
if( RibbonData != null )
RibbonData( value );
}
•在 DataGridView 的 DoubleClick 事件处理程序中调用事件
string value = // Get Value from the gridView
OnRibbonData( value );
目标表单: •添加公共属性以设置 TextBox 值
public string TextBoxValue
{
get { return txtValue.Text; }
set { txtValue.Text = value; }
}
父母表格:
•将这两个表单添加为字段
private Form _sourceForm;
private Form _destinationForm;
•初始化表格
// Well, initialize the forms in the way you need it, maybe on the Load event?
_sourceForm = new SourceForm();
_destinationForm = new DestinationForm();
_sourceForm.RibbonData += new SourceForm.RibbonDataHandler(OnRibbonData);
•定义RibbonData处理程序
private void OnRibbonData( string value )
{
_destinationForm.TextBoxValue = value ?? String.Empty;
}
的声明:强> 的 我写下了所有这些,在这个时刻没有VS,如果你有更多的问题,或者如果有什么不起作用,请发表评论。 :)