[英]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.