• 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
Would be nice to see the full code instead of snippets.

Code:
        public MainWindow()
        {
            InitializeComponent();
        }

        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);
            }
        }

        private void SelectTitle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listTitles.SelectedIndex == -1)
                return;

            CharTitlesEntry t = (CharTitlesEntry)listTitles.Items[listTitles.SelectedIndex];

            textbox_titleId1.Text = t.Id.ToString();
            textbox_titleId2.Text = t.TitleMaskId.ToString();
            textbox_titleMaleName.Text = t.NameMale;
            textbox_titleFemaleName.Text = t.NameFemale;
        }

That should be everything, unless you are looking for the xaml too.

As I stated before; I know what bugs it out, it just doesn't make sense that adding "listTitles.Items.Clear();" twice fixes the duplication.
 
Last edited:

Tommy

Founder
I can't seem to recreate the problem. I have items in my listBox, it clears when selection is changed and when I do other changes. What's with using async? Just using it for an error message doesn't seem like a good beneficial place to use it. It seems as if you aren't using more than one thread, I can't really tell to be honest. Have you looked at DBCStores code to make sure it isn't causing this? Don't see how it would, but it wouldn't hurt.

I could probably fix it if I had the entire solution. I can't work off of small stuff when there's a static issue constantly going on.
 

Jpp

Administrator
I can't seem to recreate the problem. I have items in my listBox, it clears when selection is changed and when I do other changes. What's with using async? Just using it for an error message doesn't seem like a good beneficial place to use it. It seems as if you aren't using more than one thread, I can't really tell to be honest. Have you looked at DBCStores code to make sure it isn't causing this? Don't see how it would, but it wouldn't hurt.

I could probably fix it if I had the entire solution. I can't work off of small stuff when there's a static issue constantly going on.

Are you using WPF or WinForm?

And I can reproduce the problem with any items in the listBox.

As I've done a lot of changes to the program I don't have the old source for it, but here is another program that will reproduce the same bug.
https://mega.co.nz/#!tUAC0Z5J!MOGaBOhoK3SVH6cgwDwRuTZQU7eQLWj2Vgu5NEAop9k
 
Last edited:

Tommy

Founder
Are you using WPF or WinForm?

And I can reproduce the problem with any items in the listBox.

As I've done a lot of changes to the program I don't have the old source for it, but here is another program that will reproduce the same bug.
https://mega.co.nz/#!tUAC0Z5J!MOGaBOhoK3SVH6cgwDwRuTZQU7eQLWj2Vgu5NEAop9k

Why would I be using WinForm? I know what your project is in. :p

That's all I need. A source with the bug happening. Looking at it now.


----EDIT

As far as I can tell, the listbox isn't clearing because you're adding more into the listbox as soon as it tries to clear. That seems to be the duplication problem there.

So, why not do something like this?

Code:
        private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = sender as TabControl;

            if (item.SelectedIndex == 1)
            {
                if (listBox01.Items.Count > 0)
                    listBox01.Items.Clear();
                else
                {
                    listBox01.Items.Add(1234);
                    listBox01.Items.Add(1234);
                }
            }
        }

Only problem with this is somehow SelectionChanged is calling twice sometimes, but it works.
 
Last edited:

Jpp

Administrator
Why would I be using WinForm? I know what your project is in. :p

That's all I need. A source with the bug happening. Looking at it now.


----EDIT

As far as I can tell, the listbox isn't clearing because you're adding more into the listbox as soon as it tries to clear. That seems to be the duplication problem there.

So, why not do something like this?

Code:
        private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = sender as TabControl;

            if (item.SelectedIndex == 1)
            {
                if (listBox01.Items.Count > 0)
                    listBox01.Items.Clear();
                else
                {
                    listBox01.Items.Add(1234);
                    listBox01.Items.Add(1234);
                }
            }
        }

Only problem with this is somehow SelectionChanged is calling twice sometimes, but it works.

Oh, I already knew a fix for it.

All of the SelectionChanged events are the same, so that's why it's calling it 'twice'. To prevent that; simply set 'e.Handled = true'.

You could also check if it was the original source that called the event.


What I was trying to figure out though, was why clearing the listBox twice in a row 'fixed' the issue.

Thanks for your input.
 
Last edited:

Tommy

Founder
Oh, I already knew a fix for it.

All of the SelectionChanged events are the same, so that's why it's calling it 'twice'. To prevent that; simply set 'e.Handled = true'.

You could also check if it was the original source that called the event.


What I was trying to figure out though, was why clearing the listBox twice in a row 'fixed' the issue.

Thanks for your input.

The reason why twice in a row fixed the issue was probably because the event was ticking more than once which conflicted with the previous selection changed event. Though, I'm not entirely sure. I can think of the logic in my head but I can't explain it properly. It was somewhere in between it calling twice and adding data in there multiple times.
 

Jpp

Administrator
The reason why twice in a row fixed the issue was probably because the event was ticking more than once which conflicted with the previous selection changed event. Though, I'm not entirely sure. I can think of the logic in my head but I can't explain it properly. It was somewhere in between it calling twice and adding data in there multiple times.

I see now. Since it's clearing the listBox, there are no items left, so it sets the selected item to -1, which fires the event once again, and I guess it just keep on going like that until it cancels itself out.

Didn't really put a lot of thought in it until now since it didn't really matter, I just found it strange at the moment it happened.
 
Last edited:

Tommy

Founder
I see now. Since it's clearing the listBox, there are no items left, so it sets the selected item to -1, which fires the event once again, and I guess it just keep on going like that until it cancels itself out.

Didn't really put a lot of thought in it until now since it didn't really matter, I just found it strange at the moment it happened.

Yeah. Weird stuff. Basic logic though.
 
Top