-1

Update: Remade the project and it started working.


I have a Windows Form that looks like this:

enter image description here

I am trying to make a MouseDown event for each box. As you can see, the BigBox user control has a Panel and a TableLayoutPanel on top of the Panel. Inside each cell of the TableLayoutPanel there is a user control called MyBox, which is just a picture box.

When I try to do a MouseDown event for box1, nothing happens. I have this code inside BigBox.cs:

namespace TestProject.controls
{
    public partial class BigBox : UserControl
    {
        public BigBox()
        {
            InitializeComponent();
        }

        private void box1_MouseDown(object sender, MouseEventArgs e)
        {
            Console.Write("Box1 clicked.");
        }
    }
}

The console never prints anything. What am I doing wrong? I'm new to C# so I don't fully understand everything. Thanks in advance!

syy
  • 687
  • 2
  • 13
  • 32
  • Did you attach `box1_MouseDown` handler to the `MouseDown` event? – Reza Aghaei Aug 01 '16 at 21:10
  • It is going to be hard to get a MouseDown, you leave hardly any space to click since you covered the panel with the TLP. You would have to click at the edge, in practice there probably isn't any. This does not need to be fixed. – Hans Passant Aug 01 '16 at 21:10
  • I've never done any code for attaching something. Is that just going to the Events tab in the Properties box and making sure the it's listed there? If so, I did attach it. @HansPassant I'm not sure what you mean. Isn't clicking the whole cell enough space? I dock `MyBox` so it covers the whole cell. – syy Aug 01 '16 at 21:15
  • To make sure if you attached the event handler, it's enough to select your `box1` and then in properties wiindow in events tab make sure `box1_MouseDown` is written in fron of `MouseDown`. Then your event is handled. Also if you overrided `OnMouseDown` in `MyBox` user control, then make sure you called `base.OnMouseDown(e);`. – Reza Aghaei Aug 01 '16 at 21:23
  • I see. `box1_MouseDown` is inside the events tab for `MouseDown`. So it should be handled. I don't have any kind of events inside `MyBox`. – syy Aug 01 '16 at 21:26
  • So let me know when you say *The console never prints anything.* What do you mean by *The console*? Which window did you check? – Reza Aghaei Aug 01 '16 at 21:28
  • The `output` window. – syy Aug 01 '16 at 21:32
  • Did you set *Show output from* to *Debug*? – Reza Aghaei Aug 01 '16 at 21:34
  • When I do `Console.Write()` anywhere else, say the constructor of `BigBox`, it prints out whatever I write inside `Console.Write()` to the `output` window. Not sure what you mean to set it. – syy Aug 01 '16 at 21:39
  • You see a combo box at top of the output window, the selected value should be *Debug*. If you checked all of these, then you need to post a code to reproduce the problem. Currently the question and the comments are like a guess game. – Reza Aghaei Aug 01 '16 at 21:41
  • Oh ok, it was set to Debug. You want to see the Designer code? Not sure what else to post as the `MouseDown` event is the only thing I added. – syy Aug 01 '16 at 21:45
  • 1
    ummm why not just use MessageBox.Show()....that way it will just pop open a window instead of you looking for the console window..... – Sorceri Aug 01 '16 at 21:47
  • So the MessageBox wasn't showing up either. Nothing I wrote in the `MouseDown` method was working. I remade the whole project and it started working. Thanks for the help though! – syy Aug 01 '16 at 23:55
  • @Flow How did you create the `MouseDown` eventhandler? –  Aug 02 '16 at 01:26
  • See here for hints on how to [hook up](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=14|0.0000#33276161) events! You should also look into creating the PictureBoxes dynamically, so you don't have to repeat any code 20 times. That goes for the MouseDown code as well!!! – TaW Aug 02 '16 at 07:29

1 Answers1

0

use console.Writeline that prints it to the console line.

campnerd
  • 76
  • 1
  • 1
  • 10