I have a tab control that contains multiple tab controls. All of the tab controls were made using the winforms designer. The embedded tab controls each have Chart objects assigned them. These were created after the program was ran, and each chart was given three events:
chart.MouseWheel += new MouseEventHandler((sender, e) => this.Chart_MouseWheel(sender, e, chart, raw, condensed, bounds));
chart.MouseHover += new EventHandler(Chart_Hover);
chart.MouseClick += new MouseEventHandler((sender, e) => this.Chart_Click(sender, e, chart));
For easy debugging, I added a simple Console.WriteLine(); to each method to see which methods were actually being fired.
private void Chart_MouseWheel(object sender, MouseEventArgs e, Chart chart, DataTable raw, DataCondenser condensed, List<double> bounds)
{
Console.WriteLine("a");
}
private void Chart_Hover(object sender, EventArgs e)
{
Console.WriteLine("b");
}
private void Chart_Click(object sender, MouseEventArgs e, Chart chart)
{
Console.WriteLine("c");
}
After hovering, clicking, and scrolling a lot, I can only get b and c to be outputted. For some reason the scroll event will not be picked up. I have a feeling this has to do with being inside a tab control.
Any ideas why this is happening?
EDIT:
Tried a small-scale version of this and the same thing is happening.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Chart test = new Chart();
test.Width = 500;
test.Height = 500;
test.MouseWheel += new MouseEventHandler(Chart_Scroll);
test.MouseHover += new EventHandler(Chart_Hover); //mousehover event for the tooltip to activate
test.MouseClick += new MouseEventHandler(Chart_Click);
tabPage3.Controls.Add(test);
}
private void Chart_Scroll(object sender, MouseEventArgs e)
{
Console.WriteLine("a");
}
private void Chart_Hover(object sender, EventArgs e)
{
Console.WriteLine("b");
}
private void Chart_Click(object sender, MouseEventArgs e)
{
Console.WriteLine("c");
}
The same issue occurred. tabPage3 was a tabpage of a tabcontrol inside of a tab control.
EDIT 2:
so if I give the chart this event handler:
test.MouseEnter += new EventHandler(mouseEnter);
with the method:
private void mouseEnter(object sender, EventArgs e)
{
this.Focus();
}
it still doesn't work. However, if I use this:
private void mouseEnter(object sender, EventArgs e)
{
if (sender is Chart)
{
Chart temp = (Chart) sender;
temp.Focus();
}
}
It will work, even if it's embedded in other controls.
You could wire up the MouseEnter() event and give the Chart focus with a one-liner like this:
test.MouseEnter += (s, evt) => { ((Control)s).Focus(); };
Related
I am doing a simple drag and drop operation in windows forms. Whenever I start the operation the cursor changes shape. I know this is directly related to the DragDropEffects and I can't find an option that results in the default cursor. Can anybody help? Here is the code:
a.MouseDown += new MouseEventHandler(ButtonDown);
a.DragEnter += new DragEventHandler(ButtonDragEnter);
a.AllowDrop = true;
and here are the functions:
private void ButtonDown(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
ButtonClick(sender, e);
p.DoDragDrop(p.BackColor, DragDropEffects.All);
}
private void ButtonDragEnter(object sender, DragEventArgs e)
{
e.Data.GetFormats();
e.Effect = DragDropEffects.None;
Color c = new Color();
c = (Color) e.Data.GetData(c.GetType());
ButtonClick(sender, e,c);
}
Ok answered my own question here:
You first need to add an event to giveFeedBack:
a.GiveFeedback += new GiveFeedbackEventHandler(DragSource_GiveFeedback);
and here is the feedback function:
private void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
}
I have a pictureBox, the MouseEnter() event performs as follows:
private void PictureBox_pictureBox_MouseEnter(object sender, EventArgs e)
{
pictureBox.Load("..\\Debug\\Images\\highlightbutton");
pictureBox.Cursor = Cursors.Hand;
}
I want to have multiple pictureBoxes, which all refer to the same function named PictureBox_pictureBox_MouseEnter() as shown above. Is it possible to refer to the pictureBox dependant on which one is clicked? Something like this:
private void PictureBox_pictureBox_MouseEnter(object sender, EventArgs e)
{
Parent.Load("..\\Debug\\Images\\highlightbutton");
Parent.Cursor = Cursors.Hand;
}
The sender parameter is a reference to the control that raised the event:
private void PictureBox_pictureBox_MouseEnter(object sender, EventArgs e)
{
var pictureBox = sender as PictureBox;
pictureBox.Load("..\\Debug\\Images\\highlightbutton");
pictureBox.Cursor = Cursors.Hand;
}
I want to drag panels from one tableLayoutPanel to another. I also want the panels to be copied, not moved; that is, I want them to be copied (from tableLayoutPanel1 to tableLayoutPanel2), leaving the item in tableLayoutPanel1.
Can I do this? If you can give me an idea, it will be great. Thank you
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel2.AllowDrop = true;
panel3.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel2.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel2.DragDrop += panel_DragDrop;
}
private void panel3_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel3_MouseMove(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Panel)e. Data . GetData(typeof(Panel))).Parent = (Panel)sender;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
You need to create a new panel in the MouseDown handler, set its properties like those of the original, add it to the form and use DoDragDrop with this new panel.
My Windows Forms code has two superposed PictureBoxes: a small X on the top corner of a user-loaded image. I'll call them DelImage and Image, respectively. Like this:
http://imgur.com/fsW7R8i
The DelImage appears (Visible = true) on Image_MouseEnter and disappears on Image_MouseLeave. Now, I want the DelImage to have a custom behavior as well when the mouse enters it, but DelImage never fires events (nor MouseEnter, nor MouseLeave, nor Click). I've tried bringing it to front, but it already is in front of the Image.
Here's some code:
private void picImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
}
private void picImage_MouseLeave(object sender, EventArgs e)
{
delImage.Visible = false;
}
private void picDelImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
this.Cursor = Cursors.Hand;
}
private void picDelImage_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Arrow;
}
private void picDelImage_Click(object sender, EventArgs e)
{
Panel currentPanel = this.tlpImages.Controls["pnlImage"] as Panel;
currentPanel.Visible = false;
}
With this code, the picDelImage events are never reached. I never enter DelImage, apparently because I never leave Image. What can I do to make my PictureBoxes have the expected behavior?
I'm adding an array of Panel objects (which in turn contain other items) to a form at runtime. Then, I'm assigning a click event to each panel inside a loop like so:
pnlInstrument[index].Click += pnlInstrument_Click;
The empty click function looks like this:
private void pnlInstrument_Click(object sender, EventArgs e)
{
}
The event is triggering correctly, but how can I tell which panel was clicked?
Use the sender parameter of the event method..
private void pnlInstrument_Click(object sender, EventArgs e)
{
Panel panel = (sender as Panel); //This is the panel.
}
Edit: For comments of getting index..
private void pnlInstrument_Click(object sender, EventArgs e)
{
Panel panel = (sender as Panel); //This is the panel.
int panelIndex = Array.IndexOf(pnlInstrument, panel);
}
Why not:
pnlInstrument[index].Click += pnlInstrument_Click;
pnlInstrument[index].Tag += index;
private void pnlInstrument_Click(object sender, EventArgs e)
{
Panel pnl = (Panel)sender;
int index = (int)pnl.Tag;
}