开发者

How can we call the controls from one form to another form of the same application

I have two forms in C# Window application as frm_Stock.cs and frm_Purchase.cs. I want to use some controls of frm_开发者_JAVA技巧Stock in the frm_Purchase.Is it possible?IF yes then how can i do this please give me suitable example.

Thanks in advance


You will have to pass a reference to the controls/form when constructing the other form, and use that reference. A rough example,

frm_Stock = new StockForm();
frm_Purchase = new PurchaseForm(frm_Stock);

then within the purchase form code...

public class PurchaseForm : Form
{
  public PurchaseForm(StockForm frm_Stock)
  {
    frm_Stock.SomeControl.Text = "blah";
  }
}


It is possible but not recommended way of doing it, you should have pass it to the other form via some shared object(i-e StateBag ). You can make the controls of Frm_Stock public and then they will be accsible from frm _Purchase when you make frm_Stock instance.


You should create a custom user control and use it on both forms.

If you want to share the same instance of the user control across the form, then create an instance when your application starts and add it manually on both forms when they load.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