I am struggling with something that I think should be easily (ish). I have a windows form and a flowgridlayout panel at the bottom of the form. Inside this form I dynamically populate it with X number of User Controls. The controls are all the same type.
The goal is when the user hoovers the mouse over the user control it opens another form and positions it where the mouse is. When mouse leaves the form the opened form disappears.
This almost works great. The problem is when the User Control has anything like a label or text box inside it. It is considered to have left the UC so the form disappears.
My thought was then to use the X and Y to tell if it is inside the UC but I can not figure this out.
Can I ask:
1) What is the best approach to this?
2) How can I code it, as the UC's are dynamic I can not know exactly where they will be.
Thanks
EDIT
I am trying to figure out the mouse pointers but not getting there. The code below is within the UC SmallTagBox_MouseLeave event:
Point loc = this.Location;
Point p = this.PointToScreen(this.Location);
Point p2 = this.PointToScreen(this.Parent.Location);
Point ms = MousePosition;
Rectangle screenBounds = new Rectangle(this.PointToScreen(this.Location), this.Size);
if (!screenBounds.Contains(ms))
{
thw.Close();
thw = null;
}
loc {X = 275 Y = 3} System.Drawing.Point
p {X = 808 Y = 908} System.Drawing.Point
p {X = 808 Y = 908} System.Drawing.Point
p2 {X = 545 Y = 1542} System.Drawing.Point
ms {X = 574 Y = 914} System.Drawing.Point
screenBounds {X = 808 Y = 908 Width = 62 Height = 29} System.Drawing.Rectangle
I do not understand how p2 (its parent) can have a greater Y value relative to the screen.
Hooking all the controls MouseEnter and MouseLeave events, then figuring out if it is still inside the form is pretty painful. A simple timer can get the job done too:
public partial class Form1 : Form {
private Timer mTimer;
public Form1() {
InitializeComponent();
mTimer = new Timer();
mTimer.Interval = 200;
mTimer.Tick += mTimer_Tick;
mTimer.Enabled = true;
}
private void mTimer_Tick(object sender, EventArgs e) {
if (!this.DesktopBounds.Contains(Cursor.Position)) this.Close();
}
}
Idea 1) When the MouseLeave event fires, you can check the mouse coordinates (relative to screen) and check if they're still within the bounds of your usercontrol. If they are, it should be assumed that the mouse has to pass back through the control to get outside the bounds, and you can safely ignore the event this time.
Idea 2) Attach MouseEnter event handlers to the child controls. Then when the mouse enters one, you will know and can ignore the usercontrol's MouseLeave event. Then when the child's MouseLeave event fires, check for the usercontrol's MouseEnter again.
I think I would add an event handler for MouseLeave for every control that you have and use the Parent property to find the User Control you are after. I agree, it will be a bit painful though.
You can also loop through all the child controls (recursive) on your control, and attach a MouseEnter and MouseLeave event to them as well.
You have to do some bookkeeping if the mouse is in your control, or some child control.
Related
I am creating a small game that resizes the main window to fit a growing board. I want to re-position the window to keep the button that was clicked under the mouse after resizing. Currently, when clicked the board gets wider and moves the button away from the mouse.
How do I define the button as the anchor point when I move the window?
I don't know if there is a way to use the button as an anchor point. But after a little thinking, i came up with a code that can work for you.
Basically, i'm using the mouse relative to the button position before and after the resize to move the window accordingly. I hope it helps.
private void Btn1Click(object sender, RoutedEventArgs e)
{
int widthGrowth = 50;
int heightGrowth = 80;
Button btn = sender as Button;
Point oldMousePosition = Mouse.GetPosition(btn);
Width += widthGrowth;
Height += heightGrowth;
Point newMousePosition = Mouse.GetPosition(btn);
Left += newMousePosition.X - oldMousePosition.X;
Top += newMousePosition.Y - oldMousePosition.Y;
}
I am creating a form which looks like this.
Form1 has two panels:
the bottom panel (yellow)
the top panel, which is himself composed by two panels:
the left panel (red)
the right panel (green)
As the image shows, the top panel, exceeds form1's height, so I would like to use a scrollbar. I have several problems:
a) Setting auto-scroll to true in the top panel does not work.
b) If I put a button on the top panel whose location exceeds form1's height, then it scrolls, but NOT if I put it in any of its subpanels (red or green)
c) Instead of setting auto-scroll to true in the top panel, I could create my own scrollbar, but I just couldn't find a simple example handling the scroll event that moves the panel so that the view port changes as desired.
How could I properly add a scrollbar with the desired behaviour?
As LarsTech and Hans Passant suggested, AutoScrollMinSize worked, so a and b questions were solved.
Regarding question C, I thought there was a way to create a scrollbar so that it behaves like I guess 99% of scrollbars should behave (just to move a desired panel). Finally, I coded that logic myself.
I added a Scrollbar1 as a control of the basic form. initialPanelY is catched on the constructor.
This is the code that responds to the scroll event. I just move the panel panelRest2's Y location depending on the scrollbar value
using System;
.
.
using System.Windows.Forms;
namespace WindowsFormMyTests
{
public partial class Form1 : Form
{
int initialPanelY;
public Form1()
{
InitializeComponent();
initialPanelY = this.panelRest2.Location.Y;
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
int v = (sender as VScrollBar).Value; //between 1 and 100
int hiddenPanelHeight = this.panelRest2.Size.Height - this.Size.Height;
float moveY = (float)hiddenPanelHeight * ((float)v / 100);
int newY = initialPanelY - (int)moveY;
this.panelRest2.Location = new Point(this.panelRest2.Location.X, newY);
}
}
}
So I'm trying to make a slider. I'm using my cursor to move a button's x position.
I have 3 functions, the mouseDown, mouseUp and the mouseMove function. In the mouseUp and mouseDown functions I set a variable to true and false to tell the program that the mouse is clicked or not. In the mouseMove function I tell the program to set the button's x position to the x position of the mouse when the mouse is clicked. This works but has 2 problems.
The first problem is that when I press the button and move it, the button moves along with the mouse's x but it has a space between the mouse and the button. It looks a bit like this:
CURSOR.......BUTTON
The space between the cursor and button change when I change the resolution of the form.
The second problem is that when I move the button it flickers a bit. It only does this at higher speeds but it is a problem in my case.
My code looks like this:
bool mouseDown = false;
private void volumeGrabBT_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = true;
}
}
private void volumeGrabBT_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = false;
}
}
private void volumeGrabBT_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == true)
{
Point volumeBTPoint = new Point();
volumeBTPoint.X = Cursor.Position.X;
volumeBTPoint.Y = volumeGrabBT.Location.Y;
volumeGrabBT.Location = volumeBTPoint;
}
}
The volumeGrabBT is the button I'm trying to move along with the mouse.
The volumeBTPoint is the point of the button I'm trying to set the button's position to.
I hope someone can help me fix these problems. Thanks in advance!
I believe that flickering can be fixed by setting some additional form styles: SetStyle(ControlStyles.AllPaintingInWmPaint |ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
in form's constructor. It will use double buffering and generally just draws faster.
For the Cursor class, it's relative to the screen, not your form. You can use this.PointToClient() function to get client's space position of cursor, like this:
Point clientCursor = this.PointToClient(Cursor.Position);
and then use clientCursor to get exact X in your client space.
You have to translate screen coordinates to client coordinates.
Point volumeBTPoint = new Point();
Point point = this.PointToClient(Cursor.Position);
volumeBTPoint.X = point.X;
volumeBTPoint.Y = volumeGrabBT.Location.Y;
volumeGrabBT.Location = volumeBTPoint;
Instead of this you should use button's parent control (Panel, GroupBox, etc).
I am trying to create a elipse in a textbox on double click. But it doesnt seem to happen.
panel.MouseClick += create_terms;
private void create_terms(object sender, EventArgs arg)
{
if (Phys_terms_check.Checked == true)
{
MouseEventArgs e = (MouseEventArgs)arg;
Graphics g = CreateGraphics();
SolidBrush p = new SolidBrush(Color.Red);
Pen erase = new Pen(Color.White);
Panel panel = (Panel)sender;
g.FillEllipse(p, e.X+panel.Left,e.Y+panel.Top,10,10);
}
}
The e.x and e.y seem to be giving relative coordinates from the sender. How to get point relative to the form.
add sender's top and left coordinates.
g.FillEllipse(p, e.X + textbox.Left, e.Y + textbox.Top, 10, 10);
but, this won't show, because textbox paint event fill fire and repaint textbox.
First of all: TextBoxes are old legacy and rather special Controls that do not support all things normal controls let you do.
Among the things that won't work are
Setting a BackgroundImage
Owner-drawing them
The latter includes any drawing in its Paint/OnPaint events.
You can code and hook-up the Paint event, but it won't get called.
You still can draw onto a TextBox using CreateGraphics, if you do it right, but as always with this function the result is non-persistent and will go away as soon as the system refreshes the TextBox itself, which is super-fast: as soon as you move your cursor over it the circle you draw may disappear..
The code to do it would have to look similar to this:
Graphics g = yourTextBox.CreateGraphics();
g.FillEllipse(Brushes.Red, yourTextBox.Width - 22, 2, 11, 11);
But as I said this will not persist, so it has little or no value.
If you want to draw onto something with a visible Text property you can use a Label:
private void yourLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Red, yourLabel.Width - 22, 2, 11, 11);
}
The result looks the same, but only the dot in the Label will persist, e.g. a Minimize-Maximize of the form.. In fact the dot in the TextBox didn't even survive calling my screenshot program, so I had use resort to pressing the Print-Key !
For drawing circles upon mouseclicks onto normal controls see this post!
Hello it might be a silly question but i can't figure out the problem here.. here is my code to fill a form with a single block:
private void drawBackground()
{
Graphics g = genPan.CreateGraphics();
Image Block = Image.FromFile(#"C:\Users\Administrator\Desktop\movment V1\movment V1\images\BrownBlock.png");
float recWidth = Block.Width;
// rectangle width didnt change the name from previous code, it's picture width.
float recHeight = Block.Height;
// rectangle Heightdidnt change the name from previous code, it's picture Height.
float WinWidth = genPan.Width; // genPan is a panel that docked to the form
float WinHeight = genPan.Height;
float curWidth = 0; //indicates where the next block will be placed int the X axis
float curHeight = 0;//indicates where the next block will be placed int the Y axis
while ((curHeight + recHeight) <= WinHeight)
{
if (curWidth >= WinWidth / 3 || curWidth <= WinWidth / 1.5 ||
curHeight >= WinHeight / 3 || curHeight <= WinHeight / 1.5)
{
g.DrawImage(Block, curWidth, curHeight, recWidth , recHeight );
}
curWidth += recWidth;
if ((WinWidth - curWidth) < recWidth)
{
curWidth = 0;
curHeight += 50;
}
}
}
If I launch this func through a button it will work perfectly fine. But if I launch the func after the InitializeComponent(); method in the constructor OR in a FORM shown event, while the button is still on the form it will execute the func however the block backgroud wont be visible but the grey color will be. but if i remove the button the background will be visible. =\
I cant understand why is it happening, how to fix it and what am I doing wrong.. can anyone explain please..?
You can't really do that using your current logic. The problem is that the control (genPan panel in your case) has its own Paint event that when called, overwriting any graphics you used on it.
Even when you draw in button click it works only until the form is repainted e.g. try focus other window and focus your form again: you will lose what you have drawn.
Proper way to do such things is to write your own class that inherit from some basic control (Panel in your case) then overriding its OnPaint event and draw whatever you want there.
So first, have such class:
public class BlockBackgroundPanel : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Image Block = Image.FromFile(#"C:\Users\Administrator\Desktop\movment V1\movment V1\images\BrownBlock.png");
float recWidth = Block.Width;
//rest of your code, replace "genPan" with "this" as you are inside the Panel
}
}
Then in your .Designer.cs file (you can open it in the Studio) change the code so that genPan will be of your new class instance:
private BlockBackgroundPanel genPan;
//...
this.genPan = new BlockBackgroundPanel ();
If you need to draw background only based on some condition/action/user interaction...
Put the call to this funciton into the forms OnPaint method and enable it only if some bollean variale equals true. And that boolean becomes true, only on button click.
Some hypothetical example:
protected override OnPaint(...) //FORMS ONPAINT OVERRIDE
{
if(needBackGround) //INITIAL VALUE AT STARTUP IS FALSE
drawBackground();
}
public void ButtonClickHandler(...)
{
needBackGround= !needBackGround; //INVERSE THE VALUE OF BOOLEAN
}
This is clearly just a sniplet to give you a hint and not a real code. There could be other problems you will need to face, like: flickering, handling resize, performance... but this is just a point to start.