M.eljilali
M.eljilali

Reputation: 3

how can i call a button click method from another class

hello everyone i'm new to c# and wpf programming and i'm trying to create a dynamic menu where i have + and - buttons which affect a text box which represents quantity. so in a grid i call a class called productcard which i call in a page to fill the grid with the products. now the problem is how can i use the click event inside of the product card class in my page where i have multiple cards.

class productcard 
{

     Button plus = new Button();
    Button minus= new Button();
    public TextBox qtyl = new TextBox();
    Grid z = new Grid();
    public int left;
    public int top;

    GroupBox yy;
    public GroupBox XX { get { return this.yy; } set { this.yy = value; } }


    public productcard(int left , int top )
    {
        this.left = left;
        this.top = top;
        Thickness margin = new Thickness(left, top, 0, 0);
        Thickness bmar = new Thickness(0, 0, 0, 0);
        plus.Height = 30;
        plus.Width = 40;
        plus.VerticalAlignment = VerticalAlignment.Bottom;
        plus.HorizontalAlignment = HorizontalAlignment.Right;
        plus.Content = "+";
        plus.HorizontalContentAlignment = HorizontalAlignment.Center;

        // - button 
        minus.Height = 30;
        minus.Width = 40;
        minus.VerticalAlignment = VerticalAlignment.Bottom;
        minus.HorizontalAlignment = HorizontalAlignment.Left;
        minus.Content = "-";
        minus.HorizontalContentAlignment = HorizontalAlignment.Center;
        // add the button to the grid 
        z.Children.Add(plus);
        z.Children.Add(minus);
        // creat text box 
        qtyl = new TextBox();
        qtyl.Height = 30;
        qtyl.Width = 30;
        qtyl.Background = Brushes.White;
        qtyl.VerticalAlignment = VerticalAlignment.Bottom;
        qtyl.HorizontalAlignment = HorizontalAlignment.Center;
        qtyl.Text = "0";
        // add text box to the grid inside the group box
        z.Children.Add(qtyl);
        // creat group box
        GroupBox yy = new GroupBox();
        yy.Margin = margin;
        yy.VerticalAlignment = VerticalAlignment.Top;
        yy.HorizontalAlignment = HorizontalAlignment.Left;
        yy.Content = z;
        yy.Height = 150;
        yy.Width = 150;
        XX = yy;
        // insert group box in the produc grid 
    }




   public void plus_Click(object sender, EventArgs e)
    {
       // this.plus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
        MessageBox.Show(" + has been cliked");
        int result=Convert.ToInt32(qtyl.Text)+1;
        qtyl.Text = result.ToString();
    }

    private void minus_Click(object sender, EventArgs e)
    {
        int result = Convert.ToInt32(qtyl.Text) - 1;
        qtyl.Text = result.ToString();
    }

}

Upvotes: 0

Views: 1525

Answers (2)

Travis Primm
Travis Primm

Reputation: 179

Reza is correct in how to write more code for a Button generated in code.

However, I would give you a word of warning that this is not proper WPF usage of MVVM and you might be putting yourself down a path for trouble later.

I would suggest having your view's buttons bind to an ICommand that can be defined in a ViewModel that will handle doing the logic for your text update. As you mentioned, you're having different view controls represent the data based on your button press. You're currently surviving as the view's are directly updating each other (THIS IS BAD). The moment you want to represent this data in other views, say you want your button to update 5 labels in 3 different layouts in 2 windows, you're going to have unmaintainable references in your views.

If you have the ViewModel get a command from your view bound to the button, you can have the command logic update the property in the ViewModel that multiple views can be bound to and update them all at the same time via INotifyPropertyChanged. Not to mention, ICommand can also let you disable buttons cleanly from being clicked.

Consider taking an hour to check out this tutorial to see the separation of View and ViewModel. What you're doing now looks like it's setting you up for a world of hurt later...

Upvotes: 0

Reza Kargar
Reza Kargar

Reputation: 66

You can make a handler for your button like this:

Button myButton=new Button(); 

myButton.Click += delegate(object sender, RoutedEventArgs e) {
   //handle event
};

I hope this helps.

Upvotes: 1

Related Questions