2011-06-13 57 views
1

當我運行了一個程序,把它給我的值(是的,我確實有在下拉選擇列表中的項目)在組合框中我得到的是這樣的,爲什麼組合框會返回錯誤的值?

System.Windows .Forms.ComboBox + ObjectCollection

這是代碼我使用

Dim name As String 
     name = cmbworld.Text 
     MsgBox(name) 

任何想法?

P.S.我用來插入值的代碼是

cmbworld.Items.Clear() 

    If File.Exists(root + "\setting\world.txt") Then 
     For Each line As String In File.ReadLines(root + "\setting\world.txt") 
      If line.Length <> 0 Then 
       cmbworld.Items.Add(line) 
      End If 
     Next line 
    Else 
+1

你可以發表代碼,你把值放到組合框? – Tridus 2011-06-13 21:23:38

+0

@Tridus現在是 – Kuzon 2011-06-13 21:27:09

回答

3

此代碼將重現該問題:

Dim name As String 
    name = cmbworld.Items.ToString() 
    MsgBox(name) 

你有一些其他代碼的地方,被錯誤地分配Text屬性的值。你需要索引Items集合。例如:

cmbworld.Text = cmbWorld.Items(0) 
+0

對不起,我仍然感到困惑......我如何從組合框中獲得價值。我使用上面的代碼在下拉列表中分配了這些項目。這是代碼中的錯誤嗎?我的意思是放置「cmbworld.Text = cmbWorld.Items(0)」 – Kuzon 2011-06-14 11:31:07

+1

否,您發佈的代碼很好。我必須假設你的程序中有其他代碼指定了Text屬性錯誤。我不知道你是否確實這是一個猜測。唯一有意義的東西。如果它沒有幫助你,請取消標記答案。 – 2011-06-14 12:34:56

+0

現在它的作品謝謝:) – Kuzon 2011-06-15 02:30:22

3

您發佈的代碼不能是您的代碼中無法使用的代碼。

以您的示例爲例,我從我的文本文件中獲得一行乾淨的消息,其中包含一行數據。

我得到你的消息,唯一的辦法是,當我做到以下幾點:

MessageBox.Show(cmbworld.Items.ToString) 

我會把對MSGBOX線停止調試器,檢查值。

0

您正在使用錯誤的屬性,使用SelectedText。

cmbworld.SelectedText

相關問題