 |

|
When posting your question please:- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|

|
Well, I'm plum out of ideas. I've got a listbox with a List<> for its DataSource. When I init it - before the form is displayed, it works. But not after that. I am modifying the List<> contents and then I run:
public void RefreshLbx()
{
lbxQuotes.DataSource = null;
lbxQuotes.DataSource = _quotes;
}
where _quotes is the List<>
Now here is where it gets weird. I put a button on the form - here's its handler:
private void btnRefresh_Click(object sender, EventArgs e)
{
RefreshLbx();
}
So that when I click on the button then the lbxQuotes listbox is properly updated. But in the modification code - where I call RefreshLbx() directly, nothing happens.
I even tried invoking btnRefresh_Click() in the code, but alas could not find the magic.
Oh one other thought - the modification code is in a timer event handler - could this be a problem of trying to modify a control from a different thread? All I am doing is changing the List<> contents and then reseting the DataSource.
I must be missing something to kick the listbox to redisplay, or something... HELP!
|
|
|
|

|
Wow but this is weird. Okay, I found the solution because I tried the recommendation given below:
lbxQuotes.DataSource = null;
lbxQuotes.DataSource = _quotes;
lbxQuotes.SelectionMode = SelectionMode.None;
lbxQuotes.SelectionMode = SelectionMode.One;
That is, I added the SelectionMode two lines... and this caused (only once) a "from different thread" exception. Weird, I'm NOT running a method - I'm changing data... hmmm... I wonder if the DataSource is instead a Property (which secretly runs a method). D'oh!
Okay, so the solution was to create a delegate in the main form, for the RefreshLbx method, and have the RefreshLbx method recursively call this, as below:
public void RefreshLbx()
{
if (lbxQuotes.InvokeRequired)
{
lbxQuotes.Invoke(_refreshLbx);
}
else
{
lbxQuotes.DataSource = null;
lbxQuotes.DataSource = _quotes;
}
}
with the _refreshLbx delegate defined in the form's class as:
private delegate void RefreshLbxDg8();
private RefreshLbxDg8 _refreshLbx;
Then just init the delegate in the form constructor (after the InitializeComponent() call):
_refreshLbx = RefreshLbx;
Now I am able to update the listbox contents from a timer handler.
|
|
|
|

|
"System.Threading.Timer" is not threadsafe. You didn't use the Timer from the Toolbox, did you?
rbsbscrp wrote: Weird, I'm NOT running a method - I'm changing data... hmmm... You're changing a property from a different thread than where the control was created.
rbsbscrp wrote: if the DataSource is instead a Property (which secretly runs a method). Yup, it's a property; meaning it's built from two methods (not threads!)
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|

|
You can NOT so much as touch ANY part of a control from a thread other than the one that created it, the UI (or startup) thread. This is where your entire problem comes from.
You have to Invoke methods on the UI thread that manipulates the control for your calling code.
|
|
|
|

|
My thanks to all who responded.
>>You can NOT so much as touch ANY part of a control from a thread other than the one that created it, the UI (or startup) thread. This is where your entire problem comes from.
My confusion was mostly that I was not trying to run a Method at all - I was just changing a Property (or what I thought was DATA). Upon more consideration, I realized that a Property is just syntactic sugar over a procedure call, anyway, so the "you can't run code from a different thread" rule applied even changing a Property.
However, I am mystified as to WHY this rule "no touchie from different thread" even exists. Sure, if I'm trying to update a TextBox from other threads then there *could* be confusion and a random mix of characters added from the various threads due to the asynchronous timing of the code in the different threads. Yes, there *could* be... it is *possible* - just like I could really mess up a data structure if I update it from many threads without synchronization.
But that is exactly what multithreaded coding is all about - synchronizing the use of common data structures (at least it is a major issue, if not the biggest issue).
But why would the compiler assume that I am needing multithreading synchronization when updating a Textbox? The truth is, I am doing something at timed intervals - I am using a timer. It just so *happens* that C# chooses internally to implement a timer via a separate new thread. That is not *my* problem. I'm not trying to append text to the textbox from 17 different timers or asynchronous threads. I'm simply trying to update a textbox at constant intervals - in this case the system chooses to do this via ONE (other) thread.
Suddenly now I have to become fully knowledgeable about multithreading and delegates and "invoking", etc. BAH! Unnecessary, unneeded, unwanted complexification.
Aside from all that, I especially did not appreciate that the code simply failed to do anything - I got no compiler warning/error nor any runtime problem. My dialogbox simply didn't update. Then I had the fire up The Google and go searching for why it didn't work. My first real clue was that if I caused the update to happen from the very same function, via a click of a button on the UI, then it worked. Whaaa? My code definitely worked, but only if executed via a pushbutton.
I've coded for years, and I'm teaching myself C# now, and "yeah" this is a noobie kinda problem, but geeeez what an unnecessary timewaster.
Okay, 'nuff whining. Thx to all who replied. I found some code example for doing a "invoke" via a deleagate, etc., etc., and got it working.
|
|
|
|

|
Try This :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<KeyValuePair<int, string>> data = new List<KeyValuePair<int, string>>();
KeyValuePair<int, string> item;
for (int i = 1; i < 10; i++)
{
item = new KeyValuePair<int, string>(i, "Item " + i.ToString());
data.Add(item);
}
listBox1.DataSource = data;
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Key";
}
private void button1_Click(object sender, EventArgs e)
{
KeyValuePair<int, string> item = new KeyValuePair<int,string>(11,"Item 11");
List<KeyValuePair<int, string>> data = (List<KeyValuePair<int, string>>)listBox1.DataSource;
data.Add(item);
listBox1.DataSource = null;
listBox1.DataSource = data;
}
}
|
|
|
|

|
Kuel. I like that you can use a List<> of Key/Values for a ListBox. Did not know that. Then can use the DataMember, ValueMember flexibility. Thx. However...
You are executing the update from a button_click Method - thus the code is executed on the UI thread - thus it will work. My problem is that I am executing from a timer handler - because I need the update at regular intervals (taking samples) - thus the handler is on a different thread and thus, when executed, silently does absolutely nothing.
<head scratch>
Thx for the code. Educational. (and 'yes' I got it working)
|
|
|
|

|
I have two objects, PriceRule and WeekDay, and PriceRule has a DayId column I have bound to a ComboBox column in a DataGridView. When I load the grid, if DayId is null, the combo is blank, but as soon as I drop the combo down, I have to select a day. No problem, I can inject an extra empty string day into the combo's data source, but the Id property of WeekDay is not nullable, so I need to do something as clumsy and stupid as insert a false day with Id of -1.
How do I catch this -1 and make it null before saving, and vice versa, catch a null DayId and make it -1 when loading?
Is there no other way to do what is a bloody common task that MS clearly isn't capable of handling themselves?
|
|
|
|

|
I'm creating an user control like treeView, in this control, I want to put a button to let my user control hide/show in Owner form.
how could I do?
thanks
|
|
|
|
 |
|