Can't listen the event MouseWheel

A

alberto

I've done a little program to understand how the MouseWheel event works.
I've got a Form with a TabControl. In the first page of the TabControl
I've got two Panel objects, one inside the other so that the scrool bars
appear. When the user press a button, I create the panels as you can see.

The complete code is this:

private void toolStripButton1_Click(object sender, EventArgs e)
{
Panel cont = new Panel();
cont.Dock = DockStyle.Fill;
tab.TabPages[0].Controls.Add(cont);
cont.AutoScroll = true;

Panel p = new Panel();
p.BackColor = Color.LightYellow;
p.Size = new Size(2000, 2000);
cont.Controls.Add(p);


p.MouseWheel += new MouseEventHandler(p_MouseWheel);
}

void p_MouseWheel(object sender, MouseEventArgs e)
{
this.Text = "Hello";
}

The problem is that the MouseWheel event is never fired. Does anybody
knows why? Thank you very much
 
A

Alberto

El 17/12/2009 12:24, alberto escribió:
I've done a little program to understand how the MouseWheel event works.
I've got a Form with a TabControl. In the first page of the TabControl
I've got two Panel objects, one inside the other so that the scrool bars
appear. When the user press a button, I create the panels as you can see.

The complete code is this:

private void toolStripButton1_Click(object sender, EventArgs e)
{
Panel cont = new Panel();
cont.Dock = DockStyle.Fill;
tab.TabPages[0].Controls.Add(cont);
cont.AutoScroll = true;

Panel p = new Panel();
p.BackColor = Color.LightYellow;
p.Size = new Size(2000, 2000);
cont.Controls.Add(p);


p.MouseWheel += new MouseEventHandler(p_MouseWheel);
}

void p_MouseWheel(object sender, MouseEventArgs e)
{
this.Text = "Hello";
}

The problem is that the MouseWheel event is never fired. Does anybody
knows why? Thank you very much


Well, I've realised that the problem is that the tabcontrol is who is
sending the event MouseWheel.

The problem now is how to move the panel. Now I'm doing this:
p.Top += e.Delta *
System.Windows.Forms.SystemInformation.MouseWheelScrollLines;

but I don't undersand why the size of the scrool bar is changing.
Thank you for your help.
 
P

Peter Duniho

Alberto said:
[...]
The problem now is how to move the panel. Now I'm doing this:
p.Top += e.Delta *
System.Windows.Forms.SystemInformation.MouseWheelScrollLines;

but I don't undersand why the size of the scrool bar is changing.
Thank you for your help.

Hard to say without a concise-but-complete code example to work with.
But, it looks like you mean the scroll bar for the outer Panel instance
(the "cont" one), and if so, then sure...moving a childen within that
container without changing its size is necessarily going to change the
range of the scroll bar, and thus the size of the thumb.

The size of the scroll bar overall shouldn't change, unless the size of
the control is changing as well. That could happen if the problem is
slightly different from what it seems like you're describing. But of
course, all that means is that you should provide a concise-but-complete
code example that reliably demonstrates the problem, and should provide
a more detailed, precise description of the problem.

Pete
 
A

Alberto

El 17/12/2009 18:24, Peter Duniho escribió:
Alberto said:
[...]
The problem now is how to move the panel. Now I'm doing this:
p.Top += e.Delta *
System.Windows.Forms.SystemInformation.MouseWheelScrollLines;

but I don't undersand why the size of the scrool bar is changing.
Thank you for your help.

Hard to say without a concise-but-complete code example to work with.
But, it looks like you mean the scroll bar for the outer Panel instance
(the "cont" one), and if so, then sure...moving a childen within that
container without changing its size is necessarily going to change the
range of the scroll bar, and thus the size of the thumb.

The size of the scroll bar overall shouldn't change, unless the size of
the control is changing as well. That could happen if the problem is
slightly different from what it seems like you're describing. But of
course, all that means is that you should provide a concise-but-complete
code example that reliably demonstrates the problem, and should provide
a more detailed, precise description of the problem.

Pete
Ok. Here is the complete code:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void toolStripButton1_Click(object sender, EventArgs e)
{
Panel cont = new Panel();
cont.Dock = DockStyle.Fill;
tab.TabPages[0].Controls.Add(cont);
cont.AutoScroll = true;

Panel p = new Panel();
p.BackColor = Color.LightYellow;
p.Size = new Size(2000, 2000);
cont.Controls.Add(p);

tab.MouseWheel += new MouseEventHandler(tab_MouseWheel);
}

void tab_MouseWheel(object sender, MouseEventArgs e)
{
Panel p =(Panel)
((TabControl)sender).TabPages[0].Controls[0].Controls[0];

p.Top += e.Delta;
}
}

In the form there is only a toolStrip with a button and a TabControl
with 2 tabs.
Thank you.
 
P

Peter Duniho

A

Alberto

Yes, it's all the code. Do you want the designer.cs of the form??
Peter Duniho said:
Alberto said:
[...] you should provide a concise-but-complete
code example that reliably demonstrates the problem, and should provide
a more detailed, precise description of the problem.

Ok. Here is the complete code:

No, not really.

You may find these links helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/

As I said, you also need a clearer description of the problem.

Pete
 
A

Alberto

El 17/12/2009 21:11, Peter Duniho escribió:
Alberto said:
[...] you should provide a concise-but-complete
code example that reliably demonstrates the problem, and should provide
a more detailed, precise description of the problem.

Ok. Here is the complete code:

No, not really.

You may find these links helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/

As I said, you also need a clearer description of the problem.

Pete

Here is the form1.designer.cs. The code couldn't more concise and you
have ALL the code now:

partial class Form1
{
/// <summary>
/// Variable del diseñador requerida.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Limpiar los recursos que se estén utilizando.
/// </summary>
/// <param name="disposing">true si los recursos administrados
se deben eliminar; false en caso contrario, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Código generado por el Diseñador de Windows Forms

/// <summary>
/// Método necesario para admitir el Diseñador. No se puede
modificar
/// el contenido del método con el editor de código.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources =
new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton1 = new
System.Windows.Forms.ToolStripButton();
this.tab = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.toolStrip1.SuspendLayout();
this.tab.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new
System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(758, 25);
this.toolStrip1.TabIndex = 1;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.DisplayStyle =
System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton1.Image =
((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
this.toolStripButton1.ImageTransparentColor =
System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(23, 22);
this.toolStripButton1.Text = "toolStripButton1";
this.toolStripButton1.Click += new
System.EventHandler(this.toolStripButton1_Click);
//
// tab
//
this.tab.Controls.Add(this.tabPage1);
this.tab.Controls.Add(this.tabPage2);
this.tab.Dock = System.Windows.Forms.DockStyle.Fill;
this.tab.Location = new System.Drawing.Point(0, 25);
this.tab.Name = "tab";
this.tab.SelectedIndex = 0;
this.tab.Size = new System.Drawing.Size(758, 475);
this.tab.TabIndex = 2;
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(750, 449);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(192, 74);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(758, 500);
this.Controls.Add(this.tab);
this.Controls.Add(this.toolStrip1);
this.Name = "Form1";
this.Text = "Form1";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.tab.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton1;
private System.Windows.Forms.TabControl tab;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;

}
 
P

Peter Duniho

Alberto said:
Here is the form1.designer.cs. The code couldn't more concise and you
have ALL the code now: [...]

First of all, that's not true. A _complete_ code example can be
compiled directly, without _any_ additional work. The code you posted
so far a) requires reassembly in a new project, and b) doesn't even
include an entry point.

Now, you have finally posted enough code to make such a reassembly at
least possible. But the code still doesn't run. An exception is thrown
during the execution of the InitializeComponent() method, due to a
missing image resource. The code example should not rely on special
image resources anyway, but if you insist, you need to provide that as
part of the _complete_ code example.

