c#在4个文本框的文本框中显示文本 [英] c# show text in textbox in 4 textboxes

查看:115
本文介绍了c#在4个文本框的文本框中显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有textbox1显示数据库中的值,值总是7个数字



我希望这7个数字显示在4个不同的文本框中,如下所示:



如果以textbox1值为例:2145576



那么其他文本框应该是这样的:



textbox2 value = 2 - textbox3 value = 14 - textbox4 value = 55 - textbox5 value = 76

i have textbox1 that shows a value from database and the values are always 7 numbers

I want these 7 numbers to be shown in 4 different textboxes as shown below :

if textbox1 value as an example are : 2145576

then the other textboxes should be like this :

textbox2 value =2 - textbox3 value = 14 - textbox4 value = 55 - textbox5 value = 76

推荐答案

这应该很容易使用String方法, String方法(系统) [ ^ ]



例如,使用SubString()方法。 String.Substring Method(Int32,Int32)(System) [ ^ ] 。你告诉它要从字符串中获取哪些字符。
This should be very easy to do using String methods, String Methods (System)[^]

For example, use the SubString() method. String.Substring Method (Int32, Int32) (System)[^]. You tell it which characters from a string to get.


你可以使用 String.Substring方法 [ ^ ]为此:

You can use the String.Substring Method[^] for this:
string original = textbox1.Text;
textbox2.Text = original.Substring(0, 1);
textbox3.Text = original.Substring(1, 2);
textbox4.Text = original.Substring(3, 2);
textBox4.Text = original.Substring(5, 2);


我会接近这样的问题,首先,创建一个小的通用工具,让我将一个字符串拆分成一堆块,其中大小每个块的数量可以变化。
I would approach a problem like this by, first, creating a small "general purpose" tool that would let me split a string into a bunch of "chunks," where the size of each chunk can vary.
private List<string> ChunkString(string source, params int[] chunksizes)
{
    // check for match between source length and total digits required
    if (source.Length != chunksizes.Sum()) throw new ArgumentException("bad argument");

    List<string> results = new List<string>();

    StringBuilder sb = new StringBuilder(source);

    char[] chunk;

    foreach (int chunksz in chunksizes)
    {
        // create a new Char[] to hold the chunk
        chunk = new char[chunksz];

        // copy #chunksz characters from StringBuilder to chunk
        sb.CopyTo(0, chunk, 0, chunksz);
        // trim #chunksz characters off the StringBuilder
        sb.Remove(0, chunksz);

        // save the current result as string
        results.Add(new string(chunk));
    }

    return results;
}

然后,在你的情况下,我可能会这样使用它:

Then, in your case, I might use it this like this:

// in some method or EventHandler
List<string> results = ChunkString("2145576", 1, 2, 2, 2);
// assuming you have references to the four TextBoxes in a TextBox Array named 'tbAry:

TextBox[] tbAry = new TextBox[]{textBox1,textBox2,textBox3,textBox4};

for (int i = 0; i < tbAry.Length; i++)
{
    tbAry[i].Text = results[i];
}

注意:



1.虽然我在这里使用'StringBuilder来尝试最小化新字符串的创建,但我我完全不确定这里有一个真正的回报......抓住Char []数组并将它们变成字符串的业务可能会使用'StringBuilder抵消任何收益。



2.我选择避免在这里使用Linq,但你可能会用一些Linq代码来执行此操作...这看起来很简单,直到你试图理解它的作用:)

Notes:

1. While I use a 'StringBuilder here to try and minimize the creation of new Strings, I'm not at all sure that has a real "payoff" here ... the business of grabbing Char[] arrays and turning them into strings may be offsetting any gains using a 'StringBuilder.

2. I chose to avoid using Linq here, but you could probably whip-up some Linq code to do this ... which looks simple until you try to understand what it does :)


这篇关于c#在4个文本框的文本框中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