单击按钮不正确的其他文本框中的文本框值

时间:2013-08-29 12:59:25

标签: c# textbox

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

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

<>中的每个值都应按文本框值更改。在这里你得到了我的小程序的图像:

enter image description here

现在我的问题是:第一个值是正确的,但其他值不是。例如,如果我输入第二个texbox:&#34;网站&#34;它不仅会将<website>替换为&#34;网站&#34;。它将<website>替换为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;
        }

2 个答案:

答案 0 :(得分:1)

正如评论中所指出的,需要使用Text的{​​{1}}属性:

TextBox

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中的字符串。

答案 1 :(得分:1)

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,它可以更容易地看到新网址的格式:

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);

}

然后您可能需要考虑在放入网址之前对文字进行编码,请参阅此回答https://stackoverflow.com/a/16894322/360211