簡體   English   中英

列表框未顯示

[英]Listbox not showing

因此,這可能很容易,但是我才剛剛開始學習C#的精妙技巧,所以如果遇到一點“綠色”,請原諒我。

我一直在嘗試盒子。 准確地說,最近有ListBoxes。 在我的課堂上,我有以下幾點:

ListBox lb = new ListBox();

lb.Location = new System.Drawing.Point(12, 12);
lb.Name = "ListBox1";
lb.Size = new System.Drawing.Size(245, 200);
lb.BackColor = System.Drawing.Color.Blue;
lb.ForeColor = System.Drawing.Color.Green;  

lb.Items.Add("Element One");
lb.Items.Add("Element Two");
lb.Items.Add("Element Two");

lb.Show();

現在,我一直在假設ListBox可以像MessageBoxes一樣工作。

我有一個:

var confirmResult = MessageBox.Show("Question asking about " + variable + "?", "TitleHere", MessageBoxButtons.YesNo);

...而且我認為ListBox可以正常工作嗎?

問題是,事實並非如此。

MessageBox的所有功能都會彈出,但是ListBox卻無處可見。

我想念什么嗎?

更新:

好的,所以Form是他們說的方法。

我試過了:

ListBox lb = new ListBox();

lb.Location = new System.Drawing.Point(12, 12);
lb.Name = "ListBox1";
lb.Size = new System.Drawing.Size(245, 200);
lb.BackColor = System.Drawing.Color.Blue;
lb.ForeColor = System.Drawing.Color.Green;  

lb.Items.Add("Element One");
lb.Items.Add("Element Two");
lb.Items.Add("Element Two");

Form f = new Form();
f.Controls.Add(lb);

Buuut,這仍然沒有顯示我的漂亮盒子。

請指教。

您需要將列表框添加到表單上的容器(例如面板或表單本身)。

例如

MyForm.Controls.Add(lb)

要么

panel1.Controls.Add(lb)

你不需要做

lb.Show();

如在Microsoft msdn上所寫, 消息框

顯示一個消息框,其中可以包含用於通知和指示用戶的文本,按鈕和符號。

ListBox

表示一個Windows控件以顯示項目列表。

因此,您不需要一個容器(例如Form)來顯示messageBox,而是需要一個用於控件的容器(例如ListBox)。

更新:

您可能還應該添加以下內容:

form.ShowDialog();

除了Microsoft的ListBox頁面之外,您還應該查看有關Form類的內容 如果滾動到頁面底部,它們將提供簡潔的示例。

暫無
暫無

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

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