开发者

Split a text box in one form to multiple labels into another form

开发者 https://www.devze.com 2023-03-24 18:53 出处:网络
I need to send information input into a text box in one开发者_Go百科 form to labels in another form. I have it split at the moment, but since there is only one split If the user enters more that one s

I need to send information input into a text box in one开发者_Go百科 form to labels in another form. I have it split at the moment, but since there is only one split If the user enters more that one space in the text box (lets say First, Middle, Last) the second form will only show first and middle. I need to split it twice times just in case the user does enter two spaces.

This is the code I have at the moment.

    //Name Split
        if (strTextBox.Contains (' '))
        {
            var fullname = strTextBox;
            var names = fullname.Split(' ');
            // perhaps TODO -- there could be a middle name/initial
            label3.Text = names[0]; // first
            label5.Text = names[1]; // middle initial
        }

        else //without this conditional if a user only enters a first name the app fails.
            label3.Text = strTextBox;

I have been trying t figure this out but I cannot seem to wrap my head around it. This code compiles fine but I cannot get it to show the last name.

Here is my code:

    //Name Split
        if (strTextBox.Contains (' '))
        {
            var fullname = strTextBox;
            var names = fullname.Split(' ');
            // perhaps TODO -- there could be a middle name/initial
            label3.Text = names[0]; // first
            label5.Text = names[1]; // middle initial
        }
        else if (strTextBox.Contains (" " + " "))
        {
            var FN = strTextBox;
            var N = FN.Split(' ');
            label3.Text = N[0] + " " + N[1]; // first and middle
            label5.Text = N[2]; // last name
        }
        else //incase only a first name is entered
            label3.Text = strTextBox;
    }

Any Suggestions?

I already have passing the information to the next form, I just need to fix this conditional so I can get all three names just incase the user does have first, middle, last.


Without seeing some code this may be a little hard to explain but here goes. You will need to do something like this.

var fullname = StringReceivedFromFirstForm;
var names = fullname.split(" ");
FirstNameLabel.Text = names[0];
SurnameLabel.Text = names[1];

Hope this helps.


Sounds like you have two separate problems.

Splitting a name into first and last name.

     string fullName = "john doe"; // "doe, john"
     string firstName;
     string lastName;
     string[] parts = fullName.Split(new string[] {", "}, StringSplitOptions.None);
     if (parts.Length == 1)
     {
        parts = fullName.Split(' ');
        if (parts.Length == 1)
        {
           lastName = fullName;
           firstName = "";
        }
        else
        {
           lastName = parts[1];
           firstName = parts[0];
        }
     }
     else
     {
        lastName = parts[0];
        firstName = parts[1];
     }

Passing the names from one form to another.

myForm2.SetName (firstName, lastName);


public class Form2 : Form
{
   public void SetName (string firstName, string lastName)
   {
      lblFirst.Text = firstName;
      lblLast.Text = lastName;
   }
   ...


string test = textbox.Text;
char[] splited = new char[]{' '};
foreach (string str in s.Split(splited ))
Console.WriteLine(str);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号