读取C#中的TextBox值时出错

时间:2016-04-22 01:28:51

标签: c# textbox windows-forms-designer

我在制作一个能从TextBox中读取文本的程序时遇到了一些问题。我正在从两个TextBox中读取两个值,但似乎没有什么是实际读取的。最终发生的是我在TextBoxes中输入值并按下按钮将值输入到程序中。我已经将它设置为打印出我的TextBox中的值,但是当它打印时,看起来它没有从TextBox中读取任何内容。

这是我的窗口代码:

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 Giveaway_Program
{
    public partial class giveawayProgram : Form
    {
        public GiveawayEntry[] entries = {};

        public giveawayProgram()
        {
            InitializeComponent();
        }

        private void Giveaway_Load(object sender, EventArgs e)
        {

        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void emailText_TextChanged(object sender, EventArgs e)
        {

        }

        private void nameText_TextChanged(object sender, EventArgs e)
        {

        }

        private void numberOfTimes_ValueChanged(object sender, EventArgs e)
        {

        }

        private void addToList_Click(object sender, EventArgs e)
        {
            addEntry(new GiveawayEntry(emailText.Text, nameText.Text));
        }

        private void generateWinner_Click(object sender, EventArgs e)
        {

        }

        private void addEntry(GiveawayEntry newEntry)
        {
            GiveawayEntry[] newEntries = new GiveawayEntry[entries.Length + 1];
            Array.Copy(entries, newEntries, entries.Length);
            entries = newEntries;

            for (int i = 0; i < entries.Length; i++)
            {
                Console.WriteLine("Name: " + entries[i].Name + ", Email: " + entries[i].Email);
            }
        }
    }
}

这是值进入的结构:

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

namespace Giveaway_Program
{
    public struct GiveawayEntry
    {
        string email, name;

        public GiveawayEntry(string _email, string _name)
        {
            email = _email;
            name = _name;
        }

        public string Email
        {
            get
            {
                return email;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }
    }
}

这是程序的控制台输出:

'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\Users\Overlord Nate\Google Drive\Projects\Giveaway Program\Giveaway Program\bin\Debug\Giveaway Program.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0xd4c has exited with code 0 (0x0).
The thread 0x28e8 has exited with code 0 (0x0).
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\Users\Overlord Nate\Google Drive\Projects\Giveaway Program\Giveaway Program\bin\Debug\Giveaway Program.exe'. Symbols loaded.
'Giveaway Program.vshost.exe' (CLR v4.0.30319: Giveaway Program.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Name: , Email: 
The thread 0x2ec0 has exited with code 0 (0x0).
The thread 0x1630 has exited with code 0 (0x0).
The program '[6500] Giveaway Program.vshost.exe' has exited with code 0 (0x0).

'姓名:,电子邮件:'是我的价值所在的地方。

提前感谢您的帮助,如果您需要更多信息,请简单说出来,我会提供。

1 个答案:

答案 0 :(得分:1)

    private void addEntry(GiveawayEntry newEntry)
    {
        GiveawayEntry[] newEntries = new GiveawayEntry[entries.Length + 1];
        Array.Copy(entries, newEntries, entries.Length);
        entries = newEntries;

        for (int i = 0; i < entries.Length; i++)
        {
            Console.WriteLine("Name: " + entries[i].Name + ", Email: " + entries[i].Email);
        }
    }
  1. 您没有将newEntry添加到数组
  2. 如果您只使用List<GiveawayEntry>
  3. 会更容易
  4. 没有必要将其设为struct。只需使用class,除非您有令人信服的理由将其设为struct