|
Hi,
if you allow single selection, have a look at SelectedItem or SelectedIndex; if you allow multiple selection, look at SelectedItems or SelectedIndices (and their Count).
You probably need to execute only a small part of your event handler when no item is selected; and you sure need to execute some part only when something got selected.
You probably cannot just ignore the event when nothing is selected, as unselecting may be a legitimate user action requiring some response.
|
|
|
|
|
I looked into this some more...
Apparently the information I'd read about the event firing twice was false or out of date I did a simplified test, and the event only fires once, with the new selected value(s)/index/indices. All the extra events are actually being triggered in my program (for reasons that make sense depending on context).
The event handler had logic in its header to prevent it from firing in certain scenarios where I knew the program would be making too many calls that would change the index (in particular, refreshing the databinding). I realized the current selection was already being stored in a static var, and just added to the short-circuit logic a test: selectedvalue = staticVar --> quick_exit
Works like a charm; the condition is true after the initial pass sets the static var.
Thanks for your help; looking at the selectedindex vals helped me figure out what was happening.
|
|
|
|
|
You're welcome.
Yes, multiple or spurious events can be a burden and don't get documented; some Controls will even fire a Changed event when assigning the current value again, so one always has to program defensively.
|
|
|
|
|
I'm trying to make a logging class and I place SteamWriter.Close() in the destructor, however it seems that Dispose() has already been called on the StreamWriter by the time the destructor is called. Since I cant' call Close(), the text is never flushed to the file. How can I fix this?
Turning auto-flush on helps but i still think i should be closing the stream properly rather than letting it happen automatically at program exit.
|
|
|
|
|
Move your stream reader close call into your dispose method. Call the dispose method from your destructor like:
private bool mIsDiposed = false;
~MyClass(){
Dispose(false);
}
public void Dispose(){
Dispose(true);
}
public void Dispose(bool disposing){
if(!mIsDisposed){
if(disposing){
GC.SuppressFinalize(this);
}
mIsDisposed = true;
}
}
|
|
|
|
|
could it be you're already calling StreamWriter.Close or Dispose somewhere?
maybe you should show us some of the relevant code.
|
|
|
|
|
Are you using a 'using' block by any chance? If so, it automatically calls dispose at the end of the block so manual calls to dispose will cause issues.
Another possibility is that some other object (a stream reader or similar) may be closing/disposing the underlying stream.
Just a couple of ideas as without code it's very difficult to guess what the problem is.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
From the code below, I am getting three different errors. The first has to do with the Listbox UpdateBegin/Endupdate. The next error has to do with the 'Foreach' statement and 'in'.
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare an instance of the open file dialog...
OpenFileDialog dlg = new OpenFileDialog();
//The result of the open file dialog is either true or false.
Nullable<bool>result = dlg.ShowDialog();
//Set properties for the dialog...
dlg.Title = "Select one or more media files";
dlg.Multiselect = true;
dlg.Filter = "Media files (*.mp3 | *.wav | *.wma | *.avi | *.mp4 | *.mpg | *.wmv)";
//If the result of the open file dialog was true then....
if (result == true)
{
//Stop the list box from drawing while items are added.
lstBxList.UpdateBegin(); <<
|
|
|
|
|
Tichaona J wrote: Any ideas...???
yes.
read the documentation for each of the class members you intend to use.
and pay attention to Intellisense when it is available.
|
|
|
|
|
dlg.OpenFiles is a method group
dlg.OpenFiles() is a method that returns a bunch of strings
|
|
|
|
|
Ian Shlasko wrote: a bunch of strings
streams actually, which makes it sound like a rather expensive method.
|
|
|
|
|
Ah, I was just deriving from context... So he's got more bugs buried underneath the bugs he's spotted
|
|
|
|
|
don't worry, the compiler will spot them. And we will be informed, no doubt.
|
|
|
|
|
Perhaps you could wrap your code in a PRE block and paste the exact error messages you are getting and indicate which line they are occurring on? Here's an example of how to use a PRE block:
<pre lang="C#">List<int> x = new List<int> {1, 2, 3};</pre>
Here's what that looks like:
List<int> x = new List<int> {1, 2, 3};
You can edit your original message to make that change.
|
|
|
|
|
|
I finally figured it out, this works as intended:
//Open files into the list box
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
//Declare an instance of the open file dialog...
OpenFileDialog dlg = new OpenFileDialog();
//Set properties for the dialog...
dlg.Title = "Select one or more media files";
dlg.Multiselect = IsEnabled;
dlg.Filter = "Media files(*.mp3;*.wav;*.wma;*.avi;*.mp4;*.mpg;*.wmv)|*.mp3;*.wav;*.wma;*.avi;*.mp4;*.mpg;*.wmv|All files(*.*)|*.*";
//The result of the open file dialog is either true or false (didn't work).
Nullable<bool> result = dlg.ShowDialog();
//If the result of the open file dialog was true then....
if (result == true)
{
string[] files;
files = dlg.FileNames;
foreach (string file in files)
{
lstBxList.Items.Add(file);
}
}
}
|
|
|
|
|
yes, that looks better, except that you are still not using PRE tags to show a code snippet.
|
|
|
|
|
Hi,
I have some queue that i fill with some chars.
The queue is filled all the time ( add char each 10 milliseconds )
When the queue have 20 chars ( or more ) i need to type the chars on the text line.
I try to update the text line from other thread but i get some strange exception ( "external exception e0434352" )
The code:
<br />
public delegate void UpdateTextCallback( Char ch ); <br />
<br />
private void addToQueue( object sender, char ch )<br />
{<br />
lock( locker )<br />
{<br />
IncomingCharQueue.push( ch );<br />
<br />
if( IncomingCharQueue.Count > 20)<br />
{<br />
textBox.Invoke( new UpdateTextCallback( UpdateText ), IncomingCharQueue.Dequeue() );<br />
<br />
}<br />
}<br />
}<br />
<br />
private void UpdateText ( Char ch )<br />
{<br />
textBox.Text = Ch;<br />
}<br />
<br />
|
|
|
|
|
First, make sure to check if invoke is required (using this.InvokeRequired ) before you perform an invoke. Next, try using "this.Invoke" rather than "textBox.Invoke". Not sure if any of that is your problem, but it's worth a try. Also, read this tip/trick for easier ways to invoke.
|
|
|
|
|
A queue is probably the wrong structure. Give or take syntax errors. Wrote this for .NET 2.0 so it is relevant for most flavors.
StringBuilder sb = new StringBuilder();
private void Add(char a){
lock(this){
sb.Append(a);
if(sb.Length >= 20){
UpdateText(sb.ToString());
sb.Length = 0;
}
}
}
private void UpdateText(string abc){
if(InvokeRequired){
Invoke(delegate(){
textBox.Text = abc;
});
}
else{
textBox.Text = abc;
}
}
|
|
|
|
|
But in case that the add method is call evry 5 milisecond - i have this exception again.
The exception is appear in the line that the text is sign up to the control.
Someone can help with this ?
Thanks
|
|
|
|
|
please answer with sample... i googled but i mixed up!
|
|
|
|
|
I'm not sure what this has to do with C#, but this[^] may be what you are looking for.
It's time for a new signature.
|
|
|
|
|
i meaned condition compilation symbols! there is no info about condition compilation symbols in that link
|
|
|
|
|
mersad00 wrote: there is no info about condition compilation symbols in that link
I have not used this myself but looking at the documentation I am of the opinion that you cannot add conditionals; it is assumed that they are already set in the project that is to be built.
It's time for a new signature.
|
|
|
|