是否可以将文本框作为参数传递?

时间:2019-03-28 22:07:45

标签: c# textbox parameter-passing

我试图将2个文本框的名称传递给方法,以便它编辑其中的文本。我尝试过在线查找示例,但只能找到尝试通过文本框传递文本的人。

我尝试通过在方法构造函数中声明文本框来传递它。

MethodName(string text, tb_1, tb_2);

private void MethodName(string str, TextBox tb_name, TextBox tb_allergen)
{
    string ingredientName = "";
    string ingredientAllergen = "";
    //code to change strings//
    tb_name.Text = ingredientName;
    tb_allergen.Text = ingredientAllergen;
}

运行代码后,我希望将文本框文本更改为适当的值,而不是在调用中遇到有关文本框的错误。

  

“ mscorlib.dll中发生了'System.InvalidCastException'类型的未处理的异常

     

其他信息:无法将类型为'System.Windows.Forms.TextBox'的对象转换为类型为'System.IConvertible'“

如果对此有一个简单的解决方法,请对不起,但是请向我指出正确的方向。预先感谢。

真实代码

ingredientDBAccess ingredientDBA = new ingredientDBAccess(db);

populateBoxesWithIngredientResults( ingredientDBA.getIngredientsFromID(Convert.ToInt32(tb_viewIngredient1)), tb_viewIngredient1, tb_viewAllergen1);

private void populateBoxesWithIngredientResults(List<ingredient> ingredientList, TextBox tb_name, TextBox tb_allergen)
{
    string ingredientName = "";
    string ingredientAllergen = "";
    foreach (ingredient ingredient in ingredientList)
    {
        string name = Convert.ToString(ingredient.IngredientName);
        ingredientName = name;
        string allergen = "N/A";
        switch (ingredient.AllergenID)
        {
            case 0:
                allergen = "N/A";
                break;
            case 1:
                allergen = "Nut";
                break;
            case 2:
                allergen = "Gluten";
                break;
            case 3:
                allergen = "Dairy";
                break;
            case 4:
                allergen = "Egg";
                break;
        }
        ingredientAllergen = allergen;
    }
    tb_name.Text = ingredientName;
    tb_allergen.Text = ingredientAllergen;
}

5 个答案:

答案 0 :(得分:3)

是的,有可能:

void MyMethod(string str, TextBox txt)
{
     txt.Text = str + " some text from the method itself";
}

您甚至可以返回TextBox:

TextBox MyFunc(string str)
{
    TextBox txt = new TextBox();
    txt.Text = str;
    return txt;
}

您正在尝试将TextBox转换为Int32: Convert.ToInt32(tb_viewIngredient1) 这是Int32无法解析的。您可以将其文本转换为int32(如果它具有数字值并且可以解析),例如:

int.Parse(tb_viewIngredient1.Text)

Conver.ToInt32(tb_viewIngredient1.Text)

答案 1 :(得分:0)

问题出在两个地方

MethodName( string theStringVariable,tb_1,tb_2);

private void MethodName(字符串 theStringVariable ,TextBox tb_name,TextBox tb_allergen) {

答案 2 :(得分:0)

Convert.ToInt32(tb_viewIngredient1)将引发异常,因为您正尝试将TextBox控件转换为int。而是尝试将Text的{​​{1}}属性传递给方法:

TextBox

答案 3 :(得分:0)

问题出在(Convert.ToInt32(tb_viewIngredient1)中,您必须将其转换为:

(Convert.ToInt32(tb_viewIngredient1.Text)

答案 4 :(得分:0)

我在这里看到三个不同的选项。根据您的需求,这些方法甚至比固定代码更好。它们都解决了两点:

  1. 您可以将查找表用于过敏原而不是开关。生成的代码更短/更简单,应该运行得更快。
  2. 您可以循环浏览ingredientList中的每个项目,但是文本框将仅保留列表中最后一个项目的数据。要么只看最后一个项目(不需要循环),要么使用列表中的所有项目(即:创建csv字符串)。循环很浪费,并且使代码复杂化。

private void populateBoxesWithIngredientResults(IEnumerable<ingredient> ingredientList, TextBox tb_name, TextBox tb_allergen)
{
    string nameDelimiter = "";
    string allergenDelimiter = "";

    string ingredients = "";
    string allergens = "";

    var allergenTable = {"N/A", "Nut", "Gluten", "Dairy", "Egg"};

    foreach (ingredient ingredient in ingredientList)
    {
        //Is Convert.ToString() really needed here?
        // I feel like ingredient.IngredientName is ALREADY A STRING
        ingredients += delimiter + Convert.ToString(ingredient.IngredientName);
        nameDelimiter = ",";

        if (ingredient.AllergenID > 0 && ingredient.AllergenID < allergenTable.Length)
        {
            allergens += allergenDelimiter + allergenTable[ingredient.AllergenID];
            allergenDelimiter = ",";
        }
    }
    if (allergens == "") allergens = "N/A";

    tb_name.Text = ingredients;
    tb_allergen.Text = allergens;
}

private void populateBoxesWithIngredientResults(IEnumerable<ingredient> ingredientList, TextBox tb_name, TextBox tb_allergen)
{       
    tb_name.Text = string.Join(",", ingredientList.Select(i => i.IngredientName));

    var allergenTable = {"N/A", "Nut", "Gluten", "Dairy", "Egg"};
    var allergens = ingredientList.
         Select(i => (i.AllergenID > 0 && i.AllergenID < allergenTable.Length)? allergenTable[i.AllergenID]):"").
         Where(i => i.Length > 0);
    var result = string.Join(",", allergens);
    if (string.IsNullOrEmpty(result)) result = "N/A";

    tb_allergen.Text = result;
}

private void populateBoxesWithIngredientResults(List<ingredient> ingredientList, TextBox tb_name, TextBox tb_allergen)
{    
    if (ingredientList.Length == 0)
    {
        tb_name.Text = "";
        tb_allergen.Text = "";
    }

    var allergenTable = {"N/A", "Nut", "Gluten", "Dairy", "Egg"};
    var ingredient = ingredientList[ingredientList.Count - 1];

    tb_name.Text = ingredient.IngredientName;  
    if (ingredient.AllergenID >= 0 && ingredient.AllergenID < allergenTable.Length)
    {
        tb_allergen.Text = allergenTable[ingredient.AllergenID];
    }
    else
    {
        tb_allergen.Text = "N/A";
    }
}