0

在我的 C# windows 窗体中,我有 2 个窗体。我想在表单的标签中显示一组字符串。当我调试时,我在我的数组中显示了 2 个元素,但它们没有显示在我传递给它的标签中。当我悬停 toString 时,数据就在那里,但是如何将它传递给发件人,以便它显示在我的表单上的标签控件中?

我试图通过的细节

在下面的代码片段中,数据在 toString 中,但我如何从那里得到它到 sender.ToString ????

   public AccountReport_cs(Func<string> toString)
    {
        this.toString = toString;


    }

    private void AccountReport_cs_Load(object sender, EventArgs e)
    {

        label1.Text = sender.ToString();
    }

这是另一段代码,它将打开应显示信息的 form2。

        private void reportButton2_Start(object sender, EventArgs e)
    {
        AccountReport_cs accountReport = new AccountReport_cs(allTransactions.ToString);
        accountReport.ShowDialog();
    }

这是最后一段代码,它将显示数据如何到达 EndOfMonth。

    public class Transaction
{
    public string EndOfMonth { get; set; }
}

        public override List<Transaction> closeMonth()
        {
            var transactions = new List<Transaction>();
            var endString = new Transaction();

                endString.EndOfMonth = reportString;
            transactions.Add(endString);

            return transactions;
        }
4

1 回答 1

2

如果您需要在表单之间发送信息,您可以做的最好的事情是在目标表单中创建一个属性并在显示表单之前分配您要发送的值;因此您不需要更改表单的默认构造函数。

// Destiny form
public partial class FormDestiny : Form {
    // Property for receive data from other forms, you decide the datatype
    public string ExternalData { get; set; }

    public FormDestiny() {
        InitializeComponent();
        // Set external data after InitializeComponent()
        this.MyLabel.Text = ExternalData;
    }
}

// Source form. Here, prepare all data to send to destiny form
public partial class FormSource : Form {
    public FormSource() {
        InitializeComponent();
    }

    private void SenderButton_Click(object sender, EventArgs e) {
        // Instance of destiny form
        FormDestiny destinyForm = new FormDestiny();
        destinyForm.ExternalData = PrepareExternalData("someValueIfNeeded");
        destinyForm.ShowDialog();
    }

    // Your business logic here
    private string PrepareExternalData(string myparameters) {
        string result = "";
        // Some beautiful and complex code...
        return result;
    }
}
于 2018-11-11T18:08:49.023 回答