• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Just C# being weird

Jpp

Administrator
So I have had an issue with a listBox duplicating the items in it when you select a new item.

H6E58fr.gif


Code:
listTitles.Items.Clear();
listTitles.Items.Add(1234);

But when I replace the old code with this code, the 'bug' doesn't occur:

Code:
listTitles.Items.Clear();
listTitles.Items.Clear();
listTitles.Items.Add(1234);

zpSzkU2.gif


I'm having a hard time figuring out the logic behind it, do any of you have an idea?
 

uDev

Illustrious Member
Add full source with functions not only snippet. Here i don't see anything wrong maybe its in the rest of source. Also if you are first time coding better start with vb.net than c# it will be much easier for you
 

Jpp

Administrator
Add full source with functions not only snippet. Here i don't see anything wrong maybe its in the rest of source. Also if you are first time coding better start with vb.net than c# it will be much easier for you

kek

Giving you the whole function won't really do much to you.

Code:
        private async void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = sender as TabControl;
            
            if (item.SelectedIndex == 1)
            {
                Exception exception = null;
                try
                {
                    DBCStores.InitFiles();
                    DBCStores.LoadTitleEditorFiles();
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                if (exception != null)
                {
                    await this.ShowMessageAsync("Error", exception.ToString());
                    this.Close();
                }

                //listTitles.Items.Clear();
                listTitles.Items.Clear();
                foreach (CharTitlesEntry t in DBCStores.CharTitles.Records)
                    listTitles.Items.Add(t);
            }
        }

Note that I have done a few changes to it, but the same 'bug' will still occur.

Btw, vb.net is a shitty language, don't recommend that to anyone.
 
Last edited:

Rochet2

Moderator / Eluna Dev
I dont know at all how it should look work or anything and never used C# and so on..

But..
It looks to me like the top choice is the current choice.

So if you had like a list with 987 876 765, it would show the selected at the top.
By default it shows the selected, then when you click it, it offers all the options, including the selected one. The selected also shows at the top:
mUdSJbe.png
 
Last edited:

Jpp

Administrator
I dont know at all how it should look work or anything and never used C# and so on..

But..
It looks to me like the top choice is the current choice.

So if you had like a list with 987 876 765, it would show the selected at the top.
By default it shows the selected, then when you click it, it offers all the options, including the selected one. The selected also shows at the top:
Original view:
987

*clicks it*
987
987
876
765

*clicks 876*
876

*clicks it*
876
987
876
765

That's not the case.

It duplicates or loads all of the items again in the listBox. (My example is just bad, I should have put more numbers in it.)
 

Jpp

Administrator
I dont know at all how it should look work or anything and never used C# and so on..

But..
It looks to me like the top choice is the current choice.

So if you had like a list with 987 876 765, it would show the selected at the top.
By default it shows the selected, then when you click it, it offers all the options, including the selected one. The selected also shows at the top:
mUdSJbe.png

Here is a better example.

PBLBB8G.gif
 

Hyperion

Founder
Should use as a string in the foreach and don't forget to use your brackets { } You need them in loops, just not IF statements.
I know you don't 'need' them for one liners, but you should still use it for loops.


What I mean about string:

Code:
listTitles.Items.Clear();
string []lst =   DBCStores.CharTitles.Records;
foreach (CharTitlesEntry t in lst)
{
     listTitles.Items.Add(t);
}
 

Jpp

Administrator
Should use as a string in the foreach


What I mean about string:

Code:
listTitles.Items.Clear();
string []lst =   DBCStores.CharTitles.Records;
foreach (CharTitlesEntry t in lst)
{
     listTitles.Items.Add(t);
}

It won't really make any difference tbh.

and don't forget to use your brackets { } You need them in loops, just not IF statements.
I know you don't 'need' them for one liners, but you should still use it for loops.

That's just preference, like you stated. I personally try to avoid curly brackets when possible.


Just a note; I know what is causing it, I just find it weird that adding "listTitles.Items.Clear();" twice makes it stop duplicating or that it's duplicating in the first place.
 
Last edited:

Hyperion

Founder
It won't really make any difference tbh.
That's just preference, like you stated. I personally try to avoid curly brackets when possible.


