簡體   English   中英

從表單C#繼承

[英]Inheriting from a form c#

大家 我的計划是創建一個單獨的類,在其中聲明所有標簽和文本框值。 但是要做到這一點,我必須繼承一種形式。 問題是,當我從表單繼承時,我的班級變成了表單,並從自身調用元素。 將標簽和文本框的屬性設置為public並沒有幫助。 有任何想法嗎?

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
}
}

表格設計

您可能應該繼承自UserControl。 使用自己的UserControl,可以將其添加到一個或多個位置的一個或多個表單中。

有很多教程可以指導您創建自己的WinForms UserControl。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM