簡體   English   中英

如何從在UserControl中創建的文本框中獲取值

[英]How do I get values from a textbox that was created in a UserControl

我有一個其中有兩個文本框的UserControl。 用戶可以根據需要添加這些UserControl的多個副本。 每個UserControl都添加到面板的底部。 我將如何從這些UserControls中獲取信息。

這是添加我當前正在使用的UserControl的代碼:

        private void btnAddMailing_Click(object sender, EventArgs e)
        {
            //Set the Panel back to 0,0 Before adding a control to avoid Huge WhiteSpace Gap
            pnlMailingPanel.AutoScrollPosition = new Point(0,0);

            /*I know this isn't the best way of keeping track of how many UserControls 
            I've added to the Panel, But it's what i'm working with right now.*/
            int noOfMailings=0;
            foreach (Control c in pnlMailingPanel.Controls)
            {
                if (c is MailingReference)
                    noOfMailings++;
            }
            //Add the New MailingReference to the bottom of the Panel
            /*1 is the type of Mailing, noOfMailings Determines how many mailings we've sent for 
            this  project*/
            MailingReference mr = new MailingReference(1, noOfMailings);
            mr.Location = new Point(MRXpos, MRYpos);
            MRYpos += 120;
            pnlMailingPanel.Controls.Add(mr);
        }

這是MailingReference類的代碼:

public partial class MailingReference : UserControl
{
    public String Date
    {
        get { return txtDate.Text; }
        set { txtDate.Text = value; }
    }
    public String NumberSent
    {
        get { return txtNoSent.Text; }
        set { txtNoSent.Text = value; }
    }
    /// <summary>
    /// Creates a Panel for a Mailing
    /// </summary>
    /// <param name="_letterType">Type of 0 Letter, 1 Initial, 2 Final, 3 Legal, 4 Court</param>
    public MailingReference(int _letterType, int _mailingNo)
    {

        InitializeComponent();

        //alternate colors
        if (_mailingNo % 2 == 0)
            panel1.BackColor = Color.White;
        else
            panel1.BackColor = Color.LightGray;
        switch (_letterType)
        {
            case 1:
                lblLetter.Text = "Initial";
                break;
            case 2:
                lblLetter.Text = "Final";
                break;
            case 3:
                lblLetter.Text = "Legal";
                break;
            case 4:
                lblLetter.Text = "Court";
                break;
            default:
                break;

        }
        lblMailingNumber.Text = _mailingNo.ToString();
    }

    private void label1_Click(object sender, EventArgs e)
    {
        this.Parent.Controls.Remove(this);
    }

我試過使用

 foreach (Control c in pnlMailingPanel.Controls)
        {
            if (c is MailingReference)
            {
                foreach (Control c2 in MailingReference.Controls)
                {
                    //do work
                }
            }
        }

從文本框中獲取數據,但MailingReference.Controls不存在。

我不確定如何遍歷每個MailingReference UserControl並從每個文本框中的兩個文本框中獲取數據。 有小費嗎?

據我所知,您最主要的錯誤是您試圖通過類名稱訪問實例屬性Controls 您應該改為:

foreach (Control c in pnlMailingPanel.Controls)
{
    MailingReference mailingReference = c as MailingReference;

    if (mailingReference != null)
    {
        foreach (Control c2 in mailingReference.Controls)
        {
            //do work
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM