DataGridView right-click menu/copy example? - winforms

I have a DataGridView (dgv1) on my form. In a particular cell, I'd like for the user to be able to right-click and choose "COPY" to copy the contents of the cell to the clipboard. Can anyone point me in the direction of a tutorial or site that shows how to accomplish this in C#?
Thanks!

You can use ContextMenuStrip to accomplish this.
(Or ContextMenu for pre-VS2k5)
Excerpt from this article:
ContextMenuStrip mnu = new ContextMenuStrip();
ToolStripMenuItem mnuCopy = new ToolStripMenuItem("Copy");
ToolStripMenuItem mnuCut = new ToolStripMenuItem("Cut");
ToolStripMenuItem mnuPaste = new ToolStripMenuItem("Paste");
//Assign event handlers
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
//Add to main context menu
mnu.Items.AddRange(new ToolStripItem[] { mnuCopy, mnuCut, mnuPaste});
//Assign to datagridview
dataGridView1.ContextMenuStrip = mnu;
There is more information on the above link.

You might want to create a contextmenustrip for the COPY option when you right click.
And in the datagridview properties in the rightclick eventhandler, you link this contextmenustrip.
And on clicking copy, you have another function where you say Clipboard.settext(Datagriditem.value)
This link should help you figure out how to get the right click menu.
right click context menu for datagridview
And use the Clipboard.Setdataobject to get the data into clipboard.

Related

Not able to click on the drop down element

In our application when i mouse over a menu item, drop down appears. Where i want to select an item by clicking on it.
Main Menu:Admin
Sub menu: Manage Channels, Manage Users
In selenium webdriver, i tried to click directly on Manage Channels by giving the xpath, linktext,partial link text.
But all says unable to locate element. i'm attaching image of the screen shot for reference
Please check it
driver.findElement(By.linkText("Manage Channels")).click();
driver.findElement(By.xpath("//li/a[contains(., \"Manage Channels\")]")).click();
driver.findElement(By.partialLinkText("Manage Channels"));
http://farm8.staticflickr.com/7454/9490144055_1f7da5eaf1_m.jpg
I used the below code which finally solved my question.
WebElement ManageChannels = driver.findElement(By.linkText("Admin"));
Actions builder = new Actions(driver);
Action mouseOverAdmin = builder.moveToElement(ManageChannels).build();
mouseOverAdmin.perform();
driver.findElement(By.linkText("Manage Channels")).click()
i managed to click on item in the drop down. thank you very much for the effort

Trigger Populating Event in AutoCompleteBox

I have a Silverlight dialog and it contains an autocompletebox. When the dialog opens I would like the autocompletebox populated with text and the dialog opening with suggestions pre-populated without any user input.
Does anyone know how to do this? I am looking for a way to trigger the populating event.
Thanks.
Found the answer to this. You need to set the text in the underlining textbox, and when you do this the autocomplete dropdown box will display. You need to use the VisualTreeHelper to get the textbox.
DependencyObject o = VisualTreeHelper.GetChild(autocompleteBox, 0);
o = VisualTreeHelper.GetChild(o,0);
((TextBox)(o)).Text = "yourtexthere";

WPF trigger code behind button to display second form

I am a WPF novice.
I have created a form containing a combo box with which to choose a multi-field key value(populated from an XML data file).
I have also created a second WPF form which is available to display all field values from the record associated with the multi-field key value chosen from the first form.
I need to be able to click a button which will cause the second form to be displayed, with all fields filled in which are associated with the chosen key field values.
How do I go about writing such an event trigger using C#?
couple of steps (this is not really MVVM, BTW) ...
first, add a click handler to your button
second, in the click handler code, instantiate your new form
third, set the data context, etc for the new form
forth, show the new form by calling .Show()
in your xaml add a click handler to the button in question....
<Button Click="myClickHandler"/>
in visual studio, you can right click the text in the click="" and choose to navigate to the handler and visual studio will generate the code for it for you.
in your click handler, in code behind, do something like this....
public void myClickHandler(object sender,EventArgs)
{
MySecondForm form = new MySecondForm();
form.DataContext = theDataContextIWantToSet;
form.Show();
}

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

.Net 2.0 Winform Label Tool Tip

This has got to be something I just missed, but how do I add a tool tip to a label?
I saw something on the web about handling the mouse hover event, but how would I even handle it in code?
Add in your form the TooTip from the ToolBox than click once in your label and you'll see ToolTip in the property box.
Don't you just drop a ToolTip on a form, and then select the label and set the "Tooltip on Tooltip1" property? At least I remember doing it that way, or something very close to it.
Programmatically from MSDN:
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip(this.textBox1, "Hello");
Drop the TOOLTIP control onto your form. At that point, a ToolTip attribute appears in the Label's properties in the Designer (and is accessible in the code)
button = new Button();
button.Content = "Hover over me.";
tt = new ToolTip();
tt.Content = "Created with C#";
button.ToolTip = tt;
cv2.Children.Add(button);
from http://msdn.microsoft.com/en-us/library/ms754034.aspx

Resources