GabourX
GabourX

Reputation: 283

assiging text to the correct button without switch statement

am developing an app for windows phone 8.0 in VS2012 in C#

my code works fine , but it's too long and i think there is a string format to do the same in 1 line of code the problem is that i don't know how to do that

example : i need to assign the button content text string to a the correct TextBlock by the the name of the button ( btn2 --> t2 , btn3 --> t3)

There are 8 Buttons (btn1...btn8) and 8 TextBlocks (t1.....t8)

So i have this code and it works :

private void LetterClicked(object sender, RoutedEventArgs e)
    {
        Button btn = sender as Button;
        switch (lettersTyped)
                {
                    case 1:
                        t1.Text = btn.Content.ToString();
                        break;
                    case 2:
                        t2.Text = btn.Content.ToString();
                        break;
                    case 3:
                        t3.Text = btn.Content.ToString();
                        break;
                    case 4:
                        t4.Text = btn.Content.ToString();
                        break;
                    case 5:
                        t5.Text = btn.Content.ToString();
                        break;
                    case 6:
                        t6.Text = btn.Content.ToString();
                        break;
                    case 7:
                        t7.Text = btn.Content.ToString();
                        break;
                    case 8:
                        t8.Text = btn.Content.ToString();
                        break;
                }
    }

I want other method that doesn't use the switch statement

like : t"The code determines the number".Text = btn.Content.ToString();

Upvotes: 1

Views: 171

Answers (4)

Andrew Cyrul
Andrew Cyrul

Reputation: 249

Make a global (private/public/whatever) dictionary:

private Dictionary<Button, TextBox> dict;

Then initialize it somewhere (eg. the page's constructor) :

dict = new Dictionary<Button, TextBox>()
{
    { btn1, t1 },
    { btn2, t2 }
};

And finally you attach the same Click handler to all of the buttons:

private void button_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    dict[btn].Text = btn.Text;
}

Sender is the Button you click, so you just take the text from it and attach it to the TextBox assigned to the Button in the Dictionary. This way you don't need to juggle any list indexes.

Upvotes: 1

Łukasz Rejman
Łukasz Rejman

Reputation: 1892

If your TextBlocks are in one container, like StackPanel, for example:

<StackPanel x:Name="container">
    <TextBlock x:Name="t1" />
    <TextBlock x:Name="t2" />
    <TextBlock x:Name="t3" />
    <TextBlock x:Name="t4" />
    <TextBlock x:Name="t5" />
    <TextBlock x:Name="t6" />
    <TextBlock x:Name="t7" />
    <TextBlock x:Name="t8" />
</StackPanel>

Then you can get them by index.

TextBlock tb = container.Children[lettersTyped - 1] as TextBlock; // Convert lettersTyped to 0-based index
tb.Text = btn.Content.ToString();

Upvotes: 0

samy
samy

Reputation: 14972

You can initialize a Dictionary<int, TextBlock> with your letter count and the control that must be changed and just reference the number of letters typed:

dic[lettersTyped].Text = btn.Content.ToString();

You can initialize your directory by parsing the text blocks ids if you don't want to initialize all blocks by hand. However I don't see any way for you to do it in one line (unless it's a v. long line)

Pseudocode:

  • find all relevant textblocks (e.g all text blocks with id in the form textxxx with xxx numerical)
  • extract the numerical part of the id for each text block tb
  • add an entry in the dictionary with key xxx and value control tb
  • dereference correct textblock with dictionary[keys typed]

Upvotes: 1

apomene
apomene

Reputation: 14389

you can add all textblock to a List and handle accordingly:

List <TextBlock> TBlocks=new List<TextBlock>();
TBlocks.Add(t1);
...
TBlocks.Add(t8);

private void LetterClicked(object sender, RoutedEventArgs e)
{
        Button btn = sender as Button;
        TBlocks[lettersTyped-1].Text= btn.Content.ToString();
}

Upvotes: 0

Related Questions