需要帮助将标签转换为字符串,以便在另一个标签框中输出摘要:c#

本文关键字:标签 另一个 输出 转换 帮助 字符串 | 更新日期: 2023-09-27 18:09:54

strFirstName = txtFirstName.Text;
strLastName = txtLastName.Text;
lblSummary = "FirstName:" + strFirstName + Environment.NewLine +
   "Last Name:" + strLastName + Environment.NewLine +
   "Gross Income:" + decGrossIncome.ToString("c") + Environment.NewLine +
   "Taxes Due:" + decTaxesDue.ToString("c") + Environment.NewLine +
   "Total Payments:" + decTotalPayments.ToString("c") + Environment.NewLine +
   "Total Amount Due:" + decTotalAmountDue.ToString("c");

lblSummary.Text = strSummary;

*注释*下面包含错误

"Total Amount Due:" + decTotalAmountDue.ToString("c");
我得到的错误是:

错误1无法隐式地将类型"string"转换为"System.Windows.Forms"。标签' E:'CIS 162 AD'CS03'CS03'CS03'Form1.cs 79 37 CS03

需要帮助将标签转换为字符串,以便在另一个标签框中输出摘要:c#

您正试图将字符串值直接分配给Label变量。我怀疑你是这个意思:

string strSummary = "FirstName:" + strFirstName + Environment.NewLine +
    "Last Name:" + strLastName + Environment.NewLine +
    "Gross Income:" + decGrossIncome.ToString("c") + Environment.NewLine +
    "Taxes Due:" + decTaxesDue.ToString("c") + Environment.NewLine +
    "Total Payments:" + decTotalPayments.ToString("c") + Environment.NewLine +
    "Total Amount Due:" + decTotalAmountDue.ToString("c");
lblSummary.Text = strSummary;

我个人建议使用string.Format,并顺便放弃伪匈牙利语命名。

string summary = string.Format("First Name: {1}{0}" + 
                               "Last Name: {2}{0}" +
                               "Gross Income: {3:c}{0}" +
                               "Taxes Due: {4:c}{0}" +
                               "Total Payments: {5:c}{0}" +
                               "Total Amount Due: {6:c}",
                               Environment.NewLine, firstName, lastName,
                               grossIncome, taxesDue, totalPayments,
                               totalAmountDue);
// I'm not so hot on naming controls, so I'm not saying this is great - but I
// prefer it not to be control-type-specific; what's important is that it's a
// control we're using to output the summary.
summaryOutput.Text = summary;