so i have a flow panel and a button that adds listviews to it at run time. i have my doubleclick events set up - is it possible to set up some kind of click (or click and drag) event to rearrange the controls in the flow panel?
i know we can change the sort strategy (top down, left right) and wrap, but i was hoping for organization a user could simply drag the control from one spot and relocated somewhere else.
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
newListView.DragDrop += listView_DragDrop;
newListView.DragEnter += listView_DragEnter;
newListView.DoubleClick += listView_DoubleClick;
flowPanel.Controls.Add(newListView);
}
This will move a control to the top of the panel and move the remaining controls down.
FlowLayoutPanel1.Controls.SetChildIndex(myControl, 0);
For drag and drop re-ordering, you'll have to hook up drag and drop events for each control. On the drop event get the control being dragged and the index position of the target control. Then change the index with SetChildIndex. If your still working on this problem, I can dig up some code to show you.
Related
I am working on a simple C# program that allows the user to use the mouse to move simple controls (Buttons, textareas, etc), similar to a visual designer like visual studio. The controls are contained in a panel and they all work as expected. However, when I call MouseDown() on the panel the controls are contained in, the event only fires when clicking on an empty part of the panel, not when I click on a Control contained within the form.
Here is my MouseDown() Code:
private void splitContainer2_Panel2_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("MOUSE GRABBED");
...
//More code that uses the X and Y co-ords of the mouse to check which
//Control is selected
...
}
As you can see it is very straightforward. The writeLine() is not triggered when I click on a control.
I have looked at questions such as:
ignore mouse event on label inside panel
To no avail.
Any help would be appreciated, even a better method to do what I am trying to acomplish.
Instead of using a MouseDown() event for the panel, why not trying to use the same one for every object? Based on what you are trying to do, add the following code:
Where you create each form element:
nameOfElement.MouseDown += new System.Windows.Forms.MouseEventHandler(this.splitContainer2_Panel2_Objects_MouseDown);
The mouse down method:
Control ctrl = (Control)sender;
Console.WriteLine("Moused down on: " + ctrl.Name);
//Code to manipulate mouse down
Hope this helps!
Use PreviewMouseDown. The WPF hit testing engine will not raise events on parent elements if the child element absorbs the event.
I'm trying to put together a drag & drop solution in WPF TreeView control, using these techniques:
Dragging and dropping to a TreeView, finding the index where to insert the dropped item
When the user clicks on a TreeViewItem, first the treeViewItem_MouseLeftButtonDown gets executed, then the treeViewItem_Drop also. At every single click.
It sounds like you're calling DragDrop.DoDragDrop() from the treeViewItem_MouseLeftButton handler. The treeViewItem_Drop even is raised when the mouse button is released, so you're getting a drop event on every mouse click. Try calling DoDragDrop from a treeViewItem_MouseMove handler instead. Just make sure that the left mouse button is pressed before calling DroDragDrop. You may also want to make sure the mouse has moved a minimum distance before starting the DragDrop operation as well, such as
if(e.LeftButton == MouseButtonState.Pressed
&& horizontal_move > SystemParameters.MinimumHorizontalDragDistance)
{
DragDrop.DoDragDrop();
}
My scenario is the following:
I am working on a winforms application in C# that has a button inside the main page of a tabcontrol that will generate another tabpage each time that it is clicked. Each new tabpage will contain a layout defined by a user control.
My Questions are:
How can I allow the user to then close one of the tabs that were created dynamically at runtime?
How might I go about modifying the tabcontrol itself so that it has a small 'X' in each tab that the user may click on in order to close that particular tab? (Like Firefox has)
How can I expose the SelectedIndex property of the tabcontrol to the user control if I want to close the tab with a button inside the user control instead?
I found this code and was very helpful to me:
private void tabControl_MouseUp(object sender, MouseEventArgs e)
{
// check if the right mouse button was pressed
if(e.Button == MouseButtons.Right)
{
// iterate through all the tab pages
for(int i = 0; i < tabControl1.TabCount; i++)
{
// get their rectangle area and check if it contains the mouse cursor
Rectangle r = tabControl1.GetTabRect(i);
if (r.Contains(e.Location))
{
// show the context menu here
System.Diagnostics.Debug.WriteLine("TabPressed: " + i);
}
}
}
}
TabControl: How To Capture Mouse Right-Click On Tab
I created a derived tab control about one year ago. I am not going to post the source here, because it's about 700 lines long and coded quite messy. Maybe I will find some time to clean the code up and then release it here. For now I will briefly outline the way it is build.
Each tab page has a 'X' icon to the left of the title and the tab pages support reordering by drag and drop and moving them between multiple tab control.
I choose the easy way to get the icon on the tab pages. The tab control has the TabControl.ImageList property and a tab page has a TabPage.ImageIndex property. So I just added three icons to a image list - normal, hover, pressed - and process the mouse events.
With TabControl.GetTabRect() you can test if the mouse is over a specific tab pages and with some math you find if it is over the icon. Then you just need to change the icon depending on the mouse button state and eventually remove the tab page under the mouse if the button was pressed.
The main problem with this solution is, that calculating if the mouse is over the icon requires to know where the icon is painted relative to the tab page and this might change with a new windows version. And the icon is to the left of the title, but that does not look too bad.
I did the following:
on the create (add) TabPage stage, I added a toolStrip
ToolStrip ts = new ToolStrip();
ts.Dock = DockStyle.Top;
ts.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
Then, create the X button and add it to toolstrip
ToolStripButton ToolStripButton = new ToolStripButton("X");
ts.Items.Add(ToolStripButton);
create an event on clicking the X button
ToolStripButton.Click += new EventHandler(ToolStripButton_Click);
add toolstrip to the tabpage
tabControl1.TabPages[curenttabpage].Controls.Add(ts);
now for the ToolStripButton_Click is as follows:
void ToolStripButton_Click(object sender, EventArgs e)
{
ToolStripButton t = (ToolStripButton)(sender);
ToolStrip ts = t.Owner;
TabPage tb = (TabPage)
(ts.Parent);tabControl1.TabPages.Remove(tb);
}
Maybe it is not as you want, but it will work well.
I created a setup that is similar.
Each control that is added to the tab page at runtime is derived from a special base control I created. This base control has a close button (along with some other features such as safe to close flag).
Close tab code I'm using on my base control:
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(parent);
I know this is an old thread but I did find this link that will allow you to "hide" tabs in an array and then you can just re-load the tabs you want at run time. I added this more for a place I can easily find it again.
This code might help throgh closing the tab controls with middle mouse click :
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Middle)
return;
for (int i = 0; i < MainTabControl.TabPages.Count; i++)
{
if (this.MainTabControl.GetTabRect(i).Contains(e.Location))
{
this.MainTabControl.TabPages.RemoveAt(i);
return;
}
}
}
It´s works!
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(tabpage);
TextBoxes and NumericUpDowns have the odd property of not allowing you to deselect them once they are selected. When my user selects a NumericUpDown and clicks else-where on the form, the NumericUpDown should be deselected.
Unfortunately, this is not the case. Currently I am just handling the MouseDown event of all other controls on the form (like the panels and actual form itself) and just calling the Focus method of a random label to remove the focus from the NumericUpDown. However, this cannot be applied to menu items or scrollbars.
There must be a better way to do this. The user may want to scroll the panel instead of the NumericUpDown and intuitively click the Panel and then use the scroll-wheel, but currently that would scroll the NumericUpDown instead, since it still has focus.
Thanks for reading.
Edit: Problem still unsolved.
Normally Panel Control is a Non-Focusable control. Therefore clicking on Panel will NOT remove focus from TextBox or NumericUpDown Countrol.
The workaround can be, place a button on panel and move it away from view for example setting its x = -100 and y = -100. Do NOT set visible = false.
Now whenever user clicks on Panel (Panel_Click event) set focus (Button.Focus()) to that button. In this way panel will be scrollable through scroll-wheel.
Enclose the numeric box within a panel of some sort and then do
panel1.MouseHover += new EventHandler(panel1_MouseHover);
private void panel1_MouseHover(object sender, EventArgs e)
{
if (numericUpDown1.Focused)
{
panel1.Focus();
}
}
I tested it and it works.!
I have a list of controls that I am displaying via a WrapPanel and it horizontally oriented.
I have implemented a "Click and Drag" scrolling technique so that the user scrolls with the mouse via clicking and dragging.
Like so:
<Canvas x:Name="ParentCanvas" PreviewMouseDown="Canvas_MouseDown" MouseMove="Canvas_MouseMove">
<WrapPanel>
<WrapPanel.RenderTransform>
<TranslateTransform />
</WrapPanel.RenderTransform>
<!-- controls are all in here ... -->
</WrapPanel>
</Canvas>
Then in the code behind:
private Point _mousePosition;
private Point _lastMousePosition;
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
_lastMousePosition = e.GetPosition(ParentCanvas);
e.Handled = true;
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
_mousePosition = e.GetPosition(ParentCanvas);
var delta = _mousePosition - _lastMousePosition;
if(e.LeftButton == MouseButtonState.Pressed && delta.X != 0)
{
var transform = ((TranslateTransform)_wrapPanel.RenderTransform).Clone();
transform.X += delta.X;
_wrapPanel.RenderTransform = transform;
_lastMousePosition = _mousePosition;
}
}
This all works fine
But what I want to do is make it so that when a users clicks to drag, the items within the WrapPanel dont respond (i.e. the user is only browsing), but when the user clicks (as in a full click) then they do respond to the click.
Just like how the iphone works, when you press and drag directly on an app, it does not open the app, but rather scrolls the screen, but when you tap the app, it starts...
I hope this makes sense.
Cheers,
Mark
I believe you'll need to capture the mouse. The problem is you'll be contending with the controls (such as Button) that will also be trying to capture the mouse.
In your MouseDown event (probably PreviewMouseDown actually) you can use e.MouseDevice.Capture(_wrapPanel, CaptureMode.Element). This should direct all mouse input to the _wrapPanel and not any subtree elements.
In your MouseUp event, you'll need to release the capture by calling e.Mousedevice.Capture(null). If no scrolling has taken place you'll want to send a "click" to the control that normally would have received the click which I'm not quite sure about. Perhaps you can use the Automation Peer classes to do this?
The trick is that certain controls will not work properly if you withhold mouse events from them. Consider a slider for example. How would the slider ever be usable inside a panel that works like this?
Another, and in my opinion better, solution is to:
Add a PreviewMouseDown handler in which you set Handled=true and record the parameters including the position and set a "maybeClick" flag (unless your "recursion" flag is set), and sets a timer.
Add a MouseMove handler that clears the "maybeClick" flag if the mouse moves more than an epsilon away from the position recorded for the PreviewMouseDown.
Add a PreviewMouseUp handler that checks the "maybeClick" flag - if true, it sets the "recursion" flag, does an InputManager.ProcessInput to re-raise the original PreviewMouseDown, and then clears the "recursion" flag.
In the timer, do the same thing as for PreviewMouseUp so the click will only be delayed a moment.
The net effect is to delay a PreviewMouseDown event until you have had time to check whether the mouse moved or not.
Some things to note about this solution:
Setting Handled=true on PreviewMouseDownEvent also stops the MouseDownEvent.
The recursive call is ignored in the PreviewMouseDown handler because the recursion flag is set