What i tried now is in the richTextBox1 mouseup event and also the events for each action:
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
richTextBox1.ContextMenu = contextMenu;
}
}
void CutAction(object sender, EventArgs e)
{
richTextBox1.Cut();
}
void CopyAction(object sender, EventArgs e)
{
Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);
Clipboard.Clear();
}
void PasteAction(object sender, EventArgs e)
{
if (Clipboard.ContainsText(TextDataFormat.Rtf))
{
richTextBox1.SelectedRtf
= Clipboard.GetData(DataFormats.Rtf).ToString();
}
}
There are two problems:
When i mark text in the rcihTextbox and make right click nothing happen only when i make another right click i see the menu Cut Copy Paste. Why it's not showing the menu on the first right click ?
Second problem when i make Copy click on Copy then i go to the chrome browser and try to make paste it's empty the paste is empty like it didn't copy it at all.
I checked now again only Cut is working. If i make Copy it's not copying anything and i can't paste into Chrome address bar at top. Or if i copied from exmaple from chrome something i searched for: hello world then the Paste in the richTextBox is empty.
I want to be able to copy/cut/paste from inside the richTextBox control it self and from other external programs like notepad chrome ie or even other richtextbox control.
The ContextMenu is not showing because you set it after the right click. Try to set it on the MouseDown event. When you click copy, you set the text in the clipboard but you remove it after with the Clipboard.Clear(), so i don't know what you were thinking by writing this line, just remove it.
Related
I want to put a context sensitive menu on the right-click of a DataGridView. I want to have a single menu item derived from the content of the clicked cell and a variable number of items in a sub-menu, retrieved from a database.
I do this by building the ToolStripMenuItem in the ContextMenuStrip Opening event. And it works - almost...
If I leave the sub-menu undisplayed so the user has to click the single item in the toplevel menu, everything is fine but if I call the ShowDropDown method to display the submenu automatocally, the exents don't fire when the items are clicked on.
Here's the simplest code I can produce to recreate the problem - I've pulled out all the references to the DataGridView and database so my "dynamic" menu is decidedly static ;-)
If you put this is a Form definition, right-click anywhere on the form and you'll see the working but not desired behaviour - click on a sub-menui tem and see a popup. Tick the checkbox and right-click again and you'll see the submenu flies out automatically - but clicking items won't fire the aliasClick handler.
Any thoughts? In this particular application, I can code a perfectly servicable workaround which avoids using ShowDropDown - but I'd like to know what I'm doing wrong in case I need to use it in future.
public partial class Form1 : Form
{
private ContextMenuStrip cms;
private CheckBox chkAuto;
public Form1()
{
InitializeComponent();
chkAuto = new CheckBox();
Controls.Add(chkAuto);
cms = new ContextMenuStrip();
cms.Opening += contextMenuStrip1_Opening;
this.MouseClick += Form1_MouseClick;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
cms.Show(Cursor.Position);
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
cms.Items.Clear();
ToolStripMenuItem tsmi = new ToolStripMenuItem("Title from datagridviewcell");
tsmi.DropDownItems.Add(new ToolStripMenuItem("First item from database lookup", null, aliasClick));
tsmi.DropDownItems.Add(new ToolStripMenuItem("Second item from database lookup", null, aliasClick));
tsmi.DropDownItems.Add(new ToolStripMenuItem("Last item from database lookup", null, aliasClick));
cms.Items.Add(tsmi);
if (chkAuto.Checked)
tsmi.ShowDropDown();
e.Cancel = false;
}
private void aliasClick(object sender, EventArgs e)
{
ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
MessageBox.Show(clickedItem.Text);
}
}
I'm not entirely sure how to go about proving this, but I suspect that your call to tsmi.ShowDropDown() is somehow causing the coordinates to not be captured properly by the click handler. Replace it with cms.Show(MousePosition) and it works.
Perhaps some useful info gleaned from a look at the coordinates...
var mi = new ToolStripMenuItem("First item from database lookup", null, aliasClick);
tsmi.DropDownItems.Add(mi);
var mi2 = new ToolStripMenuItem("Second item from database lookup", null, aliasClick);
tsmi.DropDownItems.Add(mi2);
var mi3 = new ToolStripMenuItem("Last item from database lookup", null, aliasClick);
tsmi.DropDownItems.Add(mi3);
cms.Items.Add(tsmi);
if (chkAuto.Checked)
tsmi.ShowDropDown();
//cms.Show(MousePosition);
Debug.WriteLine(mi.Bounds.ToString());
I have made 2 user controls. First one contains a textbox and a button. In the second one there is a panel and a repeater control is used. Now when I am writing in the textbox 2nd control will open as a popup and and after focusing to textbox I am unable to write. I have searched a lot about this but nothing works.
CustomPopup customPopup;
Popup popup;
popup = new Popup(customPopup = new CustomPopup());
private void txtSearch_TextChanged(object sender, EventArgs e)
{
popup.Width = Width;
popup.Show(this);
popup.AutoClose = false;
}
In this way I am opening the popup from the textbox text changed event.
You can use this textbox event.
private void textBox1_Enter(object sender, EventArgs e)
{
}
this event call whenever textbox get focus
I asked a question similar to this a few days ago here. The answer works fine, with the exception of performing the same operation at the start up of the window. This means that I need to have the text in a textBox highlighted every time the window is opened.
Currently I am setting the focus to the textBox at startup with no problem in the constructor. With that being said, I am guessing that the correct area to perform this operation is in the constructor. This is what I am trying currently with no luck:
public AddDestination()
{
InitializeComponent();
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
How can I make it so that the text inside my textBox is highlighted whenever the window opens?
Instead of constructor, move it to AddDestination_Load event.
public AddDestination()
{
InitializeComponent();
}
private void AddDestination_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
textBox1.Focus();
//Highlights text **DOES NOT WORK
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
You can write this segment of code in Load EventHandler
//code
InitializeComponent();
//code
private void Form_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
To do so go to Designer, click on the window, go to Properties, click Events button, search for Load event then double-click on it.
I have encountered some problems in the bookmark section for my wpf web-browser. I want to be able to delete already existing buttons but I can't seem to figure out how to detect the content of the button I right click on. (To get my ContextMenu to show).
My visual progress so far: http://puu.sh/6Dxat.png
Adding a context menu to the buttons:
public void button_MouseRightButtonDown(object sender, MouseButtonEventArgs e)//add a context menu to buttons
{
Button button = sender as Button;
menu = new ContextMenu();
menu.Items.Add(new MenuItem() { Header = "Delete" });
button.ContextMenu = menu;
menu.Closed += button_DeleteButtonClicked;//find the right event
}
(I know that the event is wrong, but that is not important right now.)
And the event:
private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)//delete bookmark
{
//This is where I need help. I want the content (which is the URL) of the button
//right clicked onto, for example, show up in a messagebox. How to do?
}
Since you have hooked Close event of context menu here, so sender will be ContextMenu here and you can get button by using PlacementTarget property of ContextMenu.
private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)
{
Button button = ((ContextMenu)sender).PlacementTarget as Button;
}
I want to show Image on MouseEnter event of button control(I have 6 buttons) ,I can use below code for each button
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
but I Don't want to write it for each buttons enter event ,and hence trying to make it only on method so can use for each button something like this ,but how do I will select different image for different button via this method than ?
void button_MouseLeave(object sender, EventArgs e)
{
var btn = (Button)sender;
this.btn.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.particular image for particular button));
}
You register the same event for all buttons.
For example:
btn1.MouseEnter += genericButton_event;
btn2.MouseEnter += genericButton_event;
You add the image to resources for example using the same name as the button so you can use the btn.Name property. (Something like: btn1.png and btn2.png), and you assign the resource using reflection with the string property "name":
private void genericButton_event(object sender, EventArgs e)
{
var btn = (Button)sender;
btn.BackgroundImage = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources" + btn.Name +".png"));
}
You get the bitmap from resources using strings, so you can get the desired background image depending on button name.
You could use the Tag attribute for that. It takes an object - so you can put there whatever you want.
Or you could chose which image to display by naming them after the button and searching for the correctly named image.
Button btn = new Button();
btn.Tag = <YourImage>; // Here you define which image to show
btn.MouseLeave += btn_MouseLeave;
void btn_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
b.BackGroundImage = (System.Drawing.Image)b.Tag;
}
Of course you'd have to check if the Tag is null.