c#单击按钮将列表框项目发送到另一个indow

时间:2016-06-16 19:24:01

标签: c# wpf canvas listbox

我有这个按钮,用一个画布打开一个窗口。 当我点击画布时,一个事件开始,应该转到我的第一个表单,查看2个列表框中的选定项目,并发送颜色和形状,以便我可以在我的画布上绘制它。 我正在制作自己的自定义活动课程。

public class MyEventArgs
{

}

我想知道我应该在那里为构造函数或列表框中的项目编写

就像在例子中一样

public class MyEventArgs
{ 
    public DateTime EventDateTime { get; private set; }

    public MyEventArgs(DateTime eventDateTime)
    {
        EventDateTime = eventDateTime;
    }
}

我唯一的问题是创建自定义eventargs 我可以接下来的步骤

主窗口

public delegate void listboxEventHandler(object sender, MyEventArgs myEventArgs);



public partial class MainWindow : Window
{
    tblColorke kleur = new tblColorke();
    tblVorm vorm = new tblVorm();
    tblType type = new tblType();
    LinqSanderDataContext db = new LinqSanderDataContext();

    public event listboxEventHandler ListboxEvent;

    public MainWindow()
    {
        InitializeComponent();





        foreach (tblColorke kleur in db.tblColorkes)
        {
            ColorItem ci = new ColorItem
            {
                ColorId = kleur.ColourID,
                Fill = new SolidColorBrush(Color.FromArgb(255,
                    Convert.ToByte(kleur.ColorR), Convert.ToByte(kleur.ColorG), Convert.ToByte(kleur.ColorB)))
            };
            lsbColor.Items.Add(ci);
        }

        foreach (tblType type in db.tblTypes)
        {
            lsbType.Items.Add(type);
        }

        foreach (tblVorm vorm in db.tblVorms)
        {
            lsbVorm.Items.Add(vorm);
        }

        foreach (tblCanvaske canvaske in db.tblCanvaskes)
        {
            dgCanvas.Items.Add(canvas);
        }


    }

    void On_MyObjProperty_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        lsbColor.Items.Refresh();
    }




    private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        try
        {
            ColorItem John = lsbColor.SelectedItem as ColorItem;
            if (lsbVorm.SelectedItem == null)
                throw new Exception("Please select a shape before drawing!");

            Point mouseLocation = e.GetPosition(this.canvas);

            tblVorm vrm = lsbVorm.SelectedItem as tblVorm;

            switch (vrm.TypeID)
            {
                case 1:
                    Circle ci = new Circle(vrm.Hoogte, vrm.Breedte);
                    if (ci != null)
                    {
                        ci.Draw(canvas, (int)mouseLocation.X, (int)mouseLocation.Y);
                        ci.Paint(John.Fill);
                    }
                    break;

                case 2:
                    Ellipse1 el = new Ellipse1(vrm.Hoogte, vrm.Breedte);
                    if (el != null)
                    {
                        el.Draw(canvas, (int)mouseLocation.X, (int)mouseLocation.Y);
                        el.Paint(John.Fill);
                    }
                    break;

                case 3:
                    Rectangle1 re = new Rectangle1(vrm.Hoogte, vrm.Breedte);
                    if (re != null)
                    {
                        re.Draw(canvas, (int)mouseLocation.X, (int)mouseLocation.Y);
                        re.Paint(John.Fill);
                    }
                    break;

                case 4:

                    Square sq = new Square(vrm.Hoogte, vrm.Breedte);
                    if (sq != null)
                    {
                        sq.Draw(canvas, (int)mouseLocation.X, (int)mouseLocation.Y);
                        sq.Paint(John.Fill);
                    }
                    break;

                default:
                    break;
            }



        }
        catch (Exception exp)
        {

            MessageBox.Show(exp.Message);
        }
    }



    private void btnToevoegenKleur_Click(object sender, RoutedEventArgs e)
    {

        try
        {
            tblColorke kleur = new tblColorke();
            kleur.ColorR = System.Convert.ToInt32(txtR.Text);
            kleur.ColorG = System.Convert.ToInt32(txtG.Text);
            kleur.ColorB = System.Convert.ToInt32(txtB.Text);
            db.tblColorkes.InsertOnSubmit(kleur);
            db.SubmitChanges();
            txtR.Clear();
            txtG.Clear();
            txtB.Clear();
        }
        catch (Exception exp)
        {

            MessageBox.Show(exp.Message);
        }

    }

    private void btnToevoegenVorm_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            tblVorm vorm = new tblVorm();
            vorm.TypeID = ((tblType)lsbType.SelectedItem).TypeID;
            vorm.Hoogte = System.Convert.ToInt32(txtHeight.Text);
            vorm.Breedte = System.Convert.ToInt32(txtWidth.Text);
            db.tblVorms.InsertOnSubmit(vorm);
            db.SubmitChanges();
            txtHeight.Clear();
            txtWidth.Clear();
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
        }
    }

    private void btnCOpslaan_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            LinqSanderDataContext ctx = new LinqSanderDataContext();

            tblCanvaske canvasken = new tblCanvaske
            {
                Naam = txtNaam.ToString(),
                Aangemaakt = DateTime.Now,
                Aangepast = DateTime.Now
            };
            foreach (DrawableShape ds in canvas.Children)
            {
                tblTekeningening tekeningening = new tblTekeningening
                {
                    MouseLocationX = (int)Canvas.GetLeft(ds),
                    MouseLocationY = (int)Canvas.GetTop(ds),
                    Vorm = ((tblVorm)lsbVorm.SelectedItem).VormID,
                    Kleur = ((ColorItem)lsbColor.SelectedItem).ColorId,
                    tblCanvaske = canvasken
                };
                ctx.tblTekeningenings.InsertOnSubmit(tekeningening);
            }
            ctx.SubmitChanges();
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
        }
    }

    protected void onListboxEvent(MyEventArgs myEventArgs)
    {
        if (ListboxEvent != null)
        {
            ListboxEvent(this, myEventArgs);
        }
    }

    private void btnNewScrean_Click(object sender, RoutedEventArgs e)
    {

    }
}

}

0 个答案:

没有答案