Gunnar
Gunnar

Reputation: 113

I need help on displaying multiple text boxes for C#?

I am trying to make a mailing label program using WinForms where you enter your name, state, city, etc. and it click on a button and it display all the text you entered in each box and displays it on one label. I am close but when i run my program, there is no space between the words. Here is my code:

namespace Mail_Label_Program
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            lblMessage.Text = txtFirst.Text + txtLast.Text;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //This closes the program.
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //This clears all textbox forms.
            txtFirst.Text = string.Empty;
            txtLast.Text = string.Empty;
            txtCity.Text = string.Empty;
            txtStreet.Text = string.Empty;
            txtState.Text = string.Empty;
            txtZip.Text = string.Empty;
        }
    }
}

Upvotes: 3

Views: 350

Answers (4)

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

Try

lblMessage.Text = string.Format("{0} {1}", txtFirst.Text, txtLast.Text);

you can add further text like

lblMessage.Text = string.Format("{0} {1} {2} {3}...", txtFirst.Text, txtLast.Text, text2, text3...);

Upvotes: 0

Beep.exe
Beep.exe

Reputation: 1398

lblMessage.Text = txtFirst.Text + " " +  txtLast.Text;

Upvotes: 0

Eli Arbel
Eli Arbel

Reputation: 22739

That's because you didn't add a space between them when you concatenated the string. You can use:

lblMessage.Text = String.Join(" ", txtFirst.Text, txtLast.Text);

The same Join method can be used for any number of fields:

String.Join(" ", txtFirst.Text, txtLast.Text, txtCity.Text);

Alternatively, you can use String.Format:

string.Format("Name: {0} {1}, Address: {2}", txtFirst.Text, txtLast.Text, txtCity.Text);

Upvotes: 0

dcaswell
dcaswell

Reputation: 3167

Replace this:

lblMessage.Text = txtFirst.Text + txtLast.Text;

With this:

lblMessage.Text = txtFirst.Text + " " + txtLast.Text;

If someone enters leading/trailing blanks you might like this:

lblMessage.Text = trim(txtFirst.Text) + " " + (txtLast.Text);

Upvotes: 8

Related Questions