如何在winforms中从文本框中打印标签值?

时间:2016-03-06 14:42:47

标签: c# winforms

我正在编写名称生成器,并希望添加向数据库添加新名称和姓氏的功能,但是当我尝试打印运行程序后添加的值时,我得到了整个路径。例如:我想添加名称​​ John 。当生成器选择 John 时,它会打印Windows.Forms.TextBox, Text:John而不仅仅是名称。我怎样才能解决这个问题?这是代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NameGen
{
public partial class Form1 : Form
{
    public Generate gen = new Generate();
    public Form1()
    {
        InitializeComponent();
    }

    private void StartGenerate(object sender, EventArgs e)
    { 
        lblWynik.Text = Generate.NameGen() + " " + Generate.SurnameGen();
        Console.WriteLine("Generating: " + lblWynik.Text);
    }

    private void AddName(object sender, EventArgs e)
    {
        Generate.LName.Add(NameBox.ToString());
        Console.WriteLine("Adding new name to index " + NameBox);
        NameBox.Clear();
    }

    private void AddSurname(object sender, EventArgs e)
    {  
        Generate.LSurname.Add(SurnameBox.ToString());
        Console.WriteLine("Adding new surname to index " + SurnameBox);
        SurnameBox.Clear();
    }
}

public class Generate
{
    static string[] Name = new string[] { "Hank", "Terrence", "Darin", "Alf" };
    static string[] Surname = new string[] { "Cooper", "Trump", "Białkov", "Obama" };

    public static List<string> LName = new List<string>();
    public static List<string> LSurname = new List<string>();
    static Random random = new Random();

    public static string NameGen()
    {
        LName.AddRange(Name);
        int NameIndex = random.Next(LName.Count);
        return LName[NameIndex];
    }

    public static string SurnameGen()
    {
        LSurname.AddRange(Surname);
        int NameIndex = random.Next(LSurname.Count);
        return LSurname[NameIndex];
    }
}

1 个答案:

答案 0 :(得分:1)

要获取{ "cmd": ["ipconfig", ">nul 2>&1"], "name": "ipconfig", "shell": "true" } 的文字,您应使用TextBox的{​​{3}}属性。例如:TextBox

现在您使用Generate.LName.Add(NameBox.Text); ToString方法返回类型名称和文本。