0

Buttons are made programmatically containing each text string. Maybe, there're some ways to find a control with its property like this case.

Here is my code which to make buttons programmatically and a code of linked event.

What if an user press A button(Airplane), the color of button become yellow. However, what if the user press B button(Car) just after pressing A button? I want to know how to change the color of A button from yellow to back normal and the color of B button from normal to yellow. This is why I need to indicate a button with its content.

for (int k = 0; k < Overviews.Length; k++)
{
    Button btnoverviewcontent = new Button();

    ToolTip tt = new ToolTip();
    tt.Content = "Press button if you want to modify or delete";
    btnoverviewcontent.ToolTip = tt;

    btnoverviewcontent.Cursor = Cursors.Hand;

    SolidColorBrush mySolidColorBrush = new SolidColorBrush();
    mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241);
    btnoverviewcontent.Background = mySolidColorBrush;

    btnoverviewcontent.Effect = new DropShadowEffect
    {
        Color = new Color { A = 255, R = 0, G = 0, B = 0 },
        Direction = 315,
        ShadowDepth = 5,
        Opacity = 1
    };

    btnoverviewcontent.Padding = new Thickness(3, 3, 3, 3);
    btnoverviewcontent.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
    TextBlock textBlock = new TextBlock()
    {
        Text = Overviews[k],
        TextAlignment = TextAlignment.Left,
        TextWrapping = TextWrapping.Wrap,

    };

    btnoverviewcontent.Content = textBlock;
    btnoverviewcontent.BorderThickness = new Thickness(0, 0, 0, 0);
    btnoverviewcontent.FontStretch = FontStretches.UltraExpanded;
    btnoverviewcontent.Margin = new Thickness(5, 5, 5, 5);

    WrapPanelGreen.Children.Add(btnoverviewcontent);
    btnoverviewcontent.Click += new RoutedEventHandler(OnOverviewClick);
}  
void OnOverviewClick(object sender, RoutedEventArgs args)
{

    buttonOverviewmodification.Visibility = Visibility.Visible;
    buttonOverviewdeletion.Visibility = Visibility.Visible;

    Button btn = (Button)sender;
    DoubleAnimation animation = new DoubleAnimation(1, TimeSpan.FromMilliseconds(350));
    buttonOverviewmodification.BeginAnimation(Image.OpacityProperty, animation);
    buttonOverviewdeletion.BeginAnimation(Image.OpacityProperty, animation);

    SolidColorBrush mySolidColorBrush = new SolidColorBrush();
    mySolidColorBrush.Color = Color.FromArgb(255, 253, 253, 10);
    (sender as Button).Background = mySolidColorBrush;
}
ASh
  • 34,632
  • 9
  • 60
  • 82
Kay Lee
  • 922
  • 1
  • 12
  • 40
  • Possible duplicate of [How can I find WPF controls by name or type?](http://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type) – Gabe Feb 18 '16 at 02:31
  • Could you please expand on your question to make it more clear what you're asking? Example code would also be immensely useful. Thanks! – tpartee Feb 18 '16 at 02:32
  • @Gabe, To beginner like me, it's too complicated and maybe not duplicate with finding a control by name or type. I know a bypassing way that numbering buttons and save the number of clicked button somewhere and utilize the number to specify. However, I wonder to find direct and easier solution. If this is duplicate, would you please kindly guide some example? – Kay Lee Feb 18 '16 at 04:23
  • @tpartee, sorry for no example but this is very easy situation. There're 3 buttons made programmatically and each button's contents are 'Airplane', 'Car', 'Rocket'. Then, How can I know(indicate) a button with 'Airplane' programmatically. – Kay Lee Feb 18 '16 at 04:28
  • Once you find the button, what will you do with it? I am guessing that is the real question you want to answer, but without some code or idea of what you are doing, there is no answer to provide such as use an event, property, tag, etc. – Kory Gill Feb 18 '16 at 04:49
  • @KayLee As Kory stated - Without intent, esp without code there isn't a 'correct' direction we can point you in. Finding controls that are produced programmatically are usually found using reflection which, in itself is an advanced topic. However if the link above is too advanced perhaps [THIS](http://www.c-sharpcorner.com/uploadfile/mahesh/find-controls-by-name-in-wpf/) will help as an intro. Also [THIS](https://joshsmithonwpf.wordpress.com/2007/06/28/how-to-use-findname-with-a-contentcontrol/) Other than that, without a code sample of what you've tried & intent - That's the best I can do – Gabe Feb 18 '16 at 05:11
  • @Kory Gill, Thanks a lot, I updated my question and explanation. – Kay Lee Feb 18 '16 at 06:08
  • @Gabe, Thank you so much but I already got through mentioned 2 posts weeks ago. I updated my question and explanation. Do you see some solution now? – Kay Lee Feb 18 '16 at 06:10

1 Answers1

2

Without your code, an answer was not feasible. Based on what you have, this is a workable solution.

Do something like this:

private void OnOverviewClick(object sender, RoutedEventArgs e)
{
    // for all buttons in WrapPanelGreen, reset background to default value we used
    foreach (var child in WrapPanelGreen.Children)
    {
        var b = child as Button;
        if (b != null)
        {
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241);

            b.Background = mySolidColorBrush;
        }
    }

    // the rest of your code
}
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • Oh, this can be an alternative I highly appreciate your care and excellence. I've learned another good point in courtesy of you ! – Kay Lee Feb 18 '16 at 07:19
  • Finally, I've adopted your solution and it's excellently working. Much satisfied. Because it seems complicated to find a control with some condition if I think of above mentioned post. Therefore, I thought it would be not so easy though it look easy question. And I took your solution as alternative but wise enough ! – Kay Lee Feb 18 '16 at 08:46