I have a checked list and when the program loads, I need to load a list of string and boolean to the checklist box. But whilst setting the booleans with
checkedListBoxControl1.SetItemChecked(i, checkedList[i]);;
the checkedListBoxControl1_ItemCheck-Event Fires. I dont want that because when it fires, it refreshes my database and takes a long time to complete. I only want it to fire if user changes the checked list Check state.
Note: I have
Presently I am using A flag to do that and its ugly and giving me a lot of problems else here
private void checkedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) //fires second on check
{
int index = e.Index;
bool isChecked = e.State == CheckState.Checked;
this.mediaCenter.ItemManager.SetDirectoryCheck(index, isChecked);
if (this.IsUserClick)
BuildDatabaseAsync();
this.IsUserClick = false;
}
private bool IsUserClick;
private void checkedListBoxControl1_Click(object sender, EventArgs e) //Fires first on check
{
if (checkedListBoxControl1.SelectedItem == null) return;
IsUserClick = true;
}
May be my approach of filling the List-box Control is strange in the first place. But due to a lot of unwanted changes along the path. I do it as follows
private void BuildCheckListControl(string[] dirs)
{
IsUserClick = false;
this.checkedListBoxControl1.DataSource = dirs;
for (int i = 0; i < dirs.Length; i++)
checkedListBoxControl1.SetItemChecked(i, checkedList[i]);
}
checkedList[] contains an array of booleans corresponding to the dirs arrays
You can have a bool variable (class member not local variable) assigned false during your initialization. In the ItemCheck event check the bool variable and decide to proceed the DB check. Once the initialization completed set the bool variable to true.
If you don't want to create a boolean, that'll be checked you can (as stated in the comment) remove/add the eventhandler if you change your BuildCheckListControl-Method like this:
private void BuildCheckListControl(string[] dirs)
{
checkedListBoxControl1.ItemCheck -= checkedListBoxControl1_ItemCheck; //Will remove your Eventhandler
//IsUserClick = false; //You shouldn't need that anymore.
this.checkedListBoxControl1.DataSource = dirs;
for (int i = 0; i < dirs.Length; i++)
checkedListBoxControl1.SetItemChecked(i, checkedList[i]);
checkedListBoxControl1.ItemCheck += checkedListBoxControl1_ItemCheck; //Will add your Eventhandler again
}
Related
I am trying to disable some menu strip items while a textbox is displaying "Calculating...". Once that value goes away, I wish to re-enable the menu items. Its purpose is not to interrupt MD5/CRC32 calculations. So far, I've tried various method of code, and have had no luck so far. What's listed below should work, but for some reason it does not. Any help would be appreciated.
// THIS PART WORKS
if (boxMD5.Text.Contains("Calculating") == true)
{
openROMToolStripMenuItem.Enabled = false;
saveROMDataToolStripMenuItem.Enabled = false;
asTXTToolStripMenuItem.Enabled = false;
asHTMLToolStripMenuItem.Enabled = false;
}
// THIS PART DOES NOT WORK
else if (boxMD5.Text.Contains("Calculating") == false)
{
openROMToolStripMenuItem.Enabled = true;
saveROMDataToolStripMenuItem.Enabled = true;
asTXTToolStripMenuItem.Enabled = true;
asHTMLToolStripMenuItem.Enabled = true;
}
I can't quite tell you why the code isn't doing what you expect, but I can make a suggestion that will change your approach and may help achieve your goal at the same time. What you are trying to do shouldn't be to disable the menu when the textbox contains "Calculating" but instead you should disable the menu while the calculations are being performed. From a user/UI perspective, these are the same thing, but the inner-workings of your program know better.
Based on you PasteBin code, try this:
private void openROMToolStripMenuItem_Click(object sender, EventArgs e)
{
//Other code omitted for brevity
if (File.Exists(OpenFileDialog1.FileName))
{
UpdateUI("Calculating...");
backgroundWorker1.RunWorkerAsync(OpenFileDialog1.FileName);
}
//Other code omitted for brevity
}
and
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
UpdateUI(e.Result.ToString());
}
where the new method UpdateUI() looks like this:
void UpdateUI(string hash)
{
var calculating = hash == "Calculating...";
if (!calculating)
{
progressBar1.Value = 0;
}
openROMToolStripMenuItem.Enabled = !calculating;
saveROMDataToolStripMenuItem.Enabled = !calculating;
asTXTToolStripMenuItem.Enabled = !calculating;
asHTMLToolStripMenuItem.Enabled = !calculating;
boxMD5.Text = hash;
}
Also, notice how you are able to just put !calculating in the if statement rather than calculating == false. This is because the value is already true or false so you don't have to compare it to anything to figure that out. The same thing applies to your original code but you don't need it anymore with this approach.
Background:
In my winforms form, I have a Checked ListView and a "master" checkbox called checkBoxAll.
The behaviour of the master is as follows:
If the master is checked or unchecked, all ListViewItems must change accordingly.
If the user unchecks a ListViewItem, the master must change accordingly.
If the user checks a ListViewItem, and all other ListViewItems are checked aswell, the master must change accordingly.
I have written the following code to mimic this behaviour:
private bool byProgram = false; //Flag to determine the caller of the code. True for program, false for user.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
//Check if the user raised this event.
if (!byProgram)
{
//Event was raised by user!
//If checkBoxAll is checked, all listviewitems must be checked too and vice versa.
//Check if there are any items to (un)check.
if (myListView.Items.Count > 0)
{
byProgram = true; //Raise flag.
//(Un)check every item.
foreach (ListViewItem lvi in myListView.Items)
{
lvi.Checked = checkBoxAll.Checked;
}
byProgram = false; //Lower flag.
}
}
}
private void myListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
//Get the appropiate ListView that raised this event
var listView = sender as ListView;
//Check if the user raised this event.
if (!byProgram)
{
//Event was raised by user!
//If all items are checked, set checkBoxAll checked, else: uncheck him!
bool allChecked = true; //This boolean will be used to set the value of checkBoxAll
//This event was raised by an ListViewItem so we don't have to check if any exist.
//Check all items untill one is not checked.
foreach (ListViewItem lvi in listView.Items)
{
allChecked = lvi.Checked;
if (!allChecked) break;
}
byProgram = true; //Raise flag.
//Set the checkBoxAll according to the value determined for allChecked.
checkBoxAll.Checked = allChecked;
byProgram = false; //Lower flag.
}
}
In this example, I use a flag (byProgram) to make sure an event was caused by the user or not, thereby preventing an infinite loop (one event can fire another, which can fire the first one again etc. etc.). IMHO, this is a hacky solution.
I searched around but I couldn't find a MSDN documented method to determine if an User Control Event was directly fired thanks to the user. Which strikes me as odd (again, IMHO).
I know that the FormClosingEventArgs has a field which we can use to determine if the user is closing the form or not. But as far as I know, that is the only EventArg that provides this kind of functionality...
So in summary:
Is there a way (other than my example) to determine if an event was fired directly by the user?
Please note: I don't mean the sender of an event! It won't matter if I code someCheckBox.Checked = true; or manually set someCheckBox, the sender of the event will always be someCheckBox. I want to find out if it is possible to determine whether it was through the user (click) or by the program (.Checked = true).
Aaand also: 30% of the time it took to write this question was to formulate the question and the title correctly. Still not sure if it is a 100% clear so please edit if you think you can do better :)
No, there's no practical way to determine whether the change came from GUI or was done by program (in fact, you could analyze the callstack - but that's not recommended because it's very slow and error-prone).
BTW, there's one other thing you could do instead of setting byProgram. You could remove and add the event handler prior or after, respectively, change your controls:
checkBoxAll.CheckedChanged -= checkBoxAll_CheckedChanged;
// do something
checkBoxAll.CheckedChanged += checkBoxAll_CheckedChanged;
Instead of using the changed event, you could use the clicked event to cascade the change through to the relevant controls. This would be in response to a user click, and not the value being changed programatically.
This is something I come across quite a lot and what I tend to try do is not split it between user interaction vs program interaction - I use more generic code i.e. the UI is being updated and doesn't require any events to be handled. I usually package this up through BeginUpdate/EndUpdate methods e.g.
private int updates = 0;
public bool Updating { get { return updates > 0; } }
public void BeginUpdate()
{
updates++;
}
public void EndUpdate()
{
updates--;
}
public void IndividualCheckBoxChanged(...)
{
if (!Updating)
{
// run code
}
}
public void CheckAllChanged(...)
{
BeginUpdate();
try
{
// run code
}
finally
{
EndUpdate();
}
}
I have a tough time trying to solve this problem. I have been at for 3 hours, and still I couldn't find out why it is doing this. Here is the code:
private void Catagory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedCategoryId = categoryIdList[categoryListBox.SelectedIndex];
client.GetItemsAsync(selectedCategoryId);
client.GetItemsCompleted +=
new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
}
void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
itemIdList.Clear();
itemNameList.Clear();
itemNumberList.Clear();
itemDisplayList.Clear(); //Clears the Display List Items
if (e.Error == null)
{
itemIdList = e.ItemIDList;
itemNumberList = e.itemNumber;
itemNameList = e.Result;
for (int i = 0; i < itemIdList.Count; i++)
{
itemDisplayList.Add(new ItemDisplay { itemNumber = itemNumberList[i], itemName = itemNameList[i] });
}
//Populating the listbox controll with the itemDisplaylist...
Items.ItemsSource = itemDisplayList;
}
else
{
MessageBox.Show("Problem in getting the items list.");
}
}
When I change the category the first time it works perfectly. By perfectly, I mean that it calls the function GetItemsAsync(selectedCategoryId) and grabs the results and calls the event handler client_GetItemsCompleted() and the inner working of the event handler works as it is supposed to, it sets the lists with the proper data and displays the itemNumber and the itemName in the list box . But, when I change the category again to get different items, it doesn't work properly, what it's doing is that it clears the lists and populates the lists as it is supposed to, runs the for loop and populates the listBox called Items but for some reason it goes to the top of the function again and empties all the lists. Please tell my why it is executing the function again? And when I choose another category again, it executes the event handler 3 times and then 4 times and so on. Wnow why it is doing this?
Everytime this is executed:
client.GetItemsCompleted +=
You add a subscriber to the event, so the second time it will fire twice (the third time triple times, etc..).
Either unsubscrice ( -= ) in the completed method:
void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
try {
/* .... */
}
finally {
client.GetItemsCompleted -=
new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
}
}
or initiate the client object before every call.
var client = new ...();
client.GetItemsAsync(selectedCategoryId);
client.GetItemsCompleted +=
new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
I have following code:
private void askforlocation()
{
if (File.Exists("location.txt"))
{
System.IO.StreamReader loc = new System.IO.StreamReader("location.txt");
string loca = loc.ReadToEnd();
if (loca != "")
{
int index = comboBox1.FindString(loca);
comboBox1.SelectedIndex = index;
}
else
{
label6.Text = "Please select the location!";
}
loc.Close();
}
else label6.Text = "Please select the location!";
}
It is supposed to read value "location" from the file and put it to the combo box, which works ok.
I run this script on Form1_Load.
Now, I have another script:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = comboBox1.SelectedItem.ToString();
System.IO.File.WriteAllText("location.txt", value);
}
This one is supposed to record the choice so that user doesn't need to enter location every time.
What is happening is when I start a program, so the value is already set, then I try to change it (so that theoretically it should overwrite the previous one), but I get an exception, saying that file is already being used by another process.
I do close the file after I used it. I also tried FILE.DISPOSE.
What am I doing wrong?
I think what's happening here is that this code:
if (loca != "")
{
int index = comboBox1.FindString(loca);
comboBox1.SelectedIndex = index;
}
is causing the SelectedIndexChanged event to be raised on the combobox. When that event is raised, comboBox1_SelectedIndexChanged is called, and that method again tries to access location.txt.
To fix, I would first change the code in askforlocation to something like this:
if (File.Exists("location.txt"))
{
var loca = string.Emtpy;
using(var loc = new System.IO.StreamReader("location.txt"))
{
loca = loc.ReadToEnd();
}
....
}
since there's no need to keep the file open for longer than necessary (note that the using block will call the Dispose() method on the StreamReader when it exits, which in turn will call the Close() method). After that, I'd consider coming up with a way to keep the event from being fired when you set the selected index on the combobox (maybe use a flag or unwire/rewire the event handler).
It seems that you're changing the index of your combobox, thus writing to the same file before closing it. Call loca.Close() before writing to the file again.
comboBox1.SelectedIndex = index;
this will fire the event SelectedIndexChanged, so invoke the Close() method right behind ReadToEnd():
private void askforlocation()
{
if (File.Exists("location.txt"))
{
System.IO.StreamReader loc = new System.IO.StreamReader("location.txt");
string loca = loc.ReadToEnd();
loc.Close();//move that code to here
if (loca != "")
{
int index = comboBox1.FindString(loca);
comboBox1.SelectedIndex = index;
}
else
{
label6.Text = "Please select the location!";
}
//loc.Close();
}
else label6.Text = "Please select the location!";
}
Give this line loc.Close(); before setting the index of the combo box because the event is being raised earlier than you think.
You never need to call file.Close() or file.Dispose().
Please use a using statement ALWAYS (or mostly) when using a class that implements IDisposable. It will call the Dispose method for you.
using(System.IO.StreamReader loc = new System.IO.StreamReader("location.txt"))
{
string loca = loc.ReadToEnd();
}
I have a method that disables all the butttons on my window.
But i can't seem to get the type of Button to match it to the Resource collection
I'm using Expression Blend 3 with a c# code-behind
void DisableButtons()
{
for(int i = 0; i>= this.Resources.Count -1; i ++)
{
if (this.Resources[i].GetType() == typeof(Button))
{
Button btn = (Button)this.Resources[i];
btn.IsEnabled = false;
}
}
}
Update
Thanks for the answers!
Ok The loop is working but my code is incorrect.
this.Resources
Does not seem to include my buttons! This could be an Blend thing?
So Yeah.
I ended up doing it manually. Cause I'm hasty and there isn't a short easy solution. Thanks for all the input though!
void DisableButtons()
{
for(int i = 0; i < Resources.Count; i ++)
{
var btn = Resources[i] as Button;
if(btn != null)
{
btn.IsEnabled = false;
}
}
}
Easy way to implement it is use foreach instruction with LINQ query, but this way need more resuources whan easy for.
void DisableButtons()
{
foreach(var button in Resources.OfType<Button>())
{
button.IsEnabled = false;
}
}
It is possible I misunderstood something, but you're trying to find Buttons CONTAINED in your window in the RESOURCES of your window? Because those two things are different things alltogether.
If that is the case, either try setting this.IsEnabled = false (but that disables other things, not just buttons), or traverse the logical tree (or visual tree if silverlight) with LogicalTreeHelper/VisualTreeHelper, although that is a VERY expensive method.
Manual workaround would be to give names to all your buttons, make a list of them in codebehind and iterate that list.
However the best would be to create a boolean property called AreButtonsEnabled in your ViewModel (if you're not using MVVM than simply on the control itself - but make it DependencyProperty) and bind all your button's IsEnabled property to them! And then in codebehind simply set that boolean to false ... and magic ensues.
If this is not your case then sorry I wasted your time.
what about this?
if (this.Resources[i].GetType() == typeof(Button)))
or even better
if (this.Resources[i] is Button))
What about that?
if (this.Resources[i] is Button)
That way you can get anything that inherits from Button.
It looks like your loop statement is wrong. i>= this.Resources.Count -1 should be i <= this.Resources.Count - 1; It'll never get into your loop.
Also, this is just a style thing, but I'd rewrite it as:
for(int i = 0; i < Resources.Count; i ++)
{
Button btn = Resources[i] as Button; // btn will be null if not a Button
if( btn != null )
{
btn.IsEnabled = false;
}
}
I use a simple method.
First you create a bool.
bool enableButtons = true;
Now I add a timer to the form which is active all the time.
private void timer1_Tick(object sender, EventArgs e)
{
if (enableButtons = false)
{
button1.Enabled = false;
button2.Enabled = false;
}
else
{
button1.Enabled = true;
button2.Enabled = true;
}
}
So whenever I want to disable the buttons, I just change enableButtons to false.