i have tree node in my app that was created by dynamically.when right click what ever node i want to get node text that was clicked.i use that value for my futher processing .i have tried using selected node property and it not work some times.
thanks in advance
Code:
public void CmsAppList_RightClicked(object sender, MouseEventArgs e)
{
AddUser _addUser = new AddUser(this.Text); // i want to get that value to this constructor
_addUser.ShowDialog();
}
This is a known issue with most of the list controls, to work around this add an event handler to treeview's MouseDown event and set the selected node on right click, as follows. My treeview name is treeView1 just change accordingly.
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
treeView1.SelectedNode = treeView1.GetNodeAt(e.Location);
}
Now selectednode should always reflect the node you clicked last.
Edit (To correct This is known issue and saying This is by design and right)
I've been thinking about updating this answer, becuase I realized what I said about this being known issue is wrong on multiple list controls.
Let's say it is known issue and we fix it then right click and left click have similar behavior except that right click does everything(that left click does) and then opens up context menu. This sounds fine till we carefully look at context menu(right click) nature, lets assume you select 15 files and you click (left) on 16th file the selection is lost :) so you select 15 files and the right click the context menu and operations are applicable to all the 15 files selected.
This is the reason right click shouldn't change the selection in any view (in listbox or treeview or explorer widnow). If it does then multiselect and control+select features would break, and ultimately special meaning for right click being context specific actions would be lost. That is the reason the right click wouldn't select the clicked node and that is right. The code provided above is for for specific purpose of enabling right click selection. And consider the UX impact with this kind of work around or aberrant behavior.
Related
While developing a simple Windows Form UI applications, I am trying to create an effect to show and close dropdown on mouse events.
Like I can open the dropdown on MouseMove event by setting comboBox.DroppedDown = true;
However, this same is not working when I set comboBox.DroppedDown = false; on MouseLeave event to close it.
No idea what exactly is needs to be done here.
The problem is on MouseLeave the dropdown does not lose focus and hence unless you select one item from list, it does not close. It waits for user to select an item from list.
If it can lose focus on MouseLeave, would work.
Any suggestions please.
First of all I must say that I am not an experienced programmer and I just started with WPF.
I know this question is two years old but I had the same issue and I found I can close the drop down list of the ComboBox using the event IsMouseDirectlyOverChanged. What was really annoying for me was that I had a ComboBox and a button, and If the drop down menu was opened without making a selection and I wanted to click the button, nothing happens at the first click because at the first click the drop down menu was closing. After this I could click on the button.
For me it's working fine: the drop down list close if I move the mouse in any direction (up, left, down, right) and a message is append to a textbox control. I don't know if this event is something new or it could be used 2 years ago too.
private void comPortList_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (comPortList.IsDropDownOpen==true)
{
txtMsgBox.AppendText("MouseDirectlyOverChanged\n");
txtMsgBox.ScrollToEnd();
comPortList.IsDropDownOpen = false;
}
}
This event triggers when your mouse pointer is over the opened ComboBox. If you don't open the drop down list, it will not trigger.
Another thing that I've seen is that this event triggers when you enter over the opened ComboBox and also when you leave it. If I append the text before checking if the IsDropDownOpen property is true, the text "MouseDirectlyOverChanged" will appear twice in my textbox when I the mouse pointer leaves the oppened ComboBox.
If i comment the line:
comPortList.IsDropDownOpen = false;
and leave the AppendText and ScrollToEnd before if, the text will append only once.
I hope this helps :)
It sounds to me like you need to be using the MouseEnter event and not MouseMove. The reason it wouldn't work on MouseLeave is because your mouse is moving, and that will just set it to true again.
private void calendarPlanner1_ItemClick(object sender, WeekPlannerItemEventArgs e)
{
DoDragDrop(calendarPlanner1.SelectedItem, DragDropEffects.Move);
}
private void calendarPlanner1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void calendarPlanner1_DragDrop(object sender, DragEventArgs e)
{
SRRepair repairDrag = new SRRepair();
var rows = calendarPlanner1.Rows;
var row = rows.ElementAt(calendarPlanner1.SelectedRowIndex);
row.Items.Add((WeekPlannerItem)e.Data.GetData(typeof(WeekPlannerItem)));
repairDrag = assignedRepairsList[assignedItemCollection.IndexOf(calendarPlanner1.SelectedItem)];
repairDrag.AssignedToUser = engineerList[calendarPlanner1.SelectedRowIndex];
repairDrag.Update();
}
The code above is what I have so far for drag and drop operation. It works ok until the third (drag drop) method. Basically what im trying to achieve is to drag an item between the names. I could get the index where I drag the item from using 'calendar.SelectedRowIndex' but the problem is getting the index of the destination or where i want to drag it to. Allows me to drag items but when i let go of the left button in the mouse it goes back where it came from. The calendar is an open source and i found it from codeproject and i am using and modifying it to add it in an existing desktop application.
Is there anyway or an event I could use to get the position of the mouse as soon I release the left button in the mouse?
I think, beside the drag and drop operation, you need to track your mouse move(or mouse enter) too, for getting the index of one element, drag'n'drop itself would not help(it get's the very first point), just track and identify the target by mouse move.
or simply, add mouse enter event for the object(controls) you want targeted by the drop, choose the target position via mouse enter, and complete the oration via drop, and it's better to remove mouse enter event when the drop operation completed, and add it by drop enter again.
hope I got your question true
I'm pretty sure in a desktop application you can get the mouse position without having to rely on passed mouseevent arguments.
control.mouseposition - http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition.aspx
you should just be able to get the 'point' value through 'control'.mouseposition. where the control is likely the form you're working with.
Edit
As stated in the reference, the control.mouseposition method is identical to this.cursor.position.
cursor.position - http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx
Additionally, you're going to want to locate the controls (plural), which are located at (or have ClientRectangle bounds that encompass) the cursor location point you capture..
control.GetChildAtPoint(Point).. which you may have to do recursively..
GetChildAtPoint - http://msdn.microsoft.com/en-us/library/ms158404.aspx
I wonder If it is possible to have fixed number of buttons that is shared by different tab pages. However I don't know how to implement this. Do you guys have any idea.
Heres a screenshot of my gui so that all of you can have a clearer view of what I meant.
I want that that the list of Customers, Reservations, and Check In/out will share the buttons search, edit, delete and refresh.
Is it possible? or should I create diff buttons for every tabpage?
is it correct if i do:
private void buttonSearch_Click(object sender, EventArgs e)
{
if(tabpage.SelectedIndex == 1){ then perform action..}
if(tabpage.SelectedIndex == 2) {then perform action...}
}
You could put the buttons in a User Control, add some events to the User Control (e.g. SearchClicked, EditClicked, etc.). Put the user control outside of the tabcontrol.
Then when you change tabs (TabIndexChanged), remove event handlers from the previous tab, and add event handlers for the new tab:
private void tabControl_TabIndexChanged(object sender, EventArgs e)
{
UserControl1.EditClicked -= OldEventHandler;
UserControl1.EditClicked += NewEventHandler;
}
Yes, you can change the .Parent property of the buttons at runtime - but wouldn't it be better to just move the buttons outside the tab control?
I feel you should create different buttons for every tab page as each one operates on a different entity. If there is only one set of buttons then you will have to first check which tab page is selected and then do the operation. So you will have one big method doing lots of things.
Plus there would be extra UI code that you will have to write to move the buttons when a tab page is selected.
With different buttons you will have highly cohesive and loosely coupled code. Better design. More maintainable and manageable. Less code.
I'm a beginner in C# language, so I need some help from the geniuses with this scheme: I need to add a radio button for a menu strip. I've already changed the, CheckOnClick property to true, but I need an option for radio button selection. You can see it from the Windows calculator menu bar (click View).
How can I get to it via the MenuStrip property?
I know this is a near-ancient post, but I thought it worth mentioning that although there's no native support for a RadioButton MenueItem, it's easy enough to coax their checkboxes into behaving that way. Start by setting the CheckOnClick property of each MenueItem to FALSE. Then apply the same MouseDown event handler to each item:
private void ToolStripMenueItem_MouseDown(object sender, MouseEventArgs e)
{
var thisTsmi = (ToolStripMenuItem)sender;
foreach (ToolStripMenuItem tsmi in thisTsmi.GetCurrentParent().Items)
{
tsmi.Checked = thisTsmi == tsmi;
}
}
You could use the Click event instead, but I prefer MouseDown because it provides some visualization to the user that the checked item has changed while leaving the Click event open for coding the individual items if needed.
If you navigate to
msdn.microsoft.com/en-us/library/ms404318.aspx
you will see how it's done ;)!
I have a ListBox, and its populated with items, id like to know how to:
when you rightclick in the listbox, that the rightclicked item will be selected,
a rightclick menu will be displayed with several items..
when you click on any of the items, a corresponding void will be triggered..
thanks in advance for any help, and code examples please!
This feels to me like a "homework" question, so I am going to answer by (I hope) giving you just a few pointers to solving this for yourself.
Phase One
create a sample project with a ListBox
define event handlers for MouseClick, MouseDown, and Click events.
put a Console.WriteLine("some appropriate text"); statement in each of those handlers so you can look in the Output Window in Visual Studio and see which event handler was called.
...
Phase Two
run the test program and observe the difference between what events are reported for a left mouse down and a right-mouse down (assuming your environment has the context click set to the right mouse down ... which may not be true for everyone).
focus on the one event you can intercept with a context-click.
add a context menu to the test project and set that context menu to be the context menu of the ListBox.
verify that you can now right click on an item in the ListBox and that the context menu will appear, BUT THE EVENT IS STILL HANDLED BY THE HANDLER YOU "DISCOVERED" IN STEP 2.
now go through all the Event handlers for ListBox, and figure out which one could be used to detect, given a certain location in the ListBox, which List Item was "hit."
once you can determine which List Item was right-clicked on, and you know your context menu is working, you have only the problem of making sure that the right-clicked List Item is selected : and that's pretty easy.
Figuring this out yourself will teach you several very useful things you'll be able to use later in programming to other controls.
best of luck, Bill
First, you need to subscribe to ListBox.MouseClick event. You will be able do determine what button was pressed and cursor coordinates. Then, use ListBox.IndexFromPoint method to find clicked item. You can select it using ListBox.SelectedIndex property. To display context menu use ContextMenu or ContextMenuStrip classes. Additional documentation on context menu is available in MSDN
1.when you right click in the listbox, that the right clicked item will be selected
2.a right click menu will be displayed with several items..
private void listBoxNode_MouseUp(object sender, MouseEventArgs e)
{
int location = listBoxNode.IndexFromPoint(e.Location);
if (e.Button == MouseButtons.Right)
{
listBoxNode.SelectedIndex = location; //Index selected
contextMenuStrip1.Show(PointToScreen(e.Location)); //Show Menu
}
}
3.when you click on any of the items, a corresponding void will be triggered..
private void showDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
//put your code here after clicking
//on items in context menu
}