Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:34:27+00:00 2026-06-01T08:34:27+00:00

This is the code of mouse down where i click on the points: private

  • 0

This is the code of mouse down where i click on the points:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      label1.Text = e.X.ToString();
      label2.Text = e.Y.ToString();
      label1.Visible = true;
      label2.Visible = true;
      label3.Visible = true;
      label4.Visible = true;
      // find the index that is closest to the current mouse location
      MinDist = float.MaxValue;

      for (idx = 0; idx < Point_X.Count; ++idx)
      {
         float dx = Point_X[idx] - e.X;
         float dy = Point_Y[idx] - e.Y;
         float dist = (float)Math.Sqrt(dx * dx + dy * dy);

         if (dist < MinDist)
         {
            MinDist = dist;
            selectedIndex = idx;
         }
      }

      if (MinDist < 5)
      {
         mouseMove = true;
         OriginalX = Point_X[(int)selectedIndex];
         OriginalY = Point_Y[(int)selectedIndex];

         if (cyclicSelectedIndex.Count() == 2)
         {
            cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
            currentCyclicIndex++;
            if (currentCyclicIndex > 1)
            {
               currentCyclicIndex = 0;
            }
            if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) ||
                (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
            {
               button2.Enabled = false;
            }
            else
            {
               button2.Enabled = true;
            }

            label13.Text = selectedIndex.ToString();
            label13.Visible = true;
            label14.Visible = true;

            listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
         }
      }
   }
}

And this is the paint event in the first FOR LOOP i draw the points in the second FOR LOOP i draw the lines between the points.

I want that if i click on one point it will be blue and if i click on second point it will be yellow. Now when i create the points they are red.

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
   Point connectionPointStart;
   Point connectionPointEnd;
   Graphics g = e.Graphics;
   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
   SolidBrush brush = new SolidBrush(Color.Red);
   Pen p = new Pen(brush);
   for (int idx = 0; idx < Point_X.Count; ++idx)
   {
      Point dPoint = new Point((int)Point_X[idx], (int)Point_Y[idx]);
      dPoint.X = dPoint.X - 5; // was - 2
      dPoint.Y = dPoint.Y - 5; // was - 2
      Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
      g.FillEllipse(brush, rect);
   }

   for (int i = 0; i < connectionStart.Count; i++)
   {
      int startIndex = connectionStart[i];
      int endIndex = connectionEnd[i];

      connectionPointStart = new Point(
         (int)Point_X[startIndex], (int)Point_Y[startIndex]);
      connectionPointEnd = new Point(
         (int)Point_X[endIndex], (int)Point_Y[endIndex]);
      p.Width = 4;
      g.DrawLine(p, connectionPointStart, connectionPointEnd);
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:34:28+00:00Added an answer on June 1, 2026 at 8:34 am

    You’ll need to modify the brush that you use to draw each point. Here’s a simple solution.

    First, add another variable to your class called previouslySelectedIndex of type int, and initialize it with -1. Then, inside your Mouse Down event, add the line shown below:

         ...
         if (dist < MinDist)
         {
            MinDist = dist;
            previouslySelectedIndex = selectedIndex  // <--- Add this line
            selectedIndex = idx;
         }
         ...
    

    Now, add the section noted below in the correct place inside the Paint Event handler:

       ...
       Brush brush = Brushes.Red;  // <-Change the type from SolidBrush to Brush, which is more general
       ...
       for (int idx = 0; idx < Point_X.Count; ++idx)
       {
          ...
    
          // START OF NEW CODE
    
          brush = Brushes.Red;  // <-- This line was not in my original solution.
    
          if (idx==selectedIndex)  // <-- Add all this code
              brush = Brushes.Blue; 
          else if(idx==previouslySelectedIndex) 
              brush = Brushes.Yellow;
    
          // END OF NEW CODE
    
          g.FillEllipse(brush, rect);
       }
       ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
This code will output some blinks on divs when we do the mouse over.
I've this code in flex where I register a mouse out event listener -
I am binding a left click gesture to a WPF button, expecting that it
I'm trying to build drop down options on mouse over and on click. The
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.