如何根据运行时出现的需求制作动态标签

时间:2017-05-11 19:20:38

标签: c# visual-studio controls

我正在努力制定一个能够根据工资和薪酬来追踪家庭预算的计划。 为此,我希望我的程序要做的是为家里的每个工作人员生成(并在需要时删除)2个标签(名称和工资)。

到目前为止,我一直在尝试各种不同的方式,甚至直接从类似于我的问题中复制代码以显示标签,但无论我做什么,它们都不会出现(即使属性'IsDisposed'显示了标签仍然存在)

这是我的意大利面条代码:

Forms1.cs

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

namespace Budget_Management
{
    public partial class Form1 : Form
    {

        static Person[] person = new Person[1];
        static Label[] name = new Label[1];
        static Label[] salary = new Label[1];
        static int numOfPeople = 0;



        public Form1()
        {
            InitializeComponent();
            Label myLabel = new Label();
            myLabel.Top = 10;
            myLabel.Left = 10;
            MessageBox.Show(Convert.ToString(myLabel.IsDisposed));
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void addaPersonButton_Click(object sender, EventArgs e)
        {
            int temp;
            if (int.TryParse(salaryTextBox.Text, out temp) == true)
                {
                person = Program.increasePersonAmount(person, numOfPeople);

                person[numOfPeople] = new Person();
                person[numOfPeople].salary = Convert.ToInt32(salaryTextBox.Text);
                person[numOfPeople].name = nameTextBox.Text;
                name = Program.increaseLabelAmount(name, numOfPeople);
                salary = Program.increaseLabelAmount(salary, numOfPeople);
                Program.generateLabels(name, salary, nameTextBox.Text, Convert.ToInt32(salaryTextBox.Text), numOfPeople);
                numOfPeople++;
            }
            else
            {
                MessageBox.Show("Salary is invalid number!");
            }

        }


    }
}


Program.cs:

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

namespace Budget_Management
{
    static class Program
    {
        static int numOfSpendings;
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }

        public static Person[] increasePersonAmount(Person[] old, int amount)
        {
            Person[] temp = new Person[amount + 1];
            for (int i = 0; i < old.Length-1; i++)
            {


                 temp[i] = old[i];

            }
            return temp;
        }


        public static Person[] descreasePersonAmount(Person[] old, int personToDelete, int amount)
        {
            Person[] temp = new Person[amount-1];
            for (int i = 0, j = 0; i < old.Length; i++)
            {
                if (i == personToDelete)
                {

                }
                else
                {
                    temp[j] = old[i];
                    j++;
                }
            }
            return temp;
        }

        public static Label[] increaseLabelAmount(Label[] old, int amount)
        {
            Label[] temp = new Label[amount + 1];
            for (int i = 0; i < old.Length - 1; i++)
            {


                temp[i] = old[i];

            }
            return temp;
        }
        public static void generateLabels(Label[] nameLabel, Label[] salaryLabel, string name, int salary, int labelNum)
        {
            nameLabel[labelNum] = new System.Windows.Forms.Label();

            nameLabel[labelNum].Text = name;
            nameLabel[labelNum].Left = 10;
            nameLabel[labelNum].Top = 5;
            nameLabel[labelNum].Visible = true;

        }
        public static void deleteLabel(int labelNum)
        {

        }

    }
}

1 个答案:

答案 0 :(得分:0)

当hoy需要un命令隐藏标签时,请使用Visible属性设置,如果错误。