How do I handle right click on RichTextBox? - c#

How do I handle right click in RichTextBox in C#?
For example:
When I right-click mouse over a RichTextBox the program will display a MessageBox.

You have to catch Mouse_Down event, not Mouse_Click as follows:
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("you got it!");
}
}

Subscribe to the MouseClick event and check the MouseEventArgs.Button property to see which button was clicked.
What have you tried for yourself?

click RichTextBox in design mode .Right click and select properties and Then navigate to MouseDown Event.Double click that event and you will be directed to Code behind page and there you can write your code.

Related

Onclick event for textbox in csharp form application

I'm creating form application on c# . I have dragged a textbox with some text in it.
private void textBox1_Click(object sender, EventArgs e)
{
}
Now what is the event for the onlick on textBox1 ?
I need to add this on that function textBox1.Clear();
P.S I searched everywhere. But all i can find is jquery and javascripts... No c#.
EDIT
I tried onfocus like below..but its not working
private void textBox1_OnFocus(object sender, EventArgs e)
{
MessageBox.Show("dsd");
}
If you want to do something when the control is clicked the handle the Click event, not the TextChanged event. Presumably you just double-clicked the control in the designer. That will only handle the default event. To handle other events, open the Properties window, click the Events button at the top and then double-click the appropriate event.
That said, is Click really appropriate? What if the user enters the TextBox using the Tab key? If what you actually want to do is act when the control gets focus then you should handle the Enter event.
You can handle OnFocus/GotGocus event in the TextBox, and clear the text in the textbox.
Hope this helps.

Best way to hide a popup menu?

I have a control that creates a popup menu on right click, and I need the best way to close it on left click. Besides handling the mouseDown event of everycontrol in my application, what is the best way to close the popup menu?
Like a global mouse click event....
private void lbKeywords_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
listBoxMenu.ShowPopup(Cursor.Position);
}
}
As an answer: try asking DevExpress or looking at their support pages - i.e. I found this after a quick search: devexpress.com/Support/Center/p/CQ14210.aspx

Auto activate an item on select in a ListView

Is it possible that in a ListView whenever an Item has been selected (e.g mouse 1 left click or Key down or key up, left right) that item gets activated (Like when you hit enter or double click!). What event or properties of ListView will do this, if any?
UPDATE
I found out it will work if ListView.Activation is set to OneClick but thi is only for the mouse, I want the same with keyboard arrows too.
You can do that by listening the the ItemSelectionChanged event and simply calling the code that runs whenever an item is double-clicked.
Alternatively you can call ItemActivate event that fires when an item is double-clicked using this bit of code, although I'd recommend the first method:
private void ListView1_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
{
ListView1_ItemActivate(sender, e);
}
You, have mouseclick and mousedoubleclick events in the listview control for this purpose.
Implement the following events with whatever you want your listview to do.
KeyDown , KeyUp, KeyPress, MouseDown, MouseUp, MousePress, MouseHover, MouseEnter, MouseClick, MouseDoubleClick
those are only a few select examples.
Lets say your listview is named listView1
to Subscribe to one of the events, do this
private void Form1_Load(object sender, EventArgs e)
{
listView1.KeyDown += new KeyEventHandler(listView1_KeyDown);
}
void listView1_KeyDown(object sender, KeyEventArgs e)
{
throw new NotImplementedException();
}
Enter your content at the throw statement

Mouse Click event

I have a picture on background in a picture box and i have another picturebox small one above the first.now second picbox is not visible its hidden i want that if user clicks and holds mouse button small picturebox displays and as soon he removes hold i.e releases click picturebox vanishes.
now i thought of doing in click event i should make small one visiable but how to check if click is pressed not released?
sorry if i messed up with question
Use the mouse down event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
}
there you can set the focus on whatever you want
and when you release:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
}
Why not use the MouseDown event in that case?

How can I give focus to a textBox after a Tab has been selected?

I'd like give focus to a textBox after a Tab has been selected but no matter what I try it doesn't work. I've looked at similar questions here but they don't get me the results I need. Here is what Ive tried.
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
and
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
Can someone please tell me what I'm doing wrong?
Thanks
First thing the Click event of the TabPage control fires when the user clicks inside the TabPage not on the header so your SelectedIndexChanged event is the one you want to use.
I just tested code very similiar to yours:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage2)
{
textBox4.Focus();
}
}
And it worked fine.
Is the password textbox not enabled or something like that?
If you try to call Focus() on a different control does that also not work?
If you set a breakpoint inside the SelectedIndexChanged code does it get hit?
Update: Interesting. If the breakpoint isn't getting hit (before the if) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.
Update: The easier way to attach an event handler to a control on your form:
1: Select the Control to want to attach an event handler to and then click the Events icon (lightning bolt) in the Properties window.
alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png
2: Find the event you want to attach to and double click to the right.
alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png
3: A code stub will be automatically generated for you and the event will be attached in the designer.
alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png
If you look at the properties window again you'll now see the name of the method that was generated.

Categories

Resources