【问题标题】:How to pass data between forms?如何在表单之间传递数据?
【发布时间】:2014-02-22 10:31:37
【问题描述】:

我正在开发一个桌面应用程序,我想在其中从用户那里获取信息。如果用户选择特定的单选按钮,那么我将在弹出窗口中打开一个新表单,其中放置了选中的列表框。从复选框中选择值后,我想访问上一个表单中的选定值。下面是可以清晰思路的图片。

当用户单击“启用内容类型”中的单选按钮(如屏幕中突出显示的那样)时,弹出窗口中会打开一个新表单以从选中的列表框中选择值。选择所需的值后,从“选择内容类型”表单中按“选择”按钮。

现在表单将被隐藏,但我想在“创建列表”表单中获取选定的值。

我的单选按钮事件代码是:

private void rdbEnableCtypeYes_CheckedChanged(object sender, EventArgs e)
{
    if (rdbEnableCtypeYes.Checked)
    {
        lblSelectContentType.Visible = true;
        frmSelectContentType selectContentType = new frmSelectContentType();
        selectContentType.rootWebUrl = rootWebUrl;
        selectContentType.MdiParent = this.MdiParent;
        selectContentType.StartPosition = FormStartPosition.CenterScreen;
        selectContentType.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        selectContentType.Show();
    }
    else
    {
        lblSelectContentType.Visible = false;
        cmbContentType.Visible = false;
    }
} 

选择内容类型的表单代码是:

public string rootWebUrl = string.Empty;
XDocument contentTypeFile = XDocument.Load(FilePaths.ContentTypesFilePath);
private void frmSelectContentType_Load(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(rootWebUrl))
    {
        if (contentTypeFile != null)
        {
            XElement xSiteCollection = contentTypeFile.Descendants(XmlElements.SiteCollection).Where(x => x.Attribute(XmlAttributes.Url).Value.Equals(rootWebUrl)).FirstOrDefault();
            if (xSiteCollection != null)
            {
                IEnumerable<XElement> xContentTypes = xSiteCollection.Descendants(XmlElements.ContentType);
                if (xContentTypes.OfType<XElement>().Count() > 0)
                {
                    foreach (XElement xContentType in xContentTypes)
                    {
                        ComboboxItem item = new ComboboxItem();
                        item.Text = xContentType.Attribute(XmlAttributes.Name).Value;
                        item.Value = xContentType.Attribute(XmlAttributes.Id).Value;
                        lstContenType.Items.Add(item);                                
                    }
                }
            }
        }
    }
}

我该怎么办?

【问题讨论】:

  • Now the form will be hidden.... 表单是隐藏还是关闭?一些代码可以显示您如何启动第二种形式,这将有助于为您提供正确的答案。
  • 先生,我正在编辑我的问题。

标签: c# winforms desktop-application


【解决方案1】:

试试这个。

选择内容类型表单

private List<string> _selectedItems = new List<String>();
public List<string> SelectedItem
{
    get {return _selectedItems;}
}

private void btnSelect_Click(object sender, EventArgs e)
{
    for(int i=0; i<lst.Items.Count;i++)
    {
        if (lstContenType.GetItemCheckState(i) == CheckState.Checked)
            _selectedItems.Add(lst.Items[i].ToString());
    }
    this.Close();
}

创建列表表单

private void rdoEnableCntType_Checked(object sender, EventArgs e)
{
    if (rdoEnableCntType.Checked = true)
    {
        FrmConentType frm = new FrmConentType();        
        frm.ShowDialog();
        List<string> list = frm.SelectedItems;
        //Place your code to use selected items
    }
}

【讨论】:

  • 感谢您的回答,先生,它在一些编辑条件下工作得很好。 :-)
【解决方案2】:

您可以在 FormB 的构造函数中传递数据。例如:

//Default Constructor
public FormB()
{
         InitializeComponent();
}

// Overloaded Constructor
public  FormB(string parameter1, string parameter2, string parameter3)
{
         InitializeComponent();
}

例如:

public FormB(bool RbuttonChecked)
{
    InitializeComponent();
    if(RbuttonChecked)
    {
        //Code
    }
}

像这样从 FormA 调用 FormB:

FormB obj=new FormB(Rbtn.checked); //Invoking the overloaded constructor
obj.Show();

【讨论】:

  • 我想访问从“选择内容类型”表单到“创建列表”表单中的选定项目。即首先打开“创建列表”表单,然后在“选择内容类型”表单中的复选框列表中选择值后打开“选择内容类型”表单我想在“创建列表”中访问它们。
【解决方案3】:

您可以在主窗体中定义一个列表来存储用户在“选择内容类型”窗口中选择的项目。当用户完成“选择内容类型”窗口后,您将选定的项目添加到变量中。

在表单“创建列表”类中:

public List<ContentType> selectedContentTypes = new List<ContentType>();

当用户在“选择内容类型”表单中按下“选择”按钮时,将项目添加到变量中:

// For each selected items in the listbxo {
    frmCreateList.selectedContentTypes.Items.Add(itemId);
// }

如果你能发布你的源代码,那会更容易提供帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    相关资源
    最近更新 更多