473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# 2003 picture box control mouse events problem

I'm trying to make a grid of picture box controls, which I do via code. Each of these boxes has a mouseclick, mousedown, mouseup, and mouseenter event. The mouseclick event is simple enough and works fine. However, i use the mousedown, mouseup, and mouseenter events to be able to click and drag to apply the same image across all of the boxes the mouse enters (similar to a terrain editor for a game like StarCraft). I set a form wide boolean variable to true when the mousedown event occurs, then check that variable whenever a mouseenter event occurs, and then set it to false when a mouseup event occurs. Most of the code is in place, however I am having a problem. After I click and drag along to apply the image in the grid, if I downclick inside of a box I've already applied an image to, the mouseenter event no longer seems to fire. I've tried removing the mouseclick event, but to no avail. I've also tried switching to mousemove event, but still no solution.

So basically my problem is that the picturebox controls that have a new image in them (they are preloaded with an image) caused by a mouseenter or a mouseclick, the mouseenter event does not fire again. But this only occurs if the first box clicked on is one with a new image. If I were to start in a box without a new image, it will drag across new images, changing them with no problem. I'll try to construct an image below.

The grid looks something like this (20x20).
1 2 3 4 5 6 7 8
____________
1|_|_|_|_|_|_|_|_|
2|_|_|_|_|_|_|_|_|
3|_|_|_|_|_|_|_|_|
4|_|_|_|_|_|_|_|_|
5|_|_|_|_|_|_|_|_|
6|_|_|_|_|_|_|_|_|
7|_|_|_|_|_|_|_|_|
8|_|_|_|_|_|_|_|_|

So basically, any thoughts?

I've included some code as well below.

Expand|Select|Wrap|Line Numbers
  1.     private void CreateGridSystem(string sOriginalDirectory)
  2.         {
  3.             int y, z = -1;
  4.  
  5.             for(y = 0; y < MAX_GRID; y++)
  6.             {
  7.                 if((y % MAX_LENGTH) == 0)
  8.                 {
  9.                     z++;
  10.                 }
  11.                 tcTerrain[y] = new TerrainClass();
  12.                 pbBoxes[y] = new PictureBox();
  13.                 // set initial properties
  14.                 pbBoxes[y].Top = (z)*MAX_SIZE + 56;
  15.                 pbBoxes[y].Left = (y%MAX_LENGTH)*MAX_SIZE;
  16.                 pbBoxes[y].Width = MAX_SIZE;
  17.                 pbBoxes[y].Height = MAX_SIZE;
  18.                 pbBoxes[y].SizeMode = PictureBoxSizeMode.StretchImage;
  19.                 //pbBoxes[x, y].Image = Image.FromFile(Directory.GetCurrentDirectory() + @"\Pictures" + @"\tree.gif");
  20.                 pbBoxes[y].Tag = y;
  21.                 pbBoxes[y].BorderStyle = BorderStyle.FixedSingle;
  22.                 this.Controls.Add(pbBoxes[y]);
  23.                 pbBoxes[y].Image = Image.FromFile(sOriginalDirectory + @"\Pictures" + @"\blank_space.bmp");
  24.             //    pbBoxes[y].Click += new System.EventHandler(PicBoxHandler);
  25.                 pbBoxes[y].MouseDown += new System.Windows.Forms.MouseEventHandler(PicBoxMouseDownHandler);
  26.                 pbBoxes[y].MouseEnter += new System.EventHandler(PicBoxDragHandler);
  27.                 pbBoxes[y].MouseUp += new System.Windows.Forms.MouseEventHandler(PicBoxMouseUpHandler);
  28.                 pbBoxes[y].MouseMove += new MouseEventHandler(MouseMoveHandler);
  29.             }
  30.         }
  31.  
  32.  
  33.         private void PicBoxHandler(Object sender, System.EventArgs e)
  34.         {
  35.             int i = 0;
  36.             short theta = 0;
  37.  
  38.             i = (int)((System.Windows.Forms.PictureBox)sender).Tag;
  39.             pbBoxes[i].Image.Dispose();
  40.             System.GC.Collect();
  41.             pbBoxes[i].Image = null;
  42.  
  43.             //tbbTree
  44.             if(sTerrainChoice == 0)
  45.             {
  46.                 pbBoxes[i].Image = imMerger.generate_new_image(sColorChoice, @"pine_tree.bmp", sDirectory, "", "", false);
  47.                 pbBoxes[i].BorderStyle = BorderStyle.None;
  48.                 tcTerrain[i].reset();
  49.                 tcTerrain[i].setTerrain(sTerrainChoice);
  50.                 tcTerrain[i].setSide(sSide);
  51.             }
  52.                 //tbbShrub
  53.             else if(sTerrainChoice == 1)
  54.             {
  55.                 tcTerrain[i].reset();
  56.                 tcTerrain[i].setTerrain(sTerrainChoice);
  57.                 tcTerrain[i].setSide(sSide);
  58.             }
  59.                 //tbbRiver
  60.             else if(sTerrainChoice == 2)
  61.             {
  62.                 pbBoxes[i].Image = /*imMerger.rotate_image(*/imMerger.generate_new_image(sColorChoice, @"river_t.bmp", sDirectory, "", "", false)/*)*/;
  63.                 pbBoxes[i].BorderStyle = BorderStyle.None;
  64.                 tcTerrain[i].reset();
  65.                 tcTerrain[i].setTerrain(sTerrainChoice);
  66.                 tcTerrain[i].setSide(sSide);
  67.                 imMerger.path_chooser(tcTerrain, true, i, MAX_LENGTH, theta);
  68.                 pbBoxes[i].Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
  69.             }
  70.                 //tbbRock
  71.             else if(sTerrainChoice == 3)
  72.             {
  73.                 sTerrainChoice = 3;
  74.             }
  75.                 //tbbGrass
  76.             else if(sTerrainChoice == 4)
  77.             {
  78.                 sTerrainChoice = 4;
  79.             }
  80.                 //tbbPath
  81.             else if(sTerrainChoice == 5)
  82.             {
  83.                 sTerrainChoice = 5;
  84.             }
  85.                 //tbbRedFlag
  86.             else if(sTerrainChoice == 6)
  87.             {
  88.                 sTerrainChoice = 6;
  89.             }
  90.                 //tbbBlueFlag
  91.             else if(sTerrainChoice == 7)
  92.             {
  93.                 sTerrainChoice = 7;
  94.             }
  95.         }
  96.  
  97.  
  98.         private void PicBoxMouseDownHandler(object sender, System.Windows.Forms.MouseEventArgs e)
  99.         {
  100.             bMouseClicked = true;
  101.             label1.Text = bMouseClicked.ToString() + " " + ((System.Windows.Forms.PictureBox)sender).Tag;
  102. //            pbBoxes[(int)((System.Windows.Forms.PictureBox)sender).Tag].Image.Dispose();
  103. //            System.GC.Collect();
  104.             PicBoxHandler(sender, e);
  105.             bMouseClicked = true;
  106. //            this.Dispose();
  107.         }
  108.         private void PicBoxMouseUpHandler(object sender, System.Windows.Forms.MouseEventArgs e)
  109.         {
  110.             bMouseClicked = false;
  111. //            this.Dispose();
  112.         }
  113.         private void PicBoxDragHandler(object sender, System.EventArgs e)
  114.         {
  115.             label1.Text = bMouseClicked.ToString() + " " + ((System.Windows.Forms.PictureBox)sender).Tag;
  116.             if((bMouseClicked == true))// && System.Windows.Forms.MouseEventArgs
  117.             {
  118. //                pbBoxes[(int)((System.Windows.Forms.PictureBox)sender).Tag].Image.Dispose();
  119. //                System.GC.Collect();
  120.                 PicBoxHandler(sender, e);
  121.                 pbBoxes[(int)((System.Windows.Forms.PictureBox)sender).Tag].MouseEnter -= System.EventHandler(PicBoxDragHandler);
  122.                 pbBoxes[(int)((System.Windows.Forms.PictureBox)sender).Tag].MouseEnter += new System.EventHandler(PicBoxDragHandler);
  123.             }
  124.         }
