![]() |
|
[.NET] cross-thread, cross-class adding items to a listbox. [RESOLVED WITH CODE] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] cross-thread, cross-class adding items to a listbox. [RESOLVED WITH CODE] (/showthread.php?tid=4908636) Pages: Pages
1
2
|
cross-thread, cross-class adding items to a listbox. [RESOLVED WITH CODE] - Vrekt - Jul 15, 2015 UPDATE: Thanks everybody who replied and tried to help me, I learned some new things today :). Here is how I solved the problem. NOTE: all this code is done in the main form containing the listbox. Thanks to Odus btw :P Firstly in your other class containing the thread put Code Public Shared flag as ObjectSecondly the function to invoke the listbox: Code Public Delegate Sub DelUpdateListBox(lb As ListBox, item As String)^^ Does not have to be public I just did that because I'm a skid and thought I needed it.... Alright great! Now we need some variables.. Code ' // We are going to be doing this on another sub, so to stop it from writing the item multiple times we need this.Awesome. So now create a new sub named "additemtolistbox" (or whatever you prefer) In this sub put this: Code If yourotherclass.flag Is Nothing ThenAlright 2 more things now. We basically want this sub to loop, so the lazy way to do so is call the sub in form paint event. So now in your other thread in the other class, simply put Code flag = "item to add"That should give you a basic idea of how this would work. This code is probably buggy, but I hope it helps some of you :) * - tweety - Jul 16, 2015 Saw thread title and walked in, then I saw the code.
* - Vrekt - Jul 16, 2015 (Jul 16, 2015 12:01 AM)Tweety. Wrote: Saw thread title and walked in, then I saw the code. ahhaha yea I'm sort of a vb noob. * - Vysse - Jul 16, 2015 (Jul 15, 2015 11:38 PM)Vrekt Wrote:NO NO NONONO NONONONONONO NONONO NO SERIOUSLY JUST FUCKING NO AND IF YOU THINK ABOUT IT AGAIN THEN NO. That is my input for this. * - Shyon143 - Jul 16, 2015 this is so new lmao idk what this is I need help learning this would make more sense * - Josh - Jul 16, 2015 Off the top of my head, in the form that has the listbox, create the following function: Code Public Function addToListBox(ByVal LB As ListBox, ByVal item As Object) As Boolean* - IntelliJ - Jul 16, 2015 (Jul 15, 2015 11:38 PM)Vrekt Wrote:For reference: VB comment: ' C# comment: // There's no need to do both last time I checked. * - HFTetramorph - Jul 16, 2015 (Jul 16, 2015 01:33 AM)IntelliJ Wrote: For reference: User is asshat- But not wrong * - IntelliJ - Jul 16, 2015 (Jul 16, 2015 01:34 AM)BinaryEvolved Wrote: User is asshat- But not wrong Thanks sweetheart. 🤗 * - Vrekt - Jul 16, 2015 (Jul 16, 2015 01:33 AM)IntelliJ Wrote: For reference: you are correct, just I prefer the look. * - HFTetramorph - Jul 16, 2015 Quote:Control.CheckForIllegalCrossThreadCalls = False NO NO NO! JUST PROPERLY INVOKE THE NEXT THREAD! This is terrible practice and should never be used. * - Vrekt - Jul 16, 2015 (Jul 16, 2015 01:23 AM)Josh Wrote: Off the top of my head, in the form that has the listbox, create the following function: Nah didn't work, I must be doing something wrong, I have tried something similar to this before. I'm using this code to use the function. NOTE: para_main is the form with the listbox. Code para_main.addToListBox(para_main.console, "test")* - Odus - Jul 16, 2015 Delegate! Code Private Delegate Sub DelUpdateListBox(lb As ListBox, item As String)Still not the most efficient way to do it though, I recommend the ManagementEventWatcher class. * - Vrekt - Jul 16, 2015 (Jul 16, 2015 02:17 AM)Odus Wrote: Delegate! Thanks so much I implemented your code and it worked! Thanks everybody else as well.. I learned some new things today :). I updated my post with how I solved the problem, (very inefficient, but works) for anybody else that has the problem. * - Vysse - Jul 16, 2015 (Jul 16, 2015 02:54 AM)Vrekt Wrote: Thanks so much I implemented your code and it worked! Thanks everybody else as well.. I learned some new things today :). I updated my post with how I solved the problem, (very inefficient, but works) for anybody else that has the problem. Did you remove the CheckForIllegalCrossThreads = False? If not please do. |