[英]How can I call this method for retrieved data in C#?
我正在開發一個程序,該程序使您可以從下拉框中選擇客戶ID。 選擇客戶ID后,將從CSV文件中提取客戶的信息並顯示在文本框中。
電話號碼信息未格式化,但我希望將其顯示為格式化(例如(800)674-3452)。 我已經為此編寫了一個方法,但是我不確定如何調用它。 你能幫忙嗎?
-抱歉,這是一個愚蠢的問題。 我還在學習。
private void idBox_SelectedIndexChanged(object sender, EventArgs e)
{
try // catch errors
{
string selectedCustomer; // variable to hold chosen customer ID
selectedCustomer = idBox.Text; // retrieve the customer number selected
chosenIndex = 0;
bool found = false; // variable if customer ID was found
while (!found && chosenIndex < allData.Length) // loop through the 2D array
{
if (allData[chosenIndex, 0] == selectedCustomer) // make sure it's the right customer
{
found = true; // Yes (true) found the correct customer
}
chosenIndex++; // add one row
}
chosenIndex -= 1; // subtract one because add 1 before exiting while
/* 0 = customer ID
* 1 = name
* 2 = address
* 3 = city
* 4 = state
* 5 = zip
* 6 = phone
* 7 = email
* 8 = charge account - yes/no
* 9 = good standing - yes/no
*/
nameBox.Text = allData[chosenIndex, 1]; // put name in nameBox
addressBox.Text = allData[chosenIndex, 2]; // put address in addressBox
cityBox.Text = allData[chosenIndex, 3]; // put city in cityBox
stateBox.Text = allData[chosenIndex, 4]; //puts state in stateBox
zipBox.Text = allData[chosenIndex, 5]; // puts zip in zipBox
phoneBox.Text = allData[chosenIndex, 6]; // puts phone number in phoneBox
emailBox.Text = allData[chosenIndex, 7]; // puts email in emailBox
if (allData[chosenIndex, 8] == "Yes") // check if charge account
{
yesChargeRadio.Checked = true; // true if Yes
}
else // otherwise
{
noChargeRadio.Checked = true; // true if No
}
if (allData[chosenIndex, 9] == "Yes") // check for good standing
{
yesStandingRadio.Checked = true; // true if Yes
}
else // otherwise
{
noStandingRadio.Checked = true; // true if No
}
}
catch (Exception errorInfo) // catch error
{
MessageBox.Show("errors: " + errorInfo, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error); // error message
}
}
這是檢查長度和格式的方法:
private bool numberCheck(string str)
{
const int NUMBER_LENGTH = 10;
bool valid = true;
if (str.Length == NUMBER_LENGTH)
{
foreach (char ch in str)
{
if (!char.IsDigit(ch))
{
valid = false;
}
}
}
else
{
valid = false;
}
return valid;
}
private void formatPhone(ref string str)
{
str = str.Insert(0, "(");
str = str.Insert(4, ")");
str = str.Insert(8, "-");
}
您的代碼幾乎完成了。 您需要做的是,在設置phoneBox.Text
之前,可以調用以下方法:
if(numberCheck(allData[chosenIndex, 6]))
{
formatPhone(ref allData[chosenIndex, 6]);
}
phoneBox.Text = allData[chosenIndex, 6];
當您使用帶有ref參數的方法時,格式化的文本將在您的Arary中更新,然后您可以將其分配給phoneBox
我希望我理解您具體要問的部分:調用定義的方法就像靜態方法IsDigit或MessageBox.Show一樣,只不過您不需要在方法名前加上名稱和句點,因為方法是調用它的對象的一部分。
因此,例如,如果我有一個方法:
public void ShowSomething()
{
MessageBox.Show("stuff");
}
在課堂上,我可以這樣稱呼它:
ShowSomething();
要傳遞參數,我將在括號中列出它們,例如,與MessageBox.Show一樣。
您可以將value用作numberCheck
這樣的方法numberCheck
返回其他布爾值,因此您可以執行以下任一操作:
bool b = numberCheck(someString);
if (numberCheck(someString))
{
//Do something, like displaying the phone number
}
此MSDN文檔可能會幫助您: http : //msdn.microsoft.com/zh-cn/library/ms173114.aspx
這是您要找的東西嗎? :
.......
phoneBox.Text = numberCheck(allData[chosenIndex, 6]) ?
formatPhone(allData[chosenIndex, 6]) :
allData[chosenIndex, 6];
.......
private string formatPhone(string str)
{
str = str.Insert(0, "(");
str = str.Insert(4, ")");
str = str.Insert(8, "-");
return str;
}
上面的代碼將檢查電話數據的有效性,如果有效,則將phoneBox.Text
設置為格式化的電話號碼,否則將phoneBox.Text
設置為原始未格式化的電話數據。
如果您不熟悉三元運算符( ?
),以供參考 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.