I handled an event "LostMouseCapture" on my wpf text box . Now I need to implement functionality that if this event got fire because of click of one specific button then I want to do something and if this event got fire because of some thing else then i want to do some other thing.
I am blank how to achieve this functionality
private void txtEcode_LostMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
{
//if the event is fired because of click of "btnEcode" then do this
{
MCondsViewModel.McondsNo = MCondsViewModel.EcodeValueOnMouseFocus;
}
else
{
//do this
}
}
Related
So, I'm trying to build kind of a sampler. Just for fun. I'm kinda at a loss here;
I've created a grid of 4x4 in wpf and filled them with buttons. Each button has a corresponding mediaplayer (so they would not cut off the other audio when triggered)
I've added a clickevent for each button and a keydown event as wel. Now, the click events all work fine. But the keydown event only works for the last button I clicked on. The rest is unresponsive. Can anyone tell me how to fix this?
a keydown event:
private void Pad_1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.NumPad7)
{
playPad(mediaPlayer1, mp1);
}
}
and my playPad method:
public void playPad(MediaPlayer mediaPlayer, string mp)
{
if (mp != null)
{
mediaPlayer.Open(new Uri(mp));
mediaPlayer.Play();
}
}
Thanks in advance!
So I would like to know what is wrong with the following code, especially from a theoretical point of view.
I have a user control in which I've added a text box.
When I click in the text box I would like the Mouse clicked event raised in the user control.
To my mind, the solution should be:
Create an event handler for the mouse click event in the text box.
in this event handler, raise the mouse click event for the user control.
so this is what i have:
private void txtLog_MouseClick(object sender, MouseEventArgs e)
{
this.OnMouseClick(e);
}
i have tried it and it doesn't work, why is this?
P.S. I would really like to know why this is wrong! A correct solution is great, but I'm really trying to understand where I'm going wrong here. Thank :-)
Well, you could just click on your textbox in design mode and in the property window in events tab add the click event. or if you want to do it in runtime you can do it like this:
textbox.Click += Txt_Click;
private static void Txt_Click(object sender, EventArgs e)
{
// do your thing
}
or even shorter:
textbox.Click += (s,e) =>
{
//do your thing
};
you should do these three steps
declare an MouseClick delegation method for textbox
assign method to textbox
add this delegation to the this (form) OnMouseClick event [on user control constructor]
Step1:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
}
Step2:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
Step3:
public myUserControl()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(textBox1_MouseClick);
}
I want a button to function like this:
if button is down(mouse button is hold down)
{
bool trySmthing = true;
}
if button is up
{
bool trySmthing = false;
}
I tried some stuff with KeyUp and KeyDown events but they don't give the right result.
Apparently the focus must be in your application; a tutorial on handling the mouse events via the .NET framework in C# can be found here. The MouseDown event, as documented here and the MouseUp event, as documented here, are of particular interest..
This should help: Micosoft Library for C# Mouse-events
void MouseUpHandler(Object sender, RoutedEventArgs args)
{
// This method is called whenever the PreviewMouseUp event fires.
}
void MouseDownHandler(Object sender, RoutedEventArgs args)
{
// This method is called whenever the PreviewMouseDown event fires.
}
i have a problem with user control.
i create it dynamically on my aspx page after clicking on a button:
protected void btnAddRules_Click(object sender, EventArgs e)
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
when i click on a button of my user control, the button event wont fire and the user control will disappear. here is the button event:
protected void btnAdd_Click1(object sender, EventArgs e)
{
WowzaRule rule = GetRuleFromGUI();
RuleList.Add(rule);
//Session["RuleList"] = RuleList;
//List<WowzaRule> test = new List<WowzaRule>();
SaveToXMLFiles(txtdbnum.Text, RuleList);
}
i understand that after pressing the button on mypage the usercontrol is released and if its not created on pag_init or page Load it wont stay, but i need to create it on my button click event and find a way for it not to disapper.
thanks in advance, Daniel
You might have to add an event handler that it can fire the click event and call your delegate
Control.Click += btnAdd_Click1;
Dynamically created controls, once added, have to be on a page on every page load in order to work correctly. What happens in your case:
RuleProperty is added after the button click
Page loads with this control
User clicks on the button within RuleProperty
Control is not added to the control tree during the page load (corresponding code is only in the button click handler, and that button was not clicked)
ASP.NET does not know which control triggered the event, so the event is not processed
To go around this issue you need to add you control on every page loading, for example using some flag stored in ViewState:
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["AddRuleProperty"] != null && (bool)ViewState["AddRuleProperty"])
{
AddRulePropertyControl();
}
}
protected void btnAddRules_Click(object sender, EventArgs e)
{
AddRulePropertyControl();
ViewState["AddRuleProperty"] = true;
}
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
Update.
If you want to remove the control from the page later on in the control's click handler, you need to remove corresponding ViewState key. This is not possible from the control directly, since property Page.ViewState is protected, and also this would have created an unwanted dependency.
What seems as the right way to do this is to subscribe to the very same event from the Page (you might need to make this event visible from the controller) and reset the key in there. Like this:
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
Control.ButtonClick += RuleProperty_ButtonClick;
MyPanel.Controls.Add(Control);
}
private void RuleProperty_ButtonClick()
{
ViewState["AddRuleProperty"] = false;
}
Please note that event name here is not real, this is just a sketch of what can be done.
I have a ComboBox that is a simple drop down style. I wanted to open a new window when the user right clicks on an item in the list, but am having trouble getting it to detect a right click has occurred.
My code:
private void cmbCardList_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && cmbCardList.SelectedIndex != -1)
{
frmViewCard vc = new frmViewCard();
vc.updateCardDisplay(cmbCardList.SelectedItem);
vc.Show();
}
}
If I change e.Button == MouseButtons.Left the whole thing fires off just fine. Any way I can get this working as I intend?
I'm afraid that will not be posible unless you do some serious hacking.
This article will explain.
Quoted for you:
Individual Controls
The following controls do not conform to the standard mouse click event behavior:
Button, CheckBox, ComboBox, and RadioButton controls
Left click: Click, MouseClick
Right click: No click events raised
Left double-click: Click, MouseClick;
Click, MouseClick
Right double-click: No click events
raised
As an epitaph to this question, you can make this work using normal .NET functionality; you just have to go a little deeper into the event call stack. Instead of handling the MouseClick event, handle the MouseDown event. I had to do something similar recently, and I simply overrode the OnMouseDown method instead of attaching a handler. But, a handler should work too. Here's the code:
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && !HandlingRightClick)
{
HandlingRightClick = true;
if (!cmsRightClickMenu.Visible)
cmsRightClickMenu.Show(this, e.Location);
else cmsRightClickMenu.Hide();
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
HandlingRightClick = false;
base.OnMouseUp(e);
}
private bool HandlingRightClick { get; set; }
The HandlingRightClick property is to prevent multiple triggers of the OnMouseDown logic; the UI will send multiple MouseDown messages, which can interfere with hiding the right-click menu. To prevent this, I only perform the logic once on the first MouseDown trigger (the logic's simple enough that I don't care if two invocations happen to race, but you might), then ignore any other MouseDown triggers until a MouseUp occurs. It's not perfect, but this'll do what you need it to.
You can use the Opening event of ContextMenuStrip to handle right click event.
var chk = new CheckBox();
chk.ContextMenuStrip = cmsNone;
private void cmsNone_Opening(object sender, CancelEventArgs e)
{
e.Cancel = true;
var cms = (ContextMenuStrip)sender;
var chk = cms.SourceControl;
//do your stuff
}