简体   繁体   中英

C# Creating white space lines between assembled .text

 private void btnAssemble_Click(object sender, EventArgs e)
    {
        txtAssembled.Text = (cboTitle.Text +  txtFirstName.Text[0] + txtMiddle.Text + txtLastName.Text + "\r\n" +txtStreet.Text + "\r\n"+ cboCity.Text);           
    }

I'm trying to get 1 character white space inbetween cboTitle.Text, txtFirname.Text, txtMiddle.Text, and txtLastName, but they all output the information together, but I want them spaced evenly. what do I need to do? thanks in advance.

I'm going to post some other code thats below the one above in my project, just in case it might be relevant.

 string AssembleText(string Title, string FirstName, string MiddleInitial, string LastName, string AddressLines, string City )
    {
        string Result = "";
        Result += Title + " ";
        Result += FirstName.Substring(0, 2) + " ";

        // Only append middle initial if it is entered
        if (MiddleInitial != "")
        {
            Result += MiddleInitial + " ";
        }

        Result += LastName + "\r\n";

        // Only append items from the multiline address box
        // if they are entered
        if ( AddressLines != "")
        {
            Result += AddressLines + "\r\n";
        }
        //if (AddressLines.Length > 0 && AddressLines.ToString() != "")
        //{
        //    Result += AddressLines + "\r\n";
        //}


        Result += City;

        return Result;
    }
}

}

If you just want a space between those specific fields in btnAssemble_Click, you can just insert them like this:

string myStr = foo + " " + bar + " " + baz;

So your first function would be modified to read:

private void btnAssemble_Click(object sender, EventArgs e) { txtAssembled.Text = (cboTitle.Text + " " + txtFirstName.Text[0] + " " + txtMiddle.Text + " " + txtLastName.Text + "\\r\\n" + txtStreet.Text + "\\r\\n" + cboCity.Text); }

A few other comments:

  • It's not clear to me what the AssembleText() function you posted has to do with this. I am confused though, as I see a few lines appending spaces at the end just like I mentioned above.
  • Using the String.Format() function may make this code easier to read and maintain.
  • Using Environment.NewLine instead of "\\r\\n" will make the string contain the newline character defined for that specific environment.
  • Using a StringBuilder object may be faster over concatenation when building strings inside of a loop (which may not apply here).

Using String.format() should feet the bill. It also make your code easy to read.

txt.assembled.text = String.Format("{0} {1} {2} {3}",
cboTitle.Text,
txtFirstName.Text[0],
txtMiddle.Text,
txtLastName.Text
);

It would be like this

 private void btnAssemble_Click(object sender, EventArgs e)
{
    txtAssembled.Text = (cboTitle.Text + " " + txtFirstName.Text[0] + " " +txtMiddle.Text + " " + txtLastName.Text + "\r\n" +txtStreet.Text + "\r\n"+ cboCity.Text);           
}

It seems that you want String.Join ; whenever you want to combine strings with a delimiter, say, " " (space) all you need is to put

  String combined = String.Join(" ", 
                                cboTitle.Text, 
                                txtFirstName.Text[0], 
                                txtMiddle.Text, 
                                txtLastName.Text);

Complete implementation (joining by space and new line ) could be

  txtAssembled.Text = String.Join(Environment.NewLine,
    String.Join(" ", 
                cboTitle.Text, 
                txtFirstName.Text[0], 
                txtMiddle.Text, 
                txtLastName.Text),
    txtStreet.Text, 
    cboCity.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM