-3

如何从单击修改为双击?谁能帮助我,对不起我的英语。

private void pbGraph_MouseClick(object sender, MouseEventArgs e)
    {
        // recorder
        string tmpToRecord = "";

        //initialize default brush and pen for drawing
        //pen for making lines, brush to fill the region
        Pen p = new Pen(Color.Blue,1);
        SolidBrush brush = new SolidBrush(Color.Red);



        //event triggered when left click
        if (e.Button == MouseButtons.Left)

        {

            //limit nodes to 5 only
            if (count < 100)
            {
                int currentNodeLocation = whereLocationIs(e.X, e.Y, 10, 10);
                if (currentNodeLocation == 0)
                {

                //add mouse click position to the arraylist
                //increase count everytime a node is created
                count++;
                //store coordinates temporarily
                tmpToRecord = "0," + count.ToString() + "," + e.X.ToString() + "," + e.Y.ToString();
                //store the coordinates based on event click to the array
                arrayForNode[count] = new LocationNodes(e.X, e.Y);

                //get graphics object for picturebox
                Graphics.FromImage(pbGraph.Image).TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                //draw rectangle nodes
                Graphics.FromImage(pbGraph.Image).FillEllipse(brush, arrayForNode[count].returnRectangle());
                //draw numbers aside nodes
                Graphics.FromImage(pbGraph.Image).DrawString(count.ToString(), new Font("Verdana", 8), brush, (float)e.X + 12, (float)e.Y + 12);
                //give status report
                labelStatus.Text = "Status: Node created  " + count;

                //dahSaveKe = false;

                this.Changes = true;
                }
            }

            else
            {
                MessageBox.Show("Maximum number of nodes reached");
            }
        }


    //event triggered when right click
    else if (e.Button == MouseButtons.Right)


    {



        //x denotes the location of node 
        int x = whereLocationIs1(e.X, e.Y);

        if (x != 0)
        {
            if (firstNodeChecked == 0)
            {
                firstNodeChecked = x;
                labelStatus.Text = "Selected Node: " + x.ToString();
            }
            else
            {
                if (firstNodeChecked == x)
                {
                    firstNodeChecked = 0;
                    return;
                }

                labelStatus.Text = "Arc is created from " + firstNodeChecked.ToString() + " to " + x.ToString();

                //FormArcValue fav = new FormArcValue();

                //DialogResult dr = fav.ShowDialog(this);

                //if (dr == DialogResult.OK)
                //{
                    //limiter for drawing arc
                    if (count < (count * count))
                    {
                        AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
                        p.CustomEndCap = bigArrow;

                        //draw line as arc to connect between 2 nodes
                        Graphics.FromImage(pbGraph.Image).DrawLine(p,
                            (arrayForNode[firstNodeChecked]._X + 5), (arrayForNode[firstNodeChecked]._Y + 5),
                            (arrayForNode[x]._X + 5), (arrayForNode[x]._Y + 5));

                        Point fa = writeStringInMiddleArcLine(new Point((arrayForNode[firstNodeChecked]._X), 
                            (arrayForNode[firstNodeChecked]._Y)), new Point((arrayForNode[x]._X), (arrayForNode[x]._Y)));

                        //determine which distribution is selected, then
                        //generate random numbers
                            switch (comboCustomer1.Text)
                            {
                                case "Johnson SB":
                                    cost = RandomNumber(costMin, costMax);
                                    demand = RandomNumber(demandMin, demandMax);
                                    break;

                                case "Uniform":
                                    cost = RandomNumber(costMin, costMax);
                                    demand = RandomNumber(demandMin, demandMax);
                                    break;
                            }



                        //write cost and demand for arc
                        Graphics.FromImage(pbGraph.Image).DrawString("(" + demand.ToString("N2") + " , " +
                            cost.ToString("N2") + 
                            ") " + displayCount.ToString(), new Font("Verdana", 8), new SolidBrush(Color.Blue), fa.X, fa.Y);

                        matrixCost[x, firstNodeChecked] = cost;
                        matrixCost[firstNodeChecked, x] = cost;

                        matrixDemand[x, firstNodeChecked] = demand;
                        matrixDemand[firstNodeChecked, x] = demand;

                        //add count and assign demand to the temp matrix
                        matrixDemandDisplay[displayCount] = demand;


                        // put already linked nodes in here
                        linkedNodes[firstNodeChecked, x] = true;
                        linkedNodes[x, firstNodeChecked] = true;

                        // string to record the path
                        tmpToRecord = "1," + x.ToString() + "," + arrayForNode[x]._X.ToString() + "," + arrayForNode[x]._Y.ToString() +
                           "," + firstNodeChecked.ToString() + "," + arrayForNode[firstNodeChecked]._X.ToString() + "," +
                           arrayForNode[firstNodeChecked]._Y.ToString() + "," + demand.ToString() + "," + cost.ToString() + "," +
                           displayCount.ToString();

                        //increase counter
                        displayCount = displayCount + 1;
                        //this.Changes = true;
                    }

                    firstNodeChecked = 0;
                    //dahSaveKe = false;
                }
                //else if (dr == DialogResult.Cancel)
                //{
                //    firstNodeChecked = 0;
                //}

                //labelStatus.Text = "Left click to draw node and right click to link a node";

                //fav.Dispose();
            }

        }


        if (!(tmpToRecord == ""))
        {
            if (!(recordPath == ""))
                recordPath += "|";

            recordPath += tmpToRecord;
        }


        pbGraph.Refresh();
    }

谁能帮我?

4

2 回答 2

1

查找将 pbGraph_MouseClick 添加到表单的位置。很可能您正在使用 winforms Designer,在这种情况下,它将位于表单的设计器文件中,在这种情况下,您希望通过 GUI 界面将其更改为双击:在那里创建一个双击事件处理程序,然后剪切- 将所有代码从单击处理程序粘贴到新的双击事件处理程序。然后,返回并删除单击处理程序。

于 2013-11-09T00:24:10.857 回答
0

Step1:创建如下myMouseDoubleClick函数:

private void myMouseDoubleClick(object sender, MouseEventArgs e)
            {
             //  Cut and Pase your MouseSingleClick Event Code here.
            }

第2步:pbGraph_MouseClick在上述函数中剪切并粘贴您的鼠标单击事件代码(在您的情况下)。

Step3:将上述函数订阅myMouseDoubleClick到鼠标双击事件。这样每当鼠标双击事件触发时,它都会通知/调用所有订阅的函数,并且您的函数myMouseDoubleClick也会得到通知。

订阅如下:

  this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.myMouseDoubleClick);
于 2013-11-09T05:31:56.713 回答