0

EDIT: For new programmers to OOP just calling the class isn't enough if the syntax is looking for a specific variable type it will throw an error. This was the problem as pointed at the answers bellow, this line of code:

System.Windows.Controls.Canvas.SetLeft(Gate_list[Gate_list.Count - 1], Pos.X);

It has one fatal mistake and that is that the Canvas.SetLeft is looking for a canvas child class (in this case a rectangle) and a point. However I sent in a Class type Gate_list[Gate_list.Count - 1] when it should've been Gate_list[Gate_list.Count - 1].GetRect()

End of edit

I have a delegate that is calling a class in a method. The delegate is detecting a mouse down event on a Rectangle(Here is how it's done). In the method I'm trying to SetLeft on a Rectangle I just added to the Canvas but get the error CS1503.

I've tried converting it to System.Windows.UIElement but System can't be converted.

public partial class Program
{
    public void Rect_Button_MouseDown(MainWindow MainWind, string Tag)
    {
        Point Pos = new Point();
        Pos = System.Windows.Input.Mouse.GetPosition(MainWind.Main_Canvas);
        if (Drag == false)
        {
            Drag = true;
            Gate_list.Add(new Gate_Class(Convert.ToInt32(Tag),new Rectangle()));
            MainWind.Main_Canvas.Children.Add(Gate_list[Gate_list.Count-1].Get_Rect());
            System.Windows.Controls.Canvas.SetLeft(Gate_list[Gate_list.Count - 1], Pos.X);
        }
    }
}

I believe there should be an away to transfer system.windows but I don't know.

My question comes down to finding a way to convert Gate_list[] to UIElement.

Andrew Foot
  • 119
  • 10
  • You should show us how Gate_Class is, because *THAT* is supposed to be UIElement compatible (from what your code indicates). – Mario Vernari May 14 '19 at 08:17
  • As a note, it is useless to initialize a local variable with a default value directly before assigning a new value to it. Just write `Point Pos = System.Windows.Input.Mouse.GetPosition(MainWind.Main_Canvas);` – Clemens May 14 '19 at 08:24
  • I forgot to add the method in the class Gate_Class to get the rectangle. I was being an idiot and forget the simple sets and tried to make it more complex sorry. – Andrew Foot May 14 '19 at 09:11

2 Answers2

1

I think in last line of your code you are providing Gate_list[Gate_list.Count - 1] but this is your custom class and not the Rectangle you just created. I can deduce from your code that you can get the Rectangle by using Gate_list[Gate_list.Count - 1].Get_Rect().

In other words it seems to me that you need to add .Get_Rect() to actually get the rectangle.

If this doesn't work, please provide code of your Gate_Class and the error message you are getting.

Mohammad
  • 1,930
  • 1
  • 21
  • 31
1

Gate_Class is apparently not a UIElement. It should have a Rectangle property that returns the Rectangle that you pass to its constructor. You can then set the Canvas.Left property of the Rectangle:

System.Windows.Controls.Canvas.SetLeft(Gate_list[Gate_list.Count - 1].Rectangle, Pos.X);

public class Gate_Class
{
    public Gate_Class(int tag, Rectangle rectangle)
    {
        //...
        Rectangle = rectangle;
    }

    public Rectangle Rectangle { get; }
}
mm8
  • 163,881
  • 10
  • 57
  • 88