【问题标题】:Textbox value in other textbox by clicking button not correct单击按钮在其他文本框中的文本框值不正确
【发布时间】:2013-08-29 12:59:24
【问题描述】:

这个程序有一个固定的链接,永远不会改变。它包含 5 个文本框。固定链接是:

<seite>utm_source=<website>_<page>_de&utm_medium=banner&utm_campaign=<kampagne>&utm_content=<format>

&lt;&gt; 中的每个值都应由文本框值更改。这是我的小程序的图像:

现在我的问题是:第一个值是正确的,但其他值不是。因此,例如,如果我输入第二个 texbox:“website”,它不仅将&lt;website&gt; 替换为“website”。它将&lt;website&gt; 替换为System.Windows.Forms.TextBox, Text: website

我尝试过的代码:

private void btn_SendAll_Click(object sender, EventArgs e)
        {
            txt_FinishLink.Text = txt_Site.Text + "utm_source=" + txt_Website + "_" + txt_Page + "_de&utm_medium=banner&utm_campaign=" + txt_Campaign + "&utm_content=" + txt_Format;
        }

【问题讨论】:

  • 你需要在每个文本框名称后使用.Text 来获取文本框中的字符串。
  • 你应该使用你的文本框的文本属性,txt_Campaign.Text 等等。
  • 我无法从您发布的代码中真正看出,但我认为您的“txt_Website”和其他的也需要是“txt_Website.Text”等等。
  • @Tim 当您提供问题的完整解决方案时,为什么不发布答案而不是评论?
  • @Andrew 因为这太简单了

标签: c# textbox


【解决方案1】:

正如 cmets 中所指出的,需要使用 TextBoxText 属性:

txt_FinishLink.Text = txt_Site.Text + "utm_source=" + txt_Website.Text + "_" + txt_Page.Text + "_de&utm_medium=banner&utm_campaign=" + txt_Campaign.Text + "&utm_content=" + txt_Format.Text

Text将返回指定TextBox中的字符串。

【讨论】:

  • 非常感谢。我不知道我写这段代码是怎么想的。但它现在可以工作了:)
【解决方案2】:
private void btn_SendAll_Click(object sender, EventArgs e)
{
    txt_FinishLink.Text = txt_Site.Text + "utm_source=" +
                          txt_Website.Text + "_" +
                          txt_Page.Text + "_de&utm_medium=banner&utm_campaign=" +
                          txt_Campaign.Text + "&utm_content=" +
                          txt_Format.Text;
}

尽管看一下 string.Format,它可以更容易地查看新 url 的格式:

private void btn_SendAll_Click(object sender, EventArgs e)
{
    txt_FinishLink.Text = string.Format(
            "{0}utm_source={1}_{2}_de&utm_medium=banner&utm_campaign={3}&utm_content={4}",
             txt_Site.Text, //{0}
             txt_Website.Text,  //{1} etc.
             txt_Page.Text,
             txt_Campaign.Text,
             txt_Format.Text);

}

那么您可能要考虑在放入 URL 之前对文本进行编码,请参阅此答案https://stackoverflow.com/a/16894322/360211

【讨论】:

  • +1 - 使用 String.Format 和编码文本的要点。
猜你喜欢
  • 2013-12-13
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多