I spent a few hours searching the internet for ways to take my panel and grab it from the left side and pull it left. I found many sources and I tried to alter it to my needs, but it always goes from left to right. My code I currently have is:
bool allowResize = false;
private void PanelResize_MouseUp(object sender, MouseEventArgs e)
{
allowResize = false;
}
private void PanelResize_MouseMove(object sender, MouseEventArgs e)
{
if (allowResize)
{
FavoritesPanel.Width = PanelResize.Left + e.X;
}
}
private void PanelResize_MouseDown(object sender, MouseEventArgs e)
{
allowResize = true;
}
"PanelResize" is a picturebox pushed to the left side of the panel. "FavoritesPanel" is the panel. Both are anchored to the top, bottom, and right side.
My overall question is, how can I correct my code to drag my panel from right to left?
problem is that default setting of changing Width of some panel means that it makes panel more width in way that right side is shifting to right direction.
You need two things. This what you have (resizing width) and second is that you need to shift whole panel to left side as long as you make it more width.
I have similar code here. I made code which allows you to roll up upper side of panel. So I had to do two things again: make my panel more height and shift whole panel more up. Here is my code:
public partial class Form1 : Form
{
private int actualCursorY;
private int lastCursorY;
private bool isDragged;
public Form1()
{
InitializeComponent();
}
private void barRadPanel_MouseDown(object sender, MouseEventArgs e)
{
lastCursorY = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y;
isDragged = true;
}
private void barRadPanel_MouseUp(object sender, MouseEventArgs e)
{
isDragged = false;
}
private void barRadPanel_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
actualCursorY = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y;
mainRadPanel.Location = new Point(mainRadPanel.Location.X, actualCursorY);
if (lastCursorY != actualCursorY)
{
mainRadPanel.Height -= actualCursorY - lastCursorY;
lastCursorY = actualCursorY;
}
}
}
}
I tried to use
e.Y
instead of
PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y
but it made some errors.
This is what it does:
Related
My problem is:
i want Form 2 to be moved only when I move form 1, and it will always stay beneath Form1.
i tried everything i could think of: Location point and set desktop position, i tried a timer of realtime moving , i just cant get it , this shouldn't be so difficult :(
i'm using a panel to move form 1
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mov = 1;
movX = e.X;
movY = e.Y;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mov == 1)
{
this.SetDesktopLocation(MousePosition.X - movX, MousePosition.Y - movY);
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mov = 0;
}
i also tried to make One form and put panels in it and make the form transparent, but i get an issue when i'm trying to move the form via the panel.
i found solution on this:
Move Form1 when I move Form2 in C#
in first form:
protected override void OnLocationChanged(EventArgs e)
{
if (secondForm == null)
{
secondForm = new SecondForm();
}
if (secondForm.wasShown == true)
{
secondForm.Location = new Point(this.Left - parentOffset.X,
this.Top - parentOffset.Y);
}
base.OnLocationChanged(e);
}
in the second form:
public bool wasShown = false;
private void SecondForm_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.Manual;
Location = new Point(topForm.lX -40, topForm.lY +85);
wasShown = true;
this.Owner = topForm;
}
I've created my own dragable control. The dragging is very simple:
bool moving = false; Point click = new Point(0, 0);
private void _MouseDown(object sender, MouseButtonEventArgs e)
{
moving = true;
click = Mouse.GetPosition(this);
}
private void _MouseUp(object sender, MouseButtonEventArgs e) { moving = false; }
private void _MouseMove(object sender, MouseEventArgs e)
{
if (moving == true)
{
Point po = Mouse.GetPosition(this);
this.Margin = new Thickness(this.Margin.Left + (po.X - click.X), this.Margin.Top + (po.Y - click.Y), 0, 0);
}
}
My problem is that if I drag too fast the cursor "escapes" my control. It's obvious why, however it's not too obvious how to fix this since I can't easily subscribe to every other control's mousemove in the window, and my control is small (about 35,15 px) so this happends a lot. I think that if I can easily force the mouse cursor to stay in the controll that would be a solution(not ideal, though).
So what is the bast way to fix this? How professinoal controls handle this?
P.S. I'm learning WPF, so I'm probably doing some things wrong
Your cursor leaves the usercontrol on fast moves and the MouseMove event will not be triggered anymore.
As said in the comments from the author in Drag Drop UserControls using the MouseMove event of a surrounding Canvas should help.
I've figured it out, it's very simple, using a timer.
bool moving = false; Point click = new Point(0, 0);
System.Timers.Timer _MOVER = new System.Timers.Timer();
public PersonControl()
{
InitializeComponent();
_MOVER.Elapsed += new System.Timers.ElapsedEventHandler((o, v) => { Dispatcher.Invoke(Move); });
_MOVER.Enabled = true;
_MOVER.Interval = 10;
}
private void _MouseDown(object sender, MouseButtonEventArgs e)
{
moving = true;
click = Mouse.GetPosition(this);
Canvas.SetZIndex(this, 100);
_MOVER.Start();
}
private void _MouseUp(object sender, MouseButtonEventArgs e)
{
moving = false;
Canvas.SetZIndex(this, 0);
_MOVER.Stop();
}
private void Move()
{
if (moving == true)
{
Point po = Mouse.GetPosition(this);
this.Margin = new Thickness(this.Margin.Left + (po.X - click.X), this.Margin.Top + (po.Y - click.Y), 0, 0);
}
}
Using C#.NET 4.5
I'm attempting to change the vScrollbar property of the User Control On when Entering and Off when Leaving the Control.
I used Mouse_Enter and Mouse_Leave, however when leaving across another control or the scroll bar ( that did show ), the scroll bar disappeared.
Question:
How do i check if the mouse is inside the user control field?
Question 2: How do i disable the scrollbar at the bottom of the user control? (Horisontal Scrollbar)
If you need more information or if i am unclear somewhere, then just tell me.
Any help will be appreciated, thanks in advance!
EDIT: This is the code for the user control:
public partial class EnemyStats : UserControl
{
public EnemyStats()
{
InitializeComponent();
label1.Left = (this.Width / 2) - (label1.Width / 2);
hpBar1.Width = this.Width - 8;
// Here i add the event that shows the scrollbar to all controls;
foreach (Control con in this.Controls)
{
con.MouseEnter += new EventHandler(EnemyStats_MouseEnter);
}
}
public double enemyMaxHP
{
get
{
return hpBar1.maxValue;
}
set
{
hpBar1.maxValue = value;
}
}
public double enemyHP
{
get
{
return hpBar1.Value;
}
set
{
hpBar1.Value = value;
}
}
private void EnemyStats_SizeChanged(object sender, EventArgs e)
{
if (this.Width < label1.Width) this.Width = label1.Width;
label1.Left = (this.Width / 2) - (label1.Width / 2);
hpBar1.Width = this.Width - 8;
}
private void EnemyStats_MouseEnter(object sender, EventArgs e)
{
// This scrollbar was added by dragging it from the toolbox onto the user control in the designer
vScrollbar1.Visible = true;
}
private void EnemyStats_MouseLeave(object sender, EventArgs e)
{
// This scrollbar was added by dragging it from the toolbox onto the user control in the designer
vScrollbar1.Visible = false;
}
}
But scrollbars does not work:
public void randomMethodInUserControl()
{
this.ScrollBars = ScrollBars.Vertical;
}
Here is what I've found ( Assuming you are using a Textbox )
with Textbox1 set to multi line ( and no scrollbars from the designer view and prepoulated with text ( long enough to use the Scrollbars ))
private void textBox1_MouseEnter(object sender, EventArgs e)
{
textBox1.ScrollBars = ScrollBars.Vertical;
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
textBox1.ScrollBars = ScrollBars.None;
}
How ever i did find that though this worked, when i wen't over the Scrollbar Side, it stayed on ( Similar to your going over other controls ). There for my solution to you ( though this is kind of a workaround ) would be to grab yourself a ( background color : Transparent ) label and encase your textbox with it. ( let it show in the designer to have a margin of 2-5px around your Textbox and let the textbox handle Mouse Enter and Mouse Leave events.
private void label1_MouseLeave(object sender, EventArgs e)
{
textBox1.ScrollBars = ScrollBars.None;
}
private void label1_MouseEnter(object sender, EventArgs e)
{
textBox1.ScrollBars = ScrollBars.Vertical;
}
I am trying to create a form which contains panels created programmaticaly and controls able to drag drop and resize just like Microsoft Visual Studio IDE.
And I created something like this. there should be so many lines (blue one) and also so many boxes(yellow one) and I can be able to move yellow boxes inside of blue lines. everything works with defined controls on design time.
and the source codes here
public partial class Form1 : Form
{
bool allowResize = false;
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel2.AllowDrop = true;
panel3.AllowDrop = true;
panel4.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel2.DragEnter += panel_DragEnter;
panel3.DragEnter += panel_DragEnter;
panel4.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel2.DragDrop += panel_DragDrop;
panel3.DragDrop += panel_DragDrop;
panel4.DragDrop += panel_DragDrop;
panelMove.MouseDown += panelMove_MouseDown;
}
void panelMove_MouseDown(object sender, MouseEventArgs e)
{
panelMove.DoDragDrop(panelMove, DragDropEffects.Move);
}
void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
void panel_DragDrop(object sender, DragEventArgs e)
{
((Panel)e.Data.GetData(typeof(Panel))).Parent = (Panel)sender;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
allowResize = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
allowResize = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (allowResize)
{
this.panelMove.Height = pictureBox1.Top + e.Y;
this.panelMove.Width = pictureBox1.Left + e.X;
}
}
}
but I dont know how to create thoose controls (blue and yellow boxes ) on runtime.
You should check the Anchor property of Control. Anchor allows the control to automatically re-sized at runtime.
Use the Anchor property to define how a control is automatically
resized as its parent control is resized. Anchoring a control to its
parent control ensures that the anchored edges remain in the same
position relative to the edges of the parent control when the parent
control is resized.
You can anchor a control to one or more edges of its container. For
example, if you have a Form with a Button whose Anchor property value
is set to Top and Bottom, the Button is stretched to maintain the
anchored distance to the top and bottom edges of the Form as the
Height of the Form is increased.
MSDN : Control.Anchor
we able to move windows forms when we mouse down on title bar .
but how can I move windows when mouse down in form ?
You'll need to record when the mouse is down and up using the MouseDown and MouseUp events:
private bool mouseIsDown = false;
private Point firstPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
firstPoint = e.Location;
mouseIsDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
As you can see, the first point is being recorded, so you can then use the MouseMove event as follows:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
// Get the difference between the two points
int xDiff = firstPoint.X - e.Location.X;
int yDiff = firstPoint.Y - e.Location.Y;
// Set the new point
int x = this.Location.X - xDiff;
int y = this.Location.Y - yDiff;
this.Location = new Point(x, y);
}
}
You can do it manually by handling the MouseDown event, as explained in other answers. Another option is to use this small utility class I wrote some time ago. It allows you to make the window "movable" automatically, without a line of code.
Listen for the event when the mouse button goes down in the form and then listen for mouse moves until it goes up again.
Here's a codeproject article that shows how to do this: Move window/form without Titlebar in C#
You can't use location provided in MouseUp or Down, you should use system location like this
private Point diffPoint;
bool mouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//saves position difference
diffPoint.X = System.Windows.Forms.Cursor.Position.X - this.Left;
diffPoint.Y = System.Windows.Forms.Cursor.Position.Y - this.Top;
mouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Left = System.Windows.Forms.Cursor.Position.X - diffPoint.X;
this.Top = System.Windows.Forms.Cursor.Position.Y - diffPoint.Y;
}
}
This works, tested.