【问题标题】:Form doesn't show anything表格不显示任何内容
【发布时间】:2013-03-03 20:30:33
【问题描述】:

我是 Winforms 和 C# 的新手,所以这听起来像是一个愚蠢的问题。我有如下所示的类,用于创建要显示为模式对话框的表单。

class FrmDelivery : Form
{
    ListBox s;
    public FrmDelivery()
    {
        s = new ListBox();

        s.DataSource = new List<int>(){1,2,3,4};
        s.Update();
        s.Show();

    }

} 

但是,当我使用 ShowDialogmethod 显示此表单时,它没有显示任何内容。我应该怎么做才能在这个表单中添加一个列表框?

编辑:

我用代码来显示表单:

       FrmDelivery frm = new FrmDelivery();
        frm.ShowDialog();

【问题讨论】:

  • 您正在尝试显示列表框而不是表单。
  • 请看我的编辑@ChrisF 我认为它与你所说的有关
  • s.Show(); 在您的 FrmDelivery 构造函数中。那是错误的。您也没有将列表框添加到可视化树中。如果您解释了为什么不能为此使用 XAML,它可能会有所帮助。

标签: c# winforms dialog


【解决方案1】:

注意 - WPF 使用 Windows,而不是 Forms,所以我不清楚您为什么要从 Form 而不是 Window 派生。但我会像您在谈论 WPF Window 作为您的“表单”一样回答。

首先,需要一些东西来显示窗口。目前,提供的代码不显示窗口,它试图显示ListBox

其次,您需要向窗口添加一个 LayoutPanel 并将您的 ListBox 添加为布局面板的子项。布局面板有多种风格,例如 GridsStackPanelsCanvases,具体取决于您想要的布局类型。

或者,您可以将WindowContent 设置为您的ListBox。这意味着Window 上唯一的东西就是你的ListBox', so if you want multiple visual elements on yourWindow`,你需要使用布局面板。

第二种方法看起来像

this.Content = s;

对于第一种方法,我建议阅读 WPF 中的布局面板。 Here is one tutorialhere 是 MSDN 关于布局的主题。谷歌搜索会产生更多结果。

【讨论】:

  • 对不起,我的意思是 WinForms 我的错.. 我不认为它有内容属性
  • +1 表示答案 - 即使它不再适用,它也很好地回答了原来的问题。
  • 问题是关于 WinForms 而不是 WPF
  • @Phil,最初他将其标记为 WPF,因此造成了混乱。
  • 不客气——我想如果没有人提到这一点,你以后会被否决;)
【解决方案2】:

我建议您使用 Add|New Item|Windows Form 创建一个新表单。 然后,您将获得一个设计图面,您可以在其中添加一个列表框,并生成将正确初始化您的表单和列表框的代码。特别是您的表单和列表框将获得它们当前没有的默认大小。

您的代码(例如 Form1.cs)将与此类似:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.listBox1.DataSource = new List<int> { 1, 2, 3, 4 };
    }

    public int? SelectedValue
    {
        get
        {
            return (int?)this.listBox1.SelectedValue;
        }
    }
}

另外,Form1.Designer.cs 中会有很多类似于

的代码
....

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        this.listBox1.Location = new System.Drawing.Point(30, 37);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(120, 95);
        this.listBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 261);
        this.Controls.Add(this.listBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion

你可以像这样使用你的表单:

    private void button1_Click(object sender, System.EventArgs e)
    {
        using (var form = new Form1()) // you should dispose forms used as dialogs
        {
            if (DialogResult.OK == form.ShowDialog()) // optional (you could have OK/Cancel buttons etc
            {
                Debug.WriteLine(form.SelectedValue ?? -1);
            }
        }
    }

【讨论】:

  • 如果你不介意我问:我该如何做到,以便当用户从列表框中选择一个值时,我可以从调用ShowDialog 方法的位置获取值?
  • 你可能应该问另一个问题 - 但我已经更新了我的答案。
【解决方案3】:

您不仅应该将控件添加到集合中,还应该设置他的特征。 至少大小和位置。

class FrmDelivery : Form
{
ListBox s;
public FrmDelivery()
{
    s = new ListBox();
    s.Location = new System.Drawing.Point(0, 0); //relative to the parent control (not an absolute value, so)
    s.Name = "listBox1";
    s.Size = new System.Drawing.Size(120, 95);


    s.DataSource = new List<int>(){1,2,3,4};
    this.Controls.Add(s); //it will add it to the form but you can add it to another control, like panel.

}

}

希望对你有帮助

【讨论】:

    【解决方案4】:

    您需要将列表框添加到控件集合中:

    ListBox s;
    public FrmDelivery()
    {
      s = new ListBox();
      s.DataSource = new List<int>() { 1, 2, 3, 4 };
    
      this.Controls.Add(s);
    }
    

    这将为您将控件放到您的表单上,尽管您可能想要设置许多其他属性(例如,让它看起来像您想要的那样) - 正如其他人所提到的,您可以看到设计器是如何通过将列表框放在表单上并检查生成的代码,在后面的代码中执行此操作。

    【讨论】:

      【解决方案5】:

      请查看您是否将默认构造中的 InitializeComponent() 注释掉了。它通常会在 FormLoad 上初始化表单的所有控件。

      【讨论】:

        猜你喜欢
        • 2020-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 2020-11-10
        相关资源
        最近更新 更多