简体   繁体   English

如何在WPF C#中获取多文本框作为方法的输入参数

[英]How to get multi textbox as input parameter of a Method in WPF C#

I'm using WPF C# Visual Studio我正在使用 WPF C# Visual Studio

I have a method in my public class.我的公共课上有一个方法。 I want to get many textboxes in the input of parameter in a method.我想在方法的参数输入中获得许多文本框。 That method will remove zero at first of length textbox like this: 0123 => 123该方法将在第一个长度文本框处删除零,如下所示:0123 => 123

here is my method:这是我的方法:

public static void Remove_Zero_atFirst(TextBox TXB2)
{
    TXB2.Text = TXB2.Text.TrimStart('0').Trim();
}

What I need is something like this when I want to use that method:当我想使用该方法时,我需要的是这样的:

Remove_Zero_atFirst(textbox1,textbox2,textbox3, . . .);

What have I tried:我试过什么:

public static class Texchanger
{
     public static void Del_Multi_SepTexs(IEnumerable<TextBox> TXBS)
     {
         TXBS.Text = TXBS.Text.TrimStart('0').Trim();
     }
}

In that what I tried line have error:在我尝试的行中有错误:

does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'IEnumerable<TextBox>' could be found

You need to either pass in an array, or use varargs by specifying as last parameter您需要传入一个数组,或者通过指定为最后一个参数来使用可变参数

params TextBox[] boxes参数 TextBox[] 框

And then inside.. foreach(TextBox tb in boxes) { }然后在里面.. foreach(TextBox tb in boxes) { }

try this:试试这个:

       public static class Texchanger
        {
            public static void Del_Multi_SepTexs(IEnumerable<TextBox> TXBS)
            {
                foreach (var item in TXBS)
                {
                    item.Text = item.Text.TrimStart('0').Trim();

                }

            }

        }

to call this method use:调用此方法使用:

var myTextBoxes = new List<TextBox>(){txt1,txt2}; Texchanger.Del_Multi_SepratTexs(myTextBoxes); 

or use like this with multiple parameters:或者像这样使用多个参数:

ublic static class Texchanger
    {
        public static void Del_Multi_SepTexs(params TextBox[] TXBS)
        {
            foreach (var item in TXBS)
            {
                item.Text = item.Text.TrimStart('0').Trim();

            }

        }

    }

For calling method use following:对于调用方法使用以下:

Texchanger.Del_Multi_SepTexs(txt1,txt2);

That is exactly what the params keyword is for.这正是params 关键字的用途。

Remove_Zero_atFirst(textbox1,textbox2,textbox3, . . .);

public void Remove_Zero_atFirst(params TextBox[] textboxes)
{
    foreach (TextBox item in textboxes)
    {
        item.Text = item.Text.TrimStart('0').Trim();
    }
}

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

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