使用C#的局部类实现 [英] partial class implementation using c#

查看:135
本文介绍了使用C#的局部类实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有带有多个选项卡的选项卡控件的表单,我想将每个选项卡的代码放在不同的文件中(表单类的部分类);我试图这样做,但是我无法从其他文件访问mais类/form的对象.有人知道为什么吗?谢谢

这是代码示例(表格):

I have a form with a tab control with multiple tabs, and I would like to put the code of each tab in a diferent file (partial classes of the form class); I tried to do that but I can not access the objects of the mais class /form from the other files. Any one knows why ? thanks

Here is a sample of the code (Form):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimplyApp
{
  public partial class frmPlanning : Form
  {
    public frmPlanning()
    {
      InitializeComponent();
      //lstvOutputs_Circuits.ColumnWidthChanging += new ColumnWidthChangingEventHandler(lstvOutputs_Circuits_ColumnWidthChanging);
    }
    SQLutils mySQL = new SQLutils();
    static byte Actuator_NumOutputs = 0;
    int prjActuatorId = 0;
    string CircuitStatus = "";
    string ActuatorStatus = "";
    public void frmPlanning_Load(object sender, EventArgs e)
    {
      Init();
    }
 
    private void Init()
    {
      mySQL.SetConn();  
      tabOptions.Visible = false;

      cboProjectCodes.Items.Clear();
      tabOptions.SelectTab(0);
      FillModuleTypes();
      FillPanelColors();
      FillImageList();
      dgvPrjCircuits.BackgroundColor = this.BackColor;
      dgvPrjActuators.BackgroundColor = this.BackColor;
      tabAct_dgvActuatorSelection.BackgroundColor = this.BackColor;
      dgvPrjRelActuatorOutputs_Circuits.BackgroundColor = this.BackColor;
      dgvPrjPanelPrgs.BackgroundColor = this.BackColor;
      dgvPrjActuatorsPrgs.BackgroundColor = this.BackColor;
      AssignImgsToTs();
      cont_Planeamento.SplitterDistance = 425;
    }

    private void tsPrj_Select_Click(object sender, EventArgs e)
    {
      if (cboDivisions.Text != "")
      {
        cboProjectCodes.Enabled = false;
        cboPlans.Enabled = false;
        cboDivisions.Enabled = false;
        tabOptions.Visible = true;
        RefreshDgvPrjCircuits();
        dgvPrjCircuits.ClearSelection();
        RefreshDgvPrjActuators();
        dgvPrjActuators.ClearSelection();
        TagUsedCircuits();
      }
    }


其他文件代码......


other file code ......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SimplyApp
{
  public partial class frmPlanning
  {
   //I do not have any access to any control in the form ... why ?
  }
}




感谢




Thanks

推荐答案

这些控件已在其他部分类页面中声明.您不能在声明性上下文中访问它们,但可以在功能性上下文中访问它们.
Those controls were declared already in the other partial class pages. You cannot access them in a declaritive context, but you can in a functional one.
public partial class frmPlanning
  {
   //I do not have any access to any control in the form ... why ?
   // cannot do anything with cboProjectCodes here, must put in a funtion
   public void Test()
   {
       cboProjectCodes.Items.Clear();
   }
  }


请记住,分部类的所有页面都是彼此的延续,因此您不能仅使用已声明的变量/控制变量,而必须将它们放入函数中.您还可以声明其他页面上不存在的新变量.与方法相同,您可以从另一个方法内部调用它们,也可以重载现有的方法.


Remember that all pages of the partial class are just continuations of each other, so you cannot just use variables/contraols that have been declared, but rather must put them in functions. You can also declare new variables that do not exist on other pages. Same with methods, you can call them from inside another method or overload the existing.


您应该能够访问部分类中的所有方法.

您将无法访问其他文件中的主窗体方法,因为您的方法似乎全部是private.
You should be able to access all methods within your partial class.

You will not be able to access main form methods in other files as your methods appear to be all private.


我不知道,因为您没有显示代码,但部分类的实现原则上不会引起任何问题.部分类声明的行为就像在一个文件中一样.

相反,您需要在遇到问题时检查"using"子句并添加缺少的内容.

—SA
I don''t know because you don''t show the code, but partial class implementation cannot cause any problem in principle. The partial class declarations behave precisely as if they were in one file.

Rather, you need to check up "using" clauses where you have a problem and add what''s missing.

—SA


这篇关于使用C#的局部类实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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