【问题标题】:Split textbox.text into other textboxes将 textbox.text 拆分为其他文本框
【发布时间】:2015-04-04 00:28:31
【问题描述】:

我目前正在寻求的结果是将textBox1.Text拆分为尽可能多的文本框,只要存在拆分符号(例如'+'或'#')

所以每个新文本框应该只有两个符号之间的单词 示例:

textBox1.text = "一+二+三";然后 textBox2.text = "one"; textBox3.text = "二"; textBox3.text = "三";

以下两个示例实现了我需要的 90%,但我仍然不知道如何将每个值放在单独的 textBox.text 中:

string str = "one\n   \ntwo\n   \nthree\n   \n   \nfour";
string[] result = Regex.Split(str, "\n\\s*");
for (int i = 0; i < result.Length; i++)
    MessageBox.Show(result[i]);

string input = "one)(two)(three)(four)(five";
string[] result = input.Split(new string[] { ")(" }, StringSplitOptions.None);
foreach (string s in result)
    MessageBox.Show(s);

【问题讨论】:

  • 这是 Windows 应用程序(WPF 或 WinForms)还是 Web 应用程序(MVC 或 WebForms)?
  • @epotter 它是 Windows 应用程序(WinForms)

标签: c#


【解决方案1】:

您需要拆分结果(就像您已经做过的那样),然后为每个结果动态创建一个文本框并设置其文本。您的代码应如下所示:

int i = 0;
foreach (var item in result)
{
    var textBox = new TextBox();
    textBox.Location = new System.Drawing.Point(0,i++*25);
    textBox.Text = item;
    this.Controls.Add(textBox);
}

整数 i 用于确保每个文本框出现在不同的位置,而不是彼此重叠。

【讨论】:

  • 嗯,这就是我真正想要的,非常感谢,但我能走得更远吗?我想知道如何将拆分文本框中的文本值用作新的搜索词。所以“Burden+of+proof”应该分成3个搜索词而不是一个。我正在使用 SQL server CE 和 C# WinForms .. 感谢您抽出宝贵时间了解更多解释,请查看我的其他未回答问题:stackoverflow.com/questions/29409857/…
【解决方案2】:

像这样的

string[] result = textBox1.text.Split('+');

foreach (int i=0;i<result.Lenght;i++)
{
TextBox box = new TextBox();
box.Name = "textBox"+i;
box.Text = result[i];
somewhere.Add(box);
}

【讨论】:

  • 感谢您的宝贵时间,但我无法让您的代码正常工作,请原谅我,但我完全是新手。它还有一些错别字,例如 (textBox1.**Text) 和 (Leng​​b>th**)。
  • 好吧,我并不是说它是确切的代码。只是试图将您推向答案。但我猜你是从另一个答案中得到的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多