From 203e265d9b92fd2f33f8e2784d42730c70343697 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Tue, 1 Oct 2019 23:28:01 +0200 Subject: [PATCH] Fix re-creating wxComboBox in widgets sample To preserve layout, old wxComboBox should be replaced with the new one in the same location in the sizer. So we need to create a new combo box first, put in the sizer and only after that the old combo box can be deleted. --- samples/widgets/combobox.cpp | 38 +++++++++++++++++------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/samples/widgets/combobox.cpp b/samples/widgets/combobox.cpp index 6618ceff1a..4fedfee377 100644 --- a/samples/widgets/combobox.cpp +++ b/samples/widgets/combobox.cpp @@ -417,6 +417,8 @@ void ComboboxWidgetsPage::Reset() void ComboboxWidgetsPage::CreateCombo() { + wxCHECK_RET( m_combobox, "No combo box exists" ); + int flags = GetAttrs().m_defaultFlags; if ( m_chkSort->GetValue() ) @@ -446,31 +448,27 @@ void ComboboxWidgetsPage::CreateCombo() } wxArrayString items; - if ( m_combobox ) - { - unsigned int count = m_combobox->GetCount(); - for ( unsigned int n = 0; n < count; n++ ) - { - items.Add(m_combobox->GetString(n)); - } - - m_sizerCombo->Detach( m_combobox ); - delete m_combobox; - } - - m_combobox = new wxComboBox(this, ComboPage_Combo, wxEmptyString, - wxDefaultPosition, wxDefaultSize, - 0, NULL, - flags); - - unsigned int count = items.GetCount(); + unsigned int count = m_combobox->GetCount(); for ( unsigned int n = 0; n < count; n++ ) { - m_combobox->Append(items[n]); + items.Add(m_combobox->GetString(n)); } + int selItem = m_combobox->GetSelection(); - m_sizerCombo->Add(m_combobox, 0, wxGROW | wxALL, 5); + wxComboBox* newCb = new wxComboBox(this, wxID_ANY, wxEmptyString, + wxDefaultPosition, wxDefaultSize, + items, + flags); + + if ( selItem != wxNOT_FOUND ) + newCb->SetSelection(selItem); + + m_sizerCombo->Replace(m_combobox, newCb); m_sizerCombo->Layout(); + + delete m_combobox; + m_combobox = newCb; + m_combobox->SetId(ComboPage_Combo); } // ----------------------------------------------------------------------------