2016-12-17 65 views
0

大家。我的计划是创建一个单独的类,我将在其中声明所有的标签和文本框值。但要做到这一点,我必须从表单继承。问题是,当我从窗体继承时,我的类变成了窗体并从它自身调用元素。将标签和文本框的属性设置为公共不会有帮助。有任何想法吗?从表单c继承#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Assignment2v3 
{ 
class Declarations : Form1 
{ 
    public List<Label> ErrRep 
    { get; set; } 
    public List<TextBox> TextBoxes 
    { get; set; } 
    public List<ComboBox> ComboBoxes 
    { get; set; } 
    public Declarations() 
    { 
     ErrRep = DeclareErrorReports(); 
     TextBoxes = DeclareTextBoxes(); 
     ComboBoxes = DeclareComboBoxes(); 
    } 
    List<Label> DeclareErrorReports() 
    { 
     var ER = new List<Label>(); 
     ER.Add(errorReport1); 
     ER.Add(errorReport2); 
     ER.Add(errorReport3); 
     return ER; 
    }//Would be used if try catch worked 
    List<TextBox> DeclareTextBoxes() 
    { 
     List<TextBox> TextBoxes = new List<TextBox>(); 
     TextBoxes.Add(textBoxPizza1); 
     TextBoxes.Add(textBoxPizza2); 
     TextBoxes.Add(textBoxPizza3); 
     return TextBoxes; 
    }//Puts all textBoxes into a list 
    List<ComboBox> DeclareComboBoxes() 
    { 
     var ComboBoxes = new List<ComboBox>(); 
     ComboBoxes.Add(comboBoxPizza1); 
     ComboBoxes.Add(comboBoxPizza2); 
     ComboBoxes.Add(comboBoxPizza3); 
     return ComboBoxes; 
    }//Puts all comboboxes into a list 
    //^Boring declarations 
} 
} 

Form design

+0

这是什么意思:问题是,当我从窗体继承我的类变成窗体并从它自己调用元素。 – CodingYoshi

+0

在屏幕截图上查看。 Declarations.cs成为一种形式。它甚至有一个设计选项,像一个正常的窗体形式 – alex3wielki

+1

以及继承是如何工作的。如果这不是你想要的,那么重新提出你的问题,并告诉我们你想完成什么,并且有人可以告诉你如何完成。整个继承点是:我需要基类所有的东西,再加上我会添加更多。 – CodingYoshi

回答

2

你或许应该从用户控件继承来代替。通过您自己的UserControl,您可以将其添加到一个或多个地方的一个或多个表单中。

有很多指导你创建你自己的WinForms UserControl的教程。

+0

将研究。谢谢 – alex3wielki