Reputation: 33
I'm writing a name generator, and want to add functionality to add new names and surnames to database, but when I'm trying to print values added after running program, I got whole path. For example: I want to add name John. When generator picks John it's printing Windows.Forms.TextBox, Text:John
instead of just name. How can I fix this? Here's the code:
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];
}
}
Upvotes: 1
Views: 964
Reputation: 125187
To get the text of a TextBox
you should use Text
property of TextBox
. For example: Generate.LName.Add(NameBox.Text);
Now you are using ToString
method of TextBox
which returns the type name and the text.
Upvotes: 1