我有两种形式(C#)。在一种形式中,有一种方法可以在关闭表单之后将数据存储在数据库中,并在另一个表单上(主表单)更新数据。如何使用面向对象或简单地做到最漂亮和最好。在C中将参数从一种形式发送到另一种形式#
-1
A
回答
1
通常,当您想要更新主窗体时,您可以在该窗体上创建一个公共方法,并在具有新数据并可以将它们发送到主窗体时从另一窗体调用它。这不应该是一个问题。
请注意,如果要将数据发送到某处,则需要对该位置的引用,即您需要对其他表单中的主表单进行引用。无论是从主要形式传递this
其他形式的构造函数,或者你也可以存储在Program
类(这样做在Main
方法,其中创建的主要形式)等
0
最OOP在静态字段参考友好的解决方案可能是以任何形式“触发”数据更新的事件,该数据更新由另一个表单的方法订阅并处理。这里有一个基本的线路:
public class Form1:Form
{
public event EventHandler<MyDataObject> DataChanged;
...
public override void OnClosing(CancelEventArgs e)
{
//Put in logic to determine whether we should fire the DataChanged event
try
{
if(DataChanged != null) DataChanged(this, myFormCurrentData);
base.OnClosing(e);
}
catch(Exception ex)
{
//If any handlers failed, cancel closing the window until the errors
//are resolved.
MessageBox.Show(ex.Message, "Error While Saving", MessageBoxButtons.OK);
e.Cancel = true;
}
}
}
...
public class Form2:Form
{
//Called from wherever you would open Form1 from Form2
public void LaunchForm1()
{
var form1 = new Form1();
form1.DataChanged += HandleDataChange;
form1.Show();
}
private void HandleDataChange(object sender, MyDataObject dataObj)
{
//Do your data validation or persistence in this method; if it fails,
//throw a descriptive exception, which will prevent Form1 from closing.
}
}
你不必使用一个事件;也可以使用一个简单的委托,它可以做几乎相同的事情,同时也可以在表单的构造函数中指定(因此需要提供处理函数)。
0
您可以从另一种形式更新以某种形式的数值做这样的事......
表2码
public event EventHandler<UpdatedEventArgs> updateEvent;
public class UpdatedEventArgs : EventArgs
{
public string SomeVal { get; set; } // create custom event arg for your need
}
protected virtual void OnFirstUpdateEvent(UpdatedEventArgs e)
{
if (updateEvent != null)
updateEvent(this, e);
}
private void button1_Click(object sender, EventArgs e)
{
UpdatedEventArgs eventData = new UpdatedEventArgs();
eventData.SomeVal = "test"; // set update event arguments, according to your need
OnFirstUpdateEvent(eventData);
}
public Form2()
{
InitializeComponent();
}
表1码
public Form1()
{
InitializeComponent();
Form2 form2 = new Form2();
form2.updateEvent += new EventHandler<Form2.UpdatedEventArgs>(form2_updateEvent); // create event handler to update form 1 from form 2
form2.Show();
}
void form2_updateEvent(object sender, Form2.UpdatedEventArgs e)
{
if (e != null && e.SomeVal != null)
{
// Do the update on Form 1
// depend on your event arguments update the grid
//MessageBox.Show(e.SomeVal);
}
}
相关问题
- 1. Rails:如何将参数从一种形式传递到另一种形式
- 2. C#从另一种形式
- 3. 从另一种形式调用工具到另一种形式?
- 4. C#发送从数据库中选定值从一种形式到另一种形式
- 5. 从一种形式到另一种形式的计时器值
- 6. php htmlspecialchars()从一种形式到另一种形式
- 7. 使用Alpacajs从一种形式导航到另一种形式
- 8. 在Qt中将变量从一种形式传递到另一种形式
- 9. 传递String与另一个形式从一种形式在C#
- 10. 从另一种形式
- 11. 从另一种形式
- 12. 从另一种形式
- 13. 从另一种形式
- 14. 试图在angularjs中从一种形式转到另一种形式
- 15. 将datagridview的值从一种形式转移到另一种形式datagridview
- 16. 继承类一种形式到另一种形式类
- 17. 将值从一种形式传递给另一种(C#)
- 18. 一种形式到多种形式
- 19. 如何在winforms c#中将数据从一种形式传输到另一种形式?
- 20. 从一种阵列形式到另一种形式的数据转换
- 21. 在另一种形式中C#
- 22. 在一种形式的Recaptcha阻止另一种形式
- 23. 如何让一种形式留在另一种形式上?
- 24. 从一种形式将数据传递到在Windows窗体另一种形式的C#
- 25. 从一种形式检索结果并将结果用于另一种形式?
- 26. 值从一种形式传递到另一种形式来为空值
- 27. 从一种形式拖放到另一种形式的任何地方
- 28. 从通过从一个数据为另一种形式在C#
- 29. 另一种形式内的一种形式
- 30. 角:创建基于另一种形式的一种新形式