在表单中刷新用户控件:1表单后:winforms中的2个结束事件C# [英] Refresh user control in form:1 after form:2 closing event in winforms C#

查看:71
本文介绍了在表单中刷新用户控件:1表单后:winforms中的2个结束事件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单:1包含一个用户控件,在用户控件网格的按钮单击事件中,将显示表单:2。当我更新表格中的值:2并关闭它时,表格中的用户控制网格:1应该刷新,

我的代码是......



1.//主表格(FrmMain.cs)

Form:1 contains a user control, on the button click event of the user control grid , will show form:2. When I update values in form:2 and close it, the user control grid in form:1 should get refreshed,
My code is...

1.// Main Form (FrmMain.cs)

public void navBarControl1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string menu = "";
            if (e.Link.Caption.ToString().ToUpper() != "")
            {
                menu = e.Link.Caption.ToString().ToUpper();
            }
            Show(menu);
        }







private void Show(string mnu)
{
 SamplesList sample = new SamplesList();
  this.maincontainer.Controls.Clear();
  maincontainer.Controls.Add(sample );
}







public void PerformRefresh()
{
SamplesList sample = new SamplesList();
this.maincontainer.Controls.Clear(); 
maincontainer.Controls.Add(sample );                        

}





2.//用户控制(SampleList.cs)



2.//User Control(SampleList.cs)

public SamplesList()
       {
           InitializeComponent();
           refreshSamplesData();
   }

public void refreshSamplesData()
       {

           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           ds = logic.getFormData("GetSamplesData");
           dt = ds.Tables[0];
           BindingSource bsBindingSource = new BindingSource();
           bsBindingSource.DataSource = dt;
           navGridCompany.BindingSource = bsBindingSource;
           gridControl1.DataSource = bsBindingSource;

       }

private void btnNew_Click(object sender, EventArgs e)
       {
               Form2 frm = new Form2();
               frm.Show();

       }





3.// Form2(Form2..cs)

string strmode;
private void btnClose_Click(object sender, EventArgs e)
        {
                if (strmode == "strnew")
                {
                    frmMain frm = new frmMain();
                    frm.PerformRefresh();
                   
                }
	}

推荐答案

你必须使用你的frmForm类的当前对象不要创建一个新的,所以为了在两个表单对象之间进行通信,你应该做下面的步骤:



1.你的主表单 fromMain 类必须像下一个一样实现接口

You have to use the current object of your frmForm class and not to create a new one, so in order to communicate between two forms objects you should do the next steps:

1.Your main form fromMain class have to implement an interface like the next one:
public interface IMyForm
{
  void PerformRefresh();
}

class frmMain : Form, IMyForm
{
//...
}



2.然后在Form2类添加IMyForm类型的属性并在Close事件中使用它:


2.Then in the Form2 class add a property of type IMyForm and use it in Close event:

class Form2 : Form
{
 public IMyForm MainForm{get;set;}
//...

private void btnClose_Click(object sender, EventArgs e)
{
 if (strmode == "strnew")
 {
  this.MainForm.PerformRefresh();
 }
}
//...
}



3.在创建 Form2 对象时,最后在主窗体中,必须将其新属性设置为下面:


3.Finally in your main form when the Form2 object is created you must set its new property like below:

private void btnNew_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.MainForm = this;  //This is line is new!
frm.Show();

}


将这些全局变量和属性添加到form1



add these global variable and property to form1

string strmode = string.Empty;

      public string Strmode
      {
          get { return strmode; }
          set { strmode = value; }
      }





删除您在上面的帖子中发布的代码,然后点击关闭





remove code u posted in ur question above in close click

private void btnClose_Click(object sender, EventArgs e)
        {
            ///from ur previous code " if (strmode == "strnew")"  i am assuming strmode has value "strnew" .no extra code needed here
        }





将这些全局变量和属性添加到form2





add these global variable and property to form2

string strmode = string.Empty;

       public string Strmode
       {
           get { return strmode; }
           set { strmode = value; }
       }





替换btn新点击代码到这个





replace btnNew click code to this

private void btnNew_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
            strmode = frm.Strmode;
            if (strmode == "strnew")
            {
                PerformRefresh();
            }

        }


这篇关于在表单中刷新用户控件:1表单后:winforms中的2个结束事件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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