All that said, I can comment out the offending line and get the program
to run, and in doing so all I have done is confirm that my original
guess was exactly correct, and that even though you claim "the size of
the scroll bar is changing", it's not the scroll bar size that's
changing, but rather the thumb within the scroll bar, and it's changing
size for exactly the reason I described.

You are moving the child in the container (or more specifically, moving
the _top_ of the child, changing that child's height), and so the size
of the scroll thumb has to change to reflect that change in range of
scrolling.

So, rather than going through all of this hassle, you could have just
read my original reply and understood the behavior you described based
on that.

Of course, in all of this, not once have you explained what it is you're
actually trying to do. Are you trying to adjust the scroll position of
the outer panel? If so, why not just adjust that position directly,
rather than manipulating the child control?

I suspect you want something like this in your mouse event handler:

Point ptT = panel.AutoScrollPosition;

ptT = new Point(-ptT.X, -(ptT.Y + e.Delta));
panel.AutoScrollPosition = ptT;

(where "panel" is a variable referencing the _outer_ panel in your tab
page).

Note that the AutoScrollPosition is sort of weird. When setting it, you
have to provide positive coordinate values. But if you get it, the
coordinate values are negative. I find it very annoying, but you pretty
much just have to live with it.

If that code doesn't do what you want, then you need to explain exactly
what it is that's different from what the code is doing that you want it
to do. Be specific, clear, and unambiguous.

Pete
 
A

Alberto

El 18/12/2009 21:30, Peter Duniho escribió:
Alberto said:
Here is the form1.designer.cs. The code couldn't more concise and you
have ALL the code now: [...]

First of all, that's not true. A _complete_ code example can be compiled
directly, without _any_ additional work. The code you posted so far a)
requires reassembly in a new project, and b) doesn't even include an
entry point.

Now, you have finally posted enough code to make such a reassembly at
least possible. But the code still doesn't run. An exception is thrown
during the execution of the InitializeComponent() method, due to a
missing image resource. The code example should not rely on special
image resources anyway, but if you insist, you need to provide that as
part of the _complete_ code example.

All that said, I can comment out the offending line and get the program
to run, and in doing so all I have done is confirm that my original
guess was exactly correct, and that even though you claim "the size of
the scroll bar is changing", it's not the scroll bar size that's
changing, but rather the thumb within the scroll bar, and it's changing
size for exactly the reason I described.

You are moving the child in the container (or more specifically, moving
the _top_ of the child, changing that child's height), and so the size
of the scroll thumb has to change to reflect that change in range of
scrolling.

So, rather than going through all of this hassle, you could have just
read my original reply and understood the behavior you described based
on that.

Of course, in all of this, not once have you explained what it is you're
actually trying to do. Are you trying to adjust the scroll position of
the outer panel? If so, why not just adjust that position directly,
rather than manipulating the child control?

I suspect you want something like this in your mouse event handler:

Point ptT = panel.AutoScrollPosition;

ptT = new Point(-ptT.X, -(ptT.Y + e.Delta));
panel.AutoScrollPosition = ptT;

(where "panel" is a variable referencing the _outer_ panel in your tab
page).

Note that the AutoScrollPosition is sort of weird. When setting it, you
have to provide positive coordinate values. But if you get it, the
coordinate values are negative. I find it very annoying, but you pretty
much just have to live with it.

If that code doesn't do what you want, then you need to explain exactly
what it is that's different from what the code is doing that you want it
to do. Be specific, clear, and unambiguous.

Pete
I only want to move the inner panel (the other panel is only a
container) with the mouse wheel just like if a move the vertical scroll
bar of the outer panel.
 
P

Peter Duniho

Alberto said:
I only want to move the inner panel (the other panel is only a
container) with the mouse wheel just like if a move the vertical scroll
bar of the outer panel.

Did you try my suggestion? If not, why not? If so, what about the
suggestion fails to address your question or goals?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top