0

I want to be able to call the following function multiple times through out my code to fill different groups of 8 text boxes in my form.

Right now reference is being passed in "tbPlay" from where it is being called initially in the code.

Each time this function will be called it will be to fill different text box groups.

I am trying to think of a way of using the empty for loop to create the necessary variable names to replace tbPlay0-7 in my case statement, so it isn't only usable for one group of text boxes in my code. I am not sure it can be done.

Can anyone help.

    private void convertBasetoDrawn(string numBase, string reference)
    {
        string baseNumber = numBase;

        for (int i = 0; i < 8; i++)
        {
            //some code here to create variables to replace the text box names in the
            //following case statement
        }

        switch (baseNumber)
        {
            case "000":
                tbPlay0.Text = "000";
                tbPlay0.ForeColor = Color.Red;
                tbPlay1.Text = "500";
                tbPlay2.Text = "050";
                tbPlay3.Text = "005";
                tbPlay4.Text = "550";
                tbPlay5.Text = "505";
                tbPlay6.Text = "055";
                tbPlay7.Text = "555";
                tbPlay7.ForeColor = Color.Red;
                break;
        }


    }
3
  • It's been forever since I did any Windows Forms stuff, assuming that's what this is, but isn't there some ContainerControl.FindControl or FindChild or Controls.Find or something to that effect that lets you go from a string name to an actual control? You could do that. Commented Nov 13, 2014 at 8:20
  • 2
    Can't you use an array or a list of TextBox? Commented Nov 13, 2014 at 8:21
  • Ahmad's suggestion is the way to go: Create a List<TextBox> for each group and pass it to the function! You could use the name, as feiyun0112 suggestes, but having nicely named groups is far better! (More readably, much more flexible, faster...) Commented Nov 13, 2014 at 9:01

2 Answers 2

1

Create a List<TextBox> for each group:

    List<TextBox> list01 = new List<TextBox>() { tbPlay0, tbPlay1, ....};
    List<TextBox> list02 = new List<TextBox>() { ..., ... , ....};
    // ..

}

And pass such a group to the function:

private void convertBasetoDrawn(List<TextBox> list, string numBase, string reference)
{
    string[] texts = new string[8] 
                 { "000", "500", "050", "005", "550", "505", "055", "555" };

    for (int t = 0; t < list.Count; t++)  list[t].Text = texts[t];
    list[0].ForeColor = Color.Red;
    list[7].ForeColor = Color.Red;

}

Assuming the texts will always look like that. If they depend on, maybe numbase you can construct them dynamically as well, as long as you know the rules.. Maybe even a simple replacement will do the job?

You didn't use reference, btw..

Now, I'm just guessig here, but maybe this is the pattern for your texts..:

 string[] texts = new string[8] 
                  { "nnn", "dnn", "ndn", "nnd", "ddn", "dnd", "ndd", "ddd" };
 for (int s = 0; s < texts.Length; s++) 
      texts[s] = texts[s].Replace("d", numBase).Replace("n", reference);

Now you can call it like this:

convertBasetoDrawn(list01, "0","5");

Update: For the rules as I understand them now you could do:

string newText = "";
for (int s = 0; s < texts.Length; s++)
{
    newText = "";
    for (int i = 0; i < 3; i++)
    {
        if (texts[s][i] == 'n') newText += numBase[i];
        else  newText +=  (Convert.ToByte(numBase[i].ToString()) + 
                            Convert.ToByte(reference[0].ToString()) ).ToString("0");
    }
    texts[s] = newText;
}

and call it like this:

 convertBasetoDrawn(list01, "001", "5");

or

 convertBasetoDrawn(list02, "000", "1");

Note: no carry over here.. You'd have to define rules for that and code it yourself..

Sign up to request clarification or add additional context in comments.

7 Comments

I like your replace here because it could save me a lot of code, but in my mind I can't seem to make it work.
I like your replace here because it could save me a lot of code, but in my mind I can't seem to make it work. Say numBase = "000" the pattern is true and replaces correctly, but say numBase = "001" then the string array would be, "001", "501", "051", "006", "551", "506", "056", and "556". If a mask could be created to increment bases off the pattern and base number, that would be tremendously helpful.
Ah, I see. In that case we need to make some changes.. In what range will the inputs lie? Will the output only be 3 digits? Can numbase be 999 or reference = 9 ? What would the output be??
Try this instead of the replace:` string newText = ""; for (int s = 0; s < texts.Length; s++) { newText = ""; for (int i = 0; i < 3; i++) { if (texts[s][i] == 'n') newText += numBase[i]; else newText += (Convert.ToByte(numBase[i]) + Convert.ToByte(reference[0]) - 2 * Convert.ToByte('0') ).ToString("0"); } texts[s] = newText; }`
Range of numBase 000 - 999. Reference was only going to be used to Pass in the base name of the textboxes now that has disappeared in the declaration. It is now, private void convertBaseToDrawn(string numBase, List<TextBox> list)
|
0

It's not clear how you plan to identify the specific group of eight. But let's assume you have somehow.

Then, if I were writing this code, I would use a UserControl to encapsulate the repeated pattern, exposing the eight TextBox controls — or rather, the properties of them that you want access to — as properties. E.g.

class TextBoxGroup : UserControl
{
    public string Text1
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }

    public Color ForeColor1
    {
        get { return textBox1.ForeColor; }
        set { textBox1.ForeColor = value; }
    }

    public string Text2
    {
        get { return textBox2.Text; }
        set { textBox2.Text = value; }
    }

    public Color ForeColor2
    {
        get { return textBox2.ForeColor; }
        set { textBox2.ForeColor = value; }
    }

    // ...

    public string Text8
    {
        get { return textBox8.Text; }
        set { textBox8.Text = value; }
    }

    public Color ForeColor8
    {
        get { return textBox8.ForeColor; }
        set { textBox8.ForeColor = value; }
    }
}

Then in your method, rather than whatever logic you planned on using to figure the starting index for your group, instead you just retrieve the appropriate TextBoxGroup instance and use it in the switch, like this:

case "000":
    textBoxGroup.Text1 = "000";
    textBoxGroup.ForeColor1 = Color.Red;
    textBoxGroup.Text2 = "500";
    textBoxGroup.Text3 = "050";
    textBoxGroup.Text4 = "005";
    textBoxGroup.Text5 = "550";
    textBoxGroup.Text6 = "505";
    textBoxGroup.Text7 = "055";
    textBoxGroup.Text8 = "555";
    textBoxGroup.ForeColor8 = Color.Red;
    break;

A variation on the above would encapsulate the properties with setter methods taking an index. E.g.

class TextBoxGroup : UserControl
{
    // Initialized in constructor to be the eight TextBoxes
    private TextBox[] _textboxes;

    public void SetText(int i, string text)
    {
        _textboxes[i].Text = text;
    }
}

Of course, if you don't want to use a UserControl, you could just initialize a similar data structure in the main form instead, so that the controls can be accessed by index. But personally, I'd prefer the UserControl as it makes it easier to reuse and ensure consistency across all the groups of TextBox controls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.