0

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

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

中的每个值<>都应由文本框值更改。这是我的小程序的图像:

在此处输入图像描述

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

2 回答 2

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 ,它可以更容易地查看新 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

于 2013-08-29T13:27:44.227 回答
1

正如评论中指出的,需要使用的Text属性: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将返回指定的字符串TextBox

于 2013-08-29T13:23:43.220 回答