简体   繁体   English

如何在C#中为获取的数据调用此方法?

[英]How can I call this method for retrieved data in C#?

I'm working on a program that allows you to select a customer ID from a dropdown box. 我正在开发一个程序,该程序使您可以从下拉框中选择客户ID。 Once the customer ID is selected, the customer's information is pulled from a CSV file and displayed in textboxes. 选择客户ID后,将从CSV文件中提取客户的信息并显示在文本框中。

The phone number information is unformatted, but I want it to be displayed formatted (ex. (800)674-3452). 电话号码信息未格式化,但我希望将其显示为格式化(例如(800)674-3452)。 I have written a method for this, but I'm not sure how to call it. 我已经为此编写了一个方法,但是我不确定如何调用它。 Can you please help? 你能帮忙吗?

-Sorry if this is a dumb question. -抱歉,这是一个愚蠢的问题。 I'm still learning. 我还在学习。

    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
        }

    }

Here is the method(s) to check the length and format: 这是检查长度和格式的方法:

    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, "-");
    }

You are almost done with your code. 您的代码几乎完成了。 What you need to do is, before you set your phoneBox.Text you can call the method as below: 您需要做的是,在设置phoneBox.Text之前,可以调用以下方法:

if(numberCheck(allData[chosenIndex, 6]))
{

    formatPhone(ref allData[chosenIndex, 6]);
}

phoneBox.Text = allData[chosenIndex, 6]; 

As you have your method with ref parameter, the formatted text will be updated in your arary and you can then assign it to your phoneBox 当您使用带有ref参数的方法时,格式化的文本将在您的Arary中更新,然后您可以将其分配给phoneBox

I hope I understand what part specifically you're asking about: you call methods you define just like the static methods IsDigit or MessageBox.Show, except you do not need to prefix the method name with a name and then a period because the method is part of the object calling it. 我希望我理解您具体要问的部分:调用定义的方法就像静态方法IsDigit或MessageBox.Show一样,只不过您不需要在方法名前加上名称和句点,因为方法是调用它的对象的一部分。

So, for example if I had a method: 因此,例如,如果我有一个方法:

public void ShowSomething()
{
     MessageBox.Show("stuff");
}

From within the class, I could call it like this: 在课堂上,我可以这样称呼它:

ShowSomething();

To pass parameters, I would list them in the parenthesis, as you do with MessageBox.Show, for example. 要传递参数,我将在括号中列出它们,例如,与MessageBox.Show一样。

You can use the value a method like numberCheck returns as any other boolean, so you could do any of these: 您可以将value用作numberCheck这样的方法numberCheck返回其他布尔值,因此您可以执行以下任一操作:

bool b = numberCheck(someString);
if (numberCheck(someString))
{
    //Do something, like displaying the phone number
}

This MSDN document might help you: http://msdn.microsoft.com/en-us/library/ms173114.aspx 此MSDN文档可能会帮助您: http : //msdn.microsoft.com/zh-cn/library/ms173114.aspx

Is this what you're looking for? 这是您要找的东西吗? :

.......
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;
}

Codes above will check validity of phone data, if it is valid set phoneBox.Text to formatted phone number, else set phoneBox.Text to raw unformatted phone data. 上面的代码将检查电话数据的有效性,如果有效,则将phoneBox.Text设置为格式化的电话号码,否则将phoneBox.Text设置为原始未格式化的电话数据。

For reference in case you're not familiar with ternary operator ( ? ). 如果您不熟悉三元运算符( ? ),以供参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM