Prevent events from firing multiple times from single action - c#

How can I prevent the firing of multiple events of the same kind triggered by a single action?
For example, I have a ListView containing some items. When I select or deselect all items, the SelectedIndexChanged event is fired once for each item. Rather, I would like to receive a single event indication the user's action (selection/deselection of items), regardless of the number of items.
Is there any way to achieve this?

You can't change the ListView code, and subclassing it doesn't provide many options.
I would suggest that you simply add a small delay (200ms or similar) to your code - i.e. you only do the calculation a little while after the last update. Something like:
using System;
using System.Windows.Forms;
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
ListView list;
TextBox txt;
Timer tmr = new Timer();
tmr.Interval = 200;
Form form = new Form {
Controls = {
(txt = new TextBox { Dock = DockStyle.Fill, Multiline = true}),
(list = new ListView { Dock = DockStyle.Right, View = View.List,
Items = { "abc", "def" , "ghi", "jkl", "mno" , "pqr"}})
}
};
list.SelectedIndexChanged += delegate {
tmr.Stop();
tmr.Start();
};
tmr.Tick += delegate {
tmr.Stop();
txt.Text += "do work on " + list.SelectedItems.Count + " items"
+ Environment.NewLine;
};
Application.Run(form);
}
}

Only by by coming at the problem from a slightly different direction. E.g. subscribe loss of focus.
In the end, the application or runtime cannot raise an event on "all selection changes done" without actually using something else because there is no way for the application to predict whether the user will perform another click on the control while it retains focus.
Even using focus, the user could switch back to that control.

If your ListView is in virtual mode, you could use the
VirtualItemsSelectionRangeChanged event. This event will be fired only once for the user's action (selection/deseclection).

Related

When tabbing out of control, enable disabled control that follows it, so that focus will go to that control, and not skip it?

