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

问题描述 投票:2回答:5

我试图将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;
}
c# textbox parameter-passing
5个回答
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)

0
投票

问题出在两个地方

方法名( 串 theStringVariable,tb_1,tb_2);

private void MethodName(string theStringVariable,TextBox tb_name,TextBox tb_allergen){


0
投票

Convert.ToInt32(tb_viewIngredient1)将抛出一个例外,因为你试图将TextBox控件转换为int。相反,尝试将TextTextBox属性传递给方法:

Convert.ToInt32(tb_viewIngredient1.Text)

0
投票

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

(Convert.ToInt32(tb_viewIngredient1.Text)

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";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.