[英]C# Convert String from Combo Box to Int and pass it
我已經聲明了一個變量“字符串文本”
在另一個函數中,我使用一個函數從ComboBox中獲取值:
comboBox1.Text = gUI.getText();
和
public string getText()
{
return text;
}
並將值文本作為參數傳遞給程序。
如何將ComboBox中的文本更改為Integers? 您可以選擇兩種狀態:“是”和“否”。 我想將“是”設置為1,將“否”設置為0傳遞給程序。 如何處理?
您要返回一個整數,具體取決於您在ComboBox中選擇了什么項目? 您是否嘗試過SelectedIndex
屬性:
public int GetText()
{
return comboBox1.SelectedIndex;
}
如果您的ComboBox包含項目No
, Yes
和Maybe
,則選擇No
結果將為0
,如果選擇Yes
結果將為1
,如果選擇Maybe
結果將為2
,等等。
好吧 ,不清楚,但是我懷疑你想要這樣的東西;
public int getText(string str)
{
if(str == "Yes")
return 1;
if(str == "No")
return 0;
return -1;
}
ComboBox
.Text
屬性需要string
類型,這就是為什么您需要使用這些整數的字符串表示形式的原因(或使用.ToString()
方法);
comboBox1.Text = gUI.getText().ToString();
否,我想從ComboBox中捕獲字符串,如果為“是”,則保存值1並將其作為參數傳遞給程序。 與“否”相同
如果我理解正確,您想檢查comboBox1
的文本,例如:
public int getText()
{
if(comboBox1.Text == "Yes")
return 1;
if(comboBox1.Text == "No")
return 0;
return -1;
}
但是將這些值保存到哪里並將其作為參數傳遞給哪個程序?
如果您仍然想返回字符串,可以使用like;
public string getText()
{
if(comboBox1.Text == "Yes")
return "1";
if(comboBox1.Text == "No")
return "0";
return "-1";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.