Just a note, I know what is causing it, I just find it weird that adding "listTitles.Items.Clear();" twice makes it stop duplicating.


Mk.

If you avoid using open and close brackets, then you're lazy coding and not doing it properly.
Apologies you didn't think my loop would make a difference considering it's stored in a string list first before reading it in the foreach.

At least you found out what's caused it, but clearing the list shouldn't have to be your bug fix.
 

Jpp

Administrator
If you avoid using open and close brackets, then you're lazy coding and not doing it properly.

Improperly in your(and others?) view, yes.

I'm used to read the code this way, so I don't encounter problems with this when I'm debugging, or at least I haven't yet.

It also takes up 2 more lines, which doesn't seem to be worth it for me. Obviously you could change the way you write it, but that would make you have to do that with everything, else it would look out of place.

Apologies you didn't think my loop would make a difference considering it's stored in a string list first before reading it in the foreach.

Don't take me wrong, it's not that I don't appreciate your help, it's that in theory you will get the same result.

At least you found out what's caused it, but clearing the list shouldn't have to be your bug fix.

It's just the way WPF is handling SelectionChanged.

I agree, it shouldn't, but it does. That's really why I created this thread; not knowing why it fixes it.
 
Last edited:

Hyperion

Founder
Improperly in your(and others?) view, yes.

I'm used to read the code this way, so I don't encounter problems with this when I'm debugging, or at least I haven't yet.

It also takes up 2 more lines, which doesn't seem to be worth it for me. Obviously you could change the way you write it, but that would make you have to do that with everything, else it would look out of place.



Don't take me wrong, it's not that I don't appreciate your help, it's that in theory you will get the same result.



It's just the way WPF is handling SelectionChanged.

I agree, it shouldn't, but it does. That's really why I created this thread; not knowing why it fixes it.

It's not about the same result, it's about how you're going about it. My point is it will take less time to read from the string list rather than the foreach because it's already stored.
it was totally irrelevant to your issue anyway, so I probably shouldn't even have brought it up.

I also didn't realize this was WPF, there's a slight difference in some syntax.
 

Jpp

Administrator
it was totally irrelevant to your issue anyway, so I probably shouldn't even have brought it up.

I misunderstood you then.

I also didn't realize this was WPF, there's a slight difference in some syntax.

I should probably have mentioned that.

It's not about the same result, it's about how you're going about it. My point is it will take less time to read from the string list rather than the foreach because it's already stored.
Oh and it wouldn't work in this context, since it's not a string as such. But you obviously wouldn't know that, since you don't have all the code.
 
Last edited:

Hyperion

Founder
Oh and it wouldn't work in this context, since it's not a string as such. But you obviously wouldn't know that, since you don't have all the code.

It's not a single string no, the foreach reads it as a list of strings hence string [] stores as a list. It should still print out the same way.
We are going a bit off topic here though :p

But from what I understand, you're trying to add every entry into a listbox right?
 

Jpp

Administrator
It's not a single string no, the foreach reads it as a list of strings hence string [] stores as a list. It should still print out the same way.

DBCStores.CharTitles.Records is not a string, so this code would give an error. I would show you if I had access to a compiler, but I don't atm.

Code:
string[] lst = DBCStores.CharTitles.Records;

We are going a bit off topic here though :p

Just a little, I don't mind, but perhaps we should keep to the topic.

But from what I understand, you're trying to add every entry into a listbox right?

I'm mainly just trying to figure out why this work

Code:
listTitles.Items.Clear();
listTitles.Items.Clear();
foreach (CharTitlesEntry t in DBCStores.CharTitles.Records)
{
    listTitles.Items.Add(t);
}

and this doesn't

Code:
listTitles.Items.Clear();
foreach (CharTitlesEntry t in DBCStores.CharTitles.Records)
{
    listTitles.Items.Add(t);
}
 
Last edited:

neglected

Enthusiast
Can you guarantee listTitles is only being modified by one thread?
This seems like a textbook concurrency error. I'm not aware of how WPF works but try wrapping all modifications to listTitles in a lock statement.
 
Top