【问题标题】:Run C# function in another form以另一种形式运行 C# 函数
【发布时间】:2021-12-20 02:48:12
【问题描述】:

我在 C# 中有两个名为 MainPage 和 Roles 的表单。

MainPage 是主窗体,Roles 是用户控件窗体。我正在使用 PanelSlider 在“MainPage”中显示“角色”,以便同时加载和显示两者。

我正在尝试将一些文本从 Roles 发送到 MainPage,但我无法做到这一点;我也在尝试从 MainPage 在 Roles 中运行一个函数。

主页代码:

public MainPage(string action, string user)
{
    InitializeComponent();
    PanelSlider.Controls.Find("Roles", false)[0].BringToFront();
    if (action == "accepted")
    {
         SideNavButtonContainer.Visible = true;
         siteuser.Text = user;
         siteuser.Visible = true;
    }
}
        
private void button1_Click(object sender, EventArgs e)
{
    // When this button is clicked I want to run 
    // doChange("started"); in Roles
}

角色代码:

public partial class Roles : UserControl
{
    public Roles()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
         MainPage frm = new MainPage("accepted", mname.Text);
         // Here I tried changing the text in MainPage 
         // but it does not change anything.    
    }

    private void doChange(string what) 
    { 
        //This is the function im trying to run from MainPage
        label1.Text = "Function has been run";
    }
}

如何将文本从 Roles 发送到 MainPage,然后从 MainPage 运行 doChange("started") 到 Roles?

我尝试使用发送文本

MainPage frm = new MainPage("accepted", mname.Text);

但 MainPage 上没有任何变化。

【问题讨论】:

  • 反馈:如果您可以在此处的帖子上运行英文拼写检查器,我们将不胜感激。请记住,您的问题和答案对其他读者具有长期的教育价值。

标签: c# winforms user-controls


【解决方案1】:

要访问另一个类中的方法,您需要将该方法的可见性设置为public,但这还不够。您还需要对当前在您的主页中使用的 Roles 类的对象实例的引用。
所以你可以写这样的东西

首先在 Roles 类中公开方法

public void doChange(string what) { 
   label1.Text = "Function has been run";
}
    

然后将您的 MainPage 代码更改为

private Roles usrRoles;
public MainPage(string action, string user)
{
      InitializeComponent();
      // Get the reference to the current instance of the usercontrol of type Roles 
      usrRoles = PanelSlider.Controls.Find("Roles", false)[0] as Roles;

      // We should really test for null here, but for keeping the example simple..
      usrRoles.BringToFront();
      if (action == "accepted")
         ...
}

private void button1_Click(object sender, EventArgs e)
{
    // Now you can call the method defined by the Roles on the current usercontrol instance 
    usrRoles.doChange("started")
}

请注意,我使用了一个名为 usrRoles 的全局类变量,您可以将其删除,但在 button1_Click 代码中您需要使用 Panel.FindFirst 再次获取引用

同样,如果您想向 MainPage 类实例发送内容,您需要对当前承载 UserControl 的 MainPage 实例的引用。这可以从用户控件的 this.Parent 属性中检索,然后调用该 MainPage 实例上的公共方法

代码 MainPage frm = new MainPage("accepted", mname.Text); 创建了一个 MainPage 的新实例,这个新实例永远不会显示,而当前现有的实例则不会完全受影响。

所以给MainPage添加一个公共方法

public void RunCommand(string command, string parameter)
{
     if(command == "accepted")
        .....
}

并从 UserControl button1 点击事件中调用它

private void button1_Click(object sender, EventArgs e)
{
   // this cast is necessary because Parent is of type object 
   // and object has no RunCommand method. Cast will fail if 
   // Parent is not an instance of MainPage
   var mainP = this.Parent as MainPage;
   if(mainP != null)
       mainP.RunCommand("accepted", mname.Text);
}

因此,除了回答您的问题外,我还希望包含一个文章链接,该链接解释了Class, Objects and Instances 之间的区别。正确理解这些概念以及如何在您选择的编程语言中应用它们至关重要。

【讨论】:

  • 感谢您的回答,尝试使用 usrRoles = PanelSlider.Controls.Find("Roles", false)[0];我收到此错误:CS0266 无法将类型“System.Windows.Forms.Control”隐式转换为“MyProject.UserControls.Roles”。存在显式转换(您是否缺少演员表?)
  • 添加 as Roles 到提到的行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2019-03-09
  • 2015-06-06
相关资源
最近更新 更多