REZR.AMX
REZR.AMX

Reputation: 59

Move TextBox Values to Other

I have a winform C# app, in which i have a issues. First here is a image of my form.(it is not final yet. So alignment is not set properly.) Image.

here is my problem. there are 36 textboxes (enclosed within the red area.) They will hold max 12 values but not sequentially i.e one after the other. Now in the Blue Area is How i want the 12 values which will be filled in the red area.

I've searched thoroughly, but could not find anything concrete. i wrote a code that will first check if the textbox is free or not, if free copy the textbox value from red area to the blue(in a broad way.), under the switch button event. it only copies the last few textboxes from here to there.

that there are 36 textboxs in red area and only 12 in the blue, i have no idea how to go about further.

Any and all help is appreciated.

Upvotes: 0

Views: 239

Answers (2)

Adriano Repetti
Adriano Repetti

Reputation: 67148

There at least two ways to achieve that result.

1) You can group your text boxes using panels, one panel for text boxes filled by user and another one for text boxes with copied values. First get values from first group:

private static IEnumerable<string> GetValues(Panel container)
{
    return container.Controls
        .OfType<TextBox>()
        .Select(x => x.Text)
        .Where(x => !String.IsNullOrWhiteSpace(x));
}

Now you have an enumeration with strings from first group, let's put them in target controls:

private static void FillWith(Panel container, IEnumerable<string> values)
{
    Queue<string> queue = new Queue<string>(values);
    foreach (var textbox in container.Controls.OfType<TextBox>())
    {
        // Not enough values to fill all textboxes
        if (queue.Count == 0)
            return;

        textbox.Text = queue.Dequeue();
    }
}

Used like this:

FillWith(pnlTarget, GetValues(pnlInputs));

2) Use some convention for names (for example name all textboxes with user inputs as txtInputXYZ, where XYZ is an incrementing number, and all outputs with txtOutputXYZ). Just loop thorugh them like this:

for (int inputIndex = 0, outputIndex = 0; inputIndex < numberOfInputs; ++inputIndex)
{
    string value = ((TextBox)Controls[String.Format("txtInput{0}", inputIndex)]).Text;
    if (String.IsNullOrWhiteSpace(value))
        continue;

    var target = (TextBox)Controls[String.Format("txtOutput{0}", outputIndex++)];
    if (target == null)
        break; // No more targets

    target.Text = value;
}

If you don't know number of input and output textboxes (because they're dynamically added) or simply because you don't want to hard-code that (good) then:

for (int inputIndex = 0, outputIndex = 0; ; ++inputIndex)
{
    var source = Controls[String.Format("txtInput{0}", inputIndex)];
    if (source == null)
        break; // No more sources

    string value = ((TextBox)source).Text;
    if (String.IsNullOrWhiteSpace(value))
        continue;

    var target = (TextBox)Controls[String.Format("txtOutput{0}", outputIndex++)];
    if (target == null)
        break; // No more targets

    target.Text = value;
}

Both methods can be used as idea, you don't actually need to use LINQ (in case you're running on older .NET version, for example). In first method you can also skip Queue (but it's such micro-optimization...) and you may manually iterate through the enumeration (returning when end is reached). Moreover second method isn't tied to names, you can decorate them somehow (an object in the Tag property?) to understand who is who then names can be anything you want.

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101731

You can use LINQ to get your textboxes.

First you need to give names to your Textboxes then identify them and distinguish between red area and blue area using their name.

For example, the name of the textboxes in red area could start with Red, blue are start with Blue.(this is horrible of course, I'm saying for example, you don't do that give more meaningful names to your textboxes).

After you do that here is the solution:

var values = this.Controls
            .OfType<TextBox>()
            .Where(x => x.Name.StartsWith("Red") && x.Text != string.Empty)
            .Select(x => x.Text)
            .ToList();

int i = 0;
foreach (var txtBox in this.Controls.OfType<TextBox>()
            .Where(x => x.Name.StartsWith("Blue")))
{
    txtBox.Text = values[i++];
}

Upvotes: 0

Related Questions