在 C# 中读取 TextBox 值时出错

Error reading TextBox value in C#

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

这是我的 window 代码:

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.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.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.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).

'Name: , Email: ' 就是我的价值观应该在的地方。

预先感谢您的帮助,如果您需要更多信息,请直接说,我会提供。

    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。除非您有令人信服的理由将其设为 struct
  5. ,否则只需使用 class