Feb 6 '07 #1
7 3422
kenobewan
4,871 Expert 4TB
Not sure if this helps, but sounds like the new image needs a mouse event too...
Feb 6 '07 #2
I tried adding a new event handler, but to no avail. I don't think this was the solution because I'm not making a new control, just changing the image of the control.
Feb 6 '07 #3
kenobewan
4,871 Expert 4TB
Have you tried using the new image to trigger the same event handler? I believe that essentially you have a JS problem and the solution lies in trying to give the new image event handlers which it currently doesn't have... Of course I may be wrong, its happened before ;).
Feb 6 '07 #4
I've tried doing what you suggested, at least I think I know what you meant in your suggestion. I added
Expand|Select|Wrap|Line Numbers
  1. this.pbBoxes[i].MouseEnter += new EventHandler(PicBoxDragHandler);
to my code after the new image is set, in PicBoxHandler (see code above) but to no avail. Is this what you mean, or am I misunderstanding you?
Feb 6 '07 #5
kenobewan
4,871 Expert 4TB
My bad, I thought that this would have worked... You could try adding a JS event that triggers an alert and build it up from there to see what you can come up with, but unfortunately I am fresh out of ideas :(. Anyone else?
Feb 6 '07 #6
quick question on your post, by JS, I assume you mean javascript? I ask because i'm trying to do this in c#, and i wasn't sure if there was a miscommunication here.
Feb 7 '07 #7
kenobewan
4,871 Expert 4TB
Just found another possible way:
handling mousedown for dynamic pictureboxes

Does this help?
Feb 7 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Chris Coho, Jr. | last post by:
Ok, I'll explain the whole problem because there may be several ways to solve this and hopefully someone knows one. What I'm doing is creating a specialty template editor, similar to say a corel...
3
by: red | last post by:
mouse events when the mouse is on a "child control" hi everyone; my problem: I have a userControl in this usercontrol, I have a child control (a button) when the mouse moves over the...
3
by: Z D | last post by:
Hello, BACKGROUND: ============== I've created a Windows User Control that contains an Image Control (among other controls). The user control handles the picture resize event. Whenever the...
6
by: jcrouse | last post by:
I am rotating some text is some label controls. In the one place I use it it works fine. In the other place I use it I can't figure out the syntax. I don't really understand the event. Where it...
9
by: Elliot Rodriguez | last post by:
WinXP Pro Let me preface this by saying I have developed with the .NET IDE since its release, and I consider myself reasonably savvy with it. I have a medium sized form with about 120 controls...
1
by: Israel | last post by:
The problem: I want to know, definitively when a slider loses focus after a user has started sliding and hasn't released the mouse yet. It appears that this is captured with the WM_ACTIVATEAPP...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
14
by: raghunadhs | last post by:
HI All! I am developing a V.B application. in this application, user is able to drag and drop a picture(user control) at run time also. for this purpose, i used form's "dragdrop" event....
9
by: prakashwadhwani | last post by:
Hi !! I'm about to develop a new project for a client. Should I go about it in Access 2003 or 2007 ? Purchasing it either for me or for my client is not a major consideration here ... what I'd...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.