(I am newbie and this is probably a duplicate question.)
To see which control is clicked on form, I have a method like this;
public void IdentifyControl(object sender, System.Windows.Input.MouseEventArgs e)
{
Control ctrl = sender as Control;
if (ctrl != null)
SelectedControl = ctrl;
this.Cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
MessageBox.Show(""+ctrl.GetChildAtPoint(System.Windows.Forms.Cursor.Position).ToString());
}
and trying to call it from mouse click.
In my first attempt, I added this function to main forms MouseClick event but it only worked for form itself but controls in form. Then I tried to create a general click event and use by Mouse class.
The main point I stuck is that I couldn't create the suitable parameters Mouse.AddMouseUpHandler(DependencyObject element,
MouseButtonEventHandler handler)
Its probably because of I don't know event handling but maybe I am all in a wrong way.
You can create a mouse click event just double clicking on any buttons, labels, etc, and the Visual studio will create a code for you, like this:
private void YOUROBJECT_Click(object sender, EventArgs e)
{
}
Just put the method you want into this click event.
Hope this can help you!
Related
I just started working on C# with Visual Studio & Windows Forms Applications. I was trying to create a Calculator and I was wondering if I could change the cursor type on a button which is disabled, I can't figure out how to do it, please help me thank you!
Edit: here's the code I tried to do, it only works if the button's enabled...
private void txt_current_operation_MouseHover(object sender,EventArgs e) {
txt_current_operation.Cursor=Cursors.Hand;
}
Hack Answer
Assuming the Button is contained by the Form, you can handle the MouseMove event of the Form and change the cursor from there:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Rectangle rc = txt_current_operation.RectangleToScreen(txt_current_operation.ClientRectangle);
this.Cursor = rc.Contains(Cursor.Position) ? Cursors.Hand : Cursors.Default;
}
If the Button was contained by a Panel, or some other container besides the Form, then you'd change to the MouseMove event of that container instead.
Demonstration:
I have a WindowForm and some controls on it.
My point is that when I click button "?" on top-right of the datagridview, it will show a picture box and when I click outside the pictureBox, it must invisible.
My MainForm
MyPictureBox
I have searched some topics on this site, but some dont work, some work partly. Like
this.
I also tried:
void pictureBox1_LostFocus(object sender, EventArgs e)
{
if (pictureBox1.Visible)
pictureBox1.Visible = false;
}
But when I click on button2, button3, ... The pictureBox wasn't invisible.
Any solution will be highly appreciated.
I think your pictureBox1 isn't losing focus, cause it never actually GOT focused. Set it to be focused after making it visible.
Oh, I have encountered this before...
I was making a Label that you could double click and it would allow you to edit the Label.Text, like a TextBox. However, I was having problems hooking into the events to know when the user had clicked off the Control and wished to stop editing. I tried Control.LostFocus, and Control.Leave, but nothing. I even got frustrated/desperate and tried some silly ones like Control.Invalidated.
What I ended up having to do was subscribe to the Click event of the Form/Container/Control behind it.
However, putting the responsibility of wiring up this event into the Form that wants to use it is poor design. What you can do, however is to make the constructor to Control class require a reference to the owner/parent/container as a parameter. That way, the requirements are not hidden, they must be satisfied before you can get a object instance, and the control can wired up to the Form.Click within itself, where that logic belongs.
private Form owner;
public EditLabel(Form Owner)
{
this.owner = Owner;
owner.Click += EndEditing;
}
Add this method in designer.cs:
pictureBoxEvent this.MouseLeave += new EventHandler(pictureBox_MouseLeave);
Add this code in cs file:
private void pictureBox_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
I currently have a form with about 13 buttons on it.
I want to be able to perform a function when one of those buttons are clicked. But I am trying to keep from having 13 different button click events.
Is there some way for me to be able to determine when any button click event is fired, and be able to tell which button fired it?
Thanks!
Have one function that handles all of the click events and use the properties of the 'sender' object to identify the specific button.
You can have different button click handlers, and name them according to button actions, also you can have single event handler for all of them, in this case parameter sender can be cast to button and for example by it's name, finding related button.
But I offer if you have similar behavior on group of buttons map them to a single function, but if actions are different using different method is better, but in all one form with 13 button is not good, you can change them to menu, Tab, ...
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
(ctl as Button).Click += MyButtonHandler;
}
protected void MyButtonHandler(object sender, EventArgs e)
{
Button clickedButton = sender as Button;
//...
}
The first parameter to the callback function is the button that was pressed.
private void OnButtonClicked(object sender, EventArgs e){
Button oButton = sender as Button;
if (oButton != null){
// your logic goes here !
}
}
oButton variables is your current button. (important to check if oButton != null when using as operator)
on the click event you get a reference to the Sender - this is the clicked button so inside that you could test for the Content or Tag and act based on the value:
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as Button).Tag.ToString());
}
I think that in this situation using delgates would be proper. Check these links out for further information on implementation.
The C# Station Tutorial: Introduction to Delegates and Events
Delegates and Events in C# / .NET
Every proper eventhandler has an Object sender parameter. Give all 13 buttons the same handler by typing the same name in the Properties window OnClick event, for instance: OnAnyButtonClicked. The created function will be:
private void OnAnyButtonClicked(object sender, ...)
{
// sender is the button that was clicked,
// find out which button is clicked
// call the corresponding function
}
Tip:
To find out which button is pressed you could use Object.ReferenceEquals
A faster way to avoid if ... then ... else if ... then ... else if...
is using the Tag property of each Button.
Give eacht Button.Tag an enum value corresponding to the action that has to be performed and use a switch statement to find out what has to be done.
You could also assing a delegate to the button.Tag, but that is almost the same as making a different onClick event handler for every button.
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
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.