Before leaving a textbox due to the user hitting the Tab key, I want to enable a control that follows it tabstop-wise, so that it becomes the focused control.
Instead it seems Winforms has already decided where focus will go so it skips the control.
I have tried using the Validating and Validated events to enable the control that follows, but it seems this too is too late, even though I know that if validation indicates failure, focus will not leave the control so clearly tab behavior can be affected at validation time.
So, example specific scenario (code follows):
3 controls, textbox + checkbox + textbox
Checkbox is disabled, focus is currently in the first textbox
As part of leaving the first textbox, I want to validate the contents of it, and if OK, I would like to enable the checkbox
As such, if the user is in the first textbox, fills it with valid data, and hits Tab, focus should now be on the checkbox, and not on the second textbox.
Observed behavior is that focus moves to the second textbox, skipping the checkbox, and the checkbox is enabled, possibly not in that order (ie. I'm describing the observed end results, not necessarily the order things happen in).
Is it possible to get what I want?
Here is a LINQPad program that demonstrates the problem
void Main()
{
var fm = new Form();
var e1 = new TextBox();
e1.Location = new Point(8, 8);
e1.CausesValidation = true;
e1.TabIndex = 0;
e1.TabStop = true;
var chk = new CheckBox();
chk.Text = "Checkbox";
chk.Location = new Point(8, e1.Bottom + 8);
chk.Enabled = false;
chk.TabIndex = 1;
chk.TabStop = true;
var e2 = new TextBox();
e2.Location = new Point(8, chk.Bottom + 8);
e2.TabIndex = 2;
e2.TabStop = true;
fm.Controls.Add(e1);
fm.Controls.Add(chk);
fm.Controls.Add(e2);
e1.Validating += (s, e) =>
{
Debug.WriteLine("e1 validating");
chk.Enabled = true;
};
e1.Validated += (s, e) =>
{
Debug.WriteLine("e1 validated");
chk.Enabled = true;
};
chk.Enter += (s, e) => Debug.WriteLine("chk entered");
e2.Enter += (s, e) => Debug.WriteLine("e2 entered");
fm.ShowDialog();
}
You're better off validating the input in the TextChanged event, such as:
e1.TextChanged += (s, e) =>
{
chk.Enabled = (e1.Text.Length > 2);
};
The MSDN docs for the Control.Validating event include this warning:
Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http://msdn.microsoft.com/library.
I don't remember the specific order that Windows uses processing messages, but I suspect that the message enabling the chk checkbox gets sent after Windows processes the tab keypress. Doing your validation in TextChanged events means you don't have to worry about this weird race condition.
Sort of a hack. but works..
e1.Leave += (s, e) =>
{
Debug.WriteLine("e1 Leave");
chk.Enabled = true;
fm.SelectNextControl( e1,true,true,true,true);
};

How to continuosly update the textbox in C#

I have the following piece of code:
class NotepadCloneNoMenu : Form
{
protected TextBox txtbox;
public NotepadCloneNoMenu(string a)
{
Text = "Notepad Clone No Menu";
txtbox = new TextBox();
txtbox.Parent = this;
txtbox.Dock = DockStyle.Fill;
txtbox.BorderStyle = BorderStyle.None;
txtbox.Multiline = true;
txtbox.ScrollBars = ScrollBars.Both;
txtbox.AcceptsTab = true;
txtbox.AppendText(a);
txtbox.AppendText("\n");
}
}
class program1
{
public static void Main()
{
string result = "abc";
while(true)
{
Application.Run(new NotepadCloneNoMenu(result));
}
}
}
I want to continuously appending the string result to the textbox so it looks like this:
abc
abc
abc
so on and so forth. However, every time I called this:
Application.Run(new NotepadCloneNoMenu(result));
It will reset the textbox. Is there anyway I can update the textbox continuously? I am fairly new to C# so this is quite confusing to me.
thanks,
Phuc Pham
First of all, you're continuously closing and opening an application. That's why it resets. If you want to run an infinite loop, you probably want to run it inside your application proper.
In your application code, use some event (maybe a timer would suit you) to append text to the textBox. Like this:
public someEventOnTheForm (object sender, EventArgs e)
{
txtBox.Text += "Notepad Clone to Menu";
}
There's two more things to take into account: first, if you don't have a stoping condition, this will just keep filling memory until you run out of it.
Second, windows forms run on only one thread by default. You'll be using that thread to update the textbox, so while it's appending text, the form itself will be unusable. It'll probably blank out during the event if it starts taking long. You'll need a second thread to handle the event if you want your form to be usable.

Button Screen Change in Android Mono using delegates

The following code is from the C# portion of my Android Mono application. It is going to eventually be the GUI for a multimeter simulator, but right now just displays text. It is rather straight forward:
-Click one of the buttons to go to that meter (voltmeter, ammeter, ohmmeter)
-Click the "re-scan" button and a TextView tells you how many times you clicked that button.
-Click one of the other meter buttons or the home button to switch views
That much is working flawlessly. Unfortunately, once I switch views, the buttons cease to work. Below is the code for the Ohm button and the Amp button. The Ohm button is the 'complete' one that brings up views of all of the other screens. For testing purposes, I was going to the amp screen but when I go there, its re-scan button does nothing. None of the buttons do anything.
I am fairly certain that the issue is my use of the delegate commands, but none of my research has led me in any way towards a solution.
I can provide more of the main code and the XML code if needed.
ampButton.Click += delegate
{
SetContentView(Resource.Layout.AmpScreen);
Button ampButtonData = FindViewById<Button>(Resource.Id.CurrentButtonamp);
TextView ampData = FindViewById<TextView>(Resource.Id.ampdata);
ampButtonData.Click += delegate
{
ampData.Text = string.Format("{0} clicks!", count2++);
};
Button amp2volt = FindViewById<Button>(Resource.Id.Amp2VoltButton);
Button amp2ohm = FindViewById<Button>(Resource.Id.Amp2OhmButton);
Button amp2home = FindViewById<Button>(Resource.Id.Amp2HomeButton);
};
ohmButton.Click += delegate
{
SetContentView(Resource.Layout.OhmScreen);
Button ohmButtonData = FindViewById<Button>(Resource.Id.CurrentButtonohm);
TextView ohmData = FindViewById<TextView>(Resource.Id.ohmdata);
ohmButtonData.Click += delegate
{
ohmData.Text = string.Format("{0} clicks!", count3++);
};
Button ohm2amp = FindViewById<Button>(Resource.Id.Ohm2AmpButton);
Button ohm2volt = FindViewById<Button>(Resource.Id.Ohm2VoltButton);
Button ohm2home = FindViewById<Button>(Resource.Id.Ohm2HomeButton);
ohm2amp.Click += delegate
{
SetContentView(Resource.Layout.AmpScreen);
};
ohm2volt.Click += delegate
{
SetContentView(Resource.Layout.VoltScreen);
};
ohm2home.Click += delegate
{
SetContentView(Resource.Layout.Main);
};
};
I think your problem is that you are replacing the entire view each time - so the button instances are changing.
What happens inside SetContentView is that the InflatorService gets asked to create a brand new set of UI objects based on the passed in XML, the existing UI is wiped clean and then those new UI objects are put in their place.
It doesn't matter if the new UI objects happen to have the same resource identifiers as the old objects - they are still separate instances.
If you want to continue using your current approach, then you need to rewire all your events after each SetContentView - e.g.
ohm2amp.Click += delegate
{
SetContentView(Resource.Layout.AmpScreen);
RewireEvents();
};
with
private void RewireEvents()
{
var ohm2home = FindViewById<Button>(Resource.Id.ohm2home);
ohm2home.Click += { /* todo */ };
// etc
}
alternatively, maybe consider a different UI:
e.g. you could change the Visibility on different child layouts rather than calling SetContentView to replace everything
e.g. or you could use multiple activities (or tabs) instead of a single activity
Hope that helps

AlertControl doesn't show

I have a strange problem with devexpress AlertControl. I create an alertu using this code
AlertInfo alertInfo = new AlertInfo(caption, text);
AlertControl control = new AlertControl();
control.FormLocation = AlertFormLocation.BottomRight;
control.Show(null,alertInfo);
this code is placed in backgroundWorker_DoWork function and it is supposed to display alerts from time to time. The problem is that alerts are not shown. I can see that show method is invoked however alerts are not shown.
Acording to documentation is I pass null as a parametr of Show function , notification should be shown on main monitor.
What can I do to make it work ?
Considering you're using a worker, I guess it's a thread problem. Try wrapping your code inside an Action object:
Action action = () =>
{
AlertControl control = new AlertControl();
control.FormLocation = AlertFormLocation.BottomRight;
control.Show(this, alertInfo); // "this" being a Form
};
this.Invoke(action);
I use a similar code inside a form with good results and once did a similar code using an AlertControl too.
Your AlertControl need a Parent Control.
AlertControl control = new AlertControl();
control.FormLocation = AlertFormLocation.BottomRight;
control.Show(MyForm,alertInfo); //replace null with a Form/Control instance
You call the Show method with a null paramater - where you should have use an instance of a Form/Control
Don't know anything about the devexpress controls, but maybe you have to show the alert from the main thread via invoke methode?
using DevExpress.XtraBars.Alerter;
// Create a regular custom button.
AlertButton btn1 = new AlertButton(Image.FromFile(#"c:\folder-16x16.png"));
btn1.Hint = "Open file";
btn1.Name = "buttonOpen";
// Create a check custom button.
AlertButton btn2 = new AlertButton(Image.FromFile(#"c:\clock-16x16.png"));
btn2.Style = AlertButtonStyle.CheckButton;
btn2.Down = true;
btn2.Hint = "Alert On";
btn2.Name = "buttonAlert";
// Add buttons to the AlertControl and subscribe to the events to process button clicks
alertControl1.Buttons.Add(btn1);
alertControl1.Buttons.Add(btn2);
alertControl1.ButtonClick += new AlertButtonClickEventHandler(alertControl1_ButtonClick);
alertControl1.ButtonDownChanged +=
new AlertButtonDownChangedEventHandler(alertControl1_ButtonDownChanged);
// Show a sample alert window.
AlertInfo info = new AlertInfo("New Window", "Text");
alertControl1.Show(this, info);
void alertControl1_ButtonDownChanged(object sender,
AlertButtonDownChangedEventArgs e) {
if (e.ButtonName == "buttonOpen") {
//...
}
}
void alertControl1_ButtonClick(object sender, AlertButtonClickEventArgs e) {
if (e.ButtonName == "buttonAlert") {
//...
}
}
ref:https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraBarsAlerterAlertControltopic

Why is my button's 'Validating' event handler never called?

In the following mini-app, I am wondering why the BtnOk_Validating event handler is never called. I expected that clicking the Ok button would call the event handler.
The real dialog has many more controls, each that have a validating event handler. My plan was to use the Ok button validating event handler to call each of the other event handlers before allowing the dialog to close.
If it's not obvious, I'm quite the novice when it comes to Forms development.
using System.ComponentModel;
using System.Windows.Forms;
namespace ConsoleApp
{
class Program
{
static void Main( string[] args )
{
Dialog dialog = new Dialog();
dialog.ShowDialog();
}
}
public class Dialog : Form
{
Button m_BtnOk;
Button m_BtnCancel;
public Dialog()
{
m_BtnOk = new System.Windows.Forms.Button();
m_BtnCancel = new System.Windows.Forms.Button();
m_BtnOk.CausesValidation = true;
m_BtnOk.DialogResult = DialogResult.OK;
m_BtnOk.Text = "Ok";
m_BtnOk.Location = new System.Drawing.Point( 0, 0 );
m_BtnOk.Size = new System.Drawing.Size( 70, 23 );
m_BtnOk.Validating += new CancelEventHandler( BtnOk_Validating );
m_BtnCancel.CausesValidation = false;
m_BtnCancel.DialogResult = DialogResult.Cancel;
m_BtnCancel.Text = "Cancel";
m_BtnCancel.Location = new System.Drawing.Point( 0, 30 );
m_BtnCancel.Size = new System.Drawing.Size( 70, 23 );
Controls.Add( this.m_BtnOk );
Controls.Add( this.m_BtnCancel );
}
private void BtnOk_Validating( object sender, CancelEventArgs e )
{
System.Diagnostics.Debug.Assert( false ); // we never get here
}
}
}
Edit: Please see my follow-up question for a more complete example that works (well mostly).
Its because the button will never loose focus with it being the only control. If you add a TextBox or something that can take the focus of the button, then you will see it fire.
From MSDN
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Update: Like Hans mentions, you'll need to extract the validating you do in each of the Validating events for all the other controls into separate functions. Then you can create a ValidateAll function to check all values. If the function returns false, then you dont close the Form. If it returns true, you call this.Close(). So it might look like this:
// pseudo code
textbox1.Validating += ValidateTx1();
textbox2.Validating += ValidateTx2();
btnOk.Click += OkBtnClicked();
private void OkBtnClicked(...)
{
if(ValidateAll())
{
this.Close();
}
}
private bool ValidateTx1(...)
{
DoTx1Validation();
}
private bool ValidateTx2(...)
{
DoTx2Validation();
}
private bool ValidateAll()
{
bool is_valid = DoTx1Validation();
return (is_valid && DoTx2Validation());
}

Categories

Resources