已创建必要的表单,但无法创建对象。我该怎么办?

时间:2019-07-09 21:45:47

标签: c#

我有一个程序,要求用户将客户数据输入到邮件列表中。我输入了信息,但收到一条错误消息。有10个标签,五个空标签用于存储客户对象。四个用于输入数据的文本框和一个用于邮件标签的复选框。两个按钮,创建对象,然后退出。
我已将表格设置为必要的标签和文本框。只是当我运行并输入信息时,我会显示if和else语句。

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

namespace Person_and_Customer_Classes
{
    class Person
    {
        string _name;
        string _address;
        string _phoneNumber;

        // creation of Name property
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        // creation of Address property
        public string Address
        {
            get
            {
                return _address;
            }
            set
            {
                _address = value;
            }
        }
        // creation of PhoneNUmber property
        public string PhoneNumber
        {
            get
            {
                return _phoneNumber;
            }
            set
            {
                _phoneNumber = value;
            }
        }
    }
}

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

namespace Person_and_Customer_Classes
{
    class Customer : Person
    {
        int _customerNumber;
        bool _mailing;

        // Creation of customerNumber property
        public int customerNumber
        {
            get
            {
                return _customerNumber;
            }
            set
            {
                _customerNumber = value;
            }
        }
        // creation of Mailing property
        public bool Mailing
        {
            get
            {
                return _mailing;
            }
            set
            {
                _mailing = value;
            }
        }
    }
}

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 Person_and_Customer_Classes
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            // object creation for customer class
            Customer objcustomer = new Customer();
            // set the properties for customer class.
            SetCustomerData(objcustomer);
            // display the customer object properties
            label9.Text = objcustomer.Name;
            label10.Text = objcustomer.Address;
            label11.Text = objcustomer.PhoneNumber;
            label15.Text = objcustomer.customerNumber.ToString();
            label12.Text = objcustomer.Mailing.ToString();
            // set the appearance of the Label control.
            setLabelAppearance();
        }
        private void SetCustomerData(Customer objcustomer)
        {
            int customerNumber;
            bool mailing;
            // checks person's Name is empty or not
            if (label9.Text != "")
            {
                objcustomer.Name = label9.Text;
            }
            else
            {
                MessageBox.Show("Please enter the person's name");
            }
            // checks Person's address is empty or not
            if (label10.Text != "")
            {
                objcustomer.Address = label10.Text;
            }
            else
            {
                MessageBox.Show("Please enter the person's Address");
            }
            // checks person's phone number is empty
            // or not
            if (label11.Text != "")
            {
                objcustomer.PhoneNumber = label11.Text;
            }
            else
            {
                MessageBox.Show("Please enter the person's Phone number");
            }
            // checks customer number is type of
            // numeric or not
            if (int.TryParse(label12.Text, out customerNumber))
            {
                objcustomer.customerNumber = customerNumber;

            }
            else
            {
                MessageBox.Show("Invalid customer number");
            }
            // checks mailing type is boolean or not
            if (bool.TryParse(label15.ToString(), out mailing))
            {
                objcustomer.Mailing = mailing;
            }
            else
            {
                MessageBox.Show("Invalid mailing");
            }
        }
        private void setLabelAppearance()
        {
            // change the fore color of a label control.
            label9.ForeColor = Color.Tomato;
            label10.ForeColor = Color.Tomato;
            label11.ForeColor = Color.Tomato;
            label12.ForeColor = Color.Tomato;
            label15.ForeColor = Color.Tomato;
            // change the Border style of a label control.
            label9.BorderStyle = BorderStyle.FixedSingle;
            label10.BorderStyle = BorderStyle.FixedSingle;
            label11.BorderStyle = BorderStyle.FixedSingle;
            label12.BorderStyle = BorderStyle.FixedSingle;
            label15.BorderStyle = BorderStyle.FixedSingle;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

0 个答案:

没有答案