none
SOS!!! Program crash when input text in some textboxes with autocomplete function, c# RRS feed

  • Question

  • I have multiple textboxes with autocomplete functions, however, when I try to input values in some of them the program crashes without any error message where I use visual studio 2017 to edit this c# program. I have checked that all these textboxes are with a normal autocomplete collection list.

    I have no clues why it crashes, even if these textboxes are in the same tab page.

    private DataTable dt_tbx_autocomplete = new DataTable();
        private void bgw_load_auto_text_do_work_process(int j, int k, int m, DoWorkEventArgs e)
        {
            cmdDatabase = new MySqlCommand("select*from database", conDatabase);
            DataTable dtPosts = new DataTable();
            for (int i = j; i < k; i++)
            {
                string[] postSource = dt_tbx_autocomplete
                                .AsEnumerable()
                                .Select<System.Data.DataRow, String>(x => x.Field<String>(pstr_database_column_name_list[i].ToString()))
                                .ToArray();
                var source = new AutoCompleteStringCollection();
                source.AddRange(postSource);
                TextBox text_box = this.Controls.Find(pstr_text_box_name_list[i], true).FirstOrDefault() as TextBox;
                if (text_box.InvokeRequired)
                {
                    text_box.Invoke(new MethodInvoker(delegate
                    {
                        text_box.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        text_box.AutoCompleteSource = AutoCompleteSource.CustomSource;
                        text_box.AutoCompleteCustomSource = source;
                        //===============below is to check if the autocomplete textbox list run normally
                        string fullList = "";
                        foreach (string item in text_box.AutoCompleteCustomSource)
                        fullList = fullList + item + Environment.NewLine;
                        MessageBox.Show("fullList = " + fullList);
                        //===============
                    }));
                }
            }
            if (bgw_load_auto_text.CancellationPending) { e.Cancel = true; return; }
            Invoke(new Action(() => { tspb_status.Value = m; }));
    
    
    }
    
    private void bgw_load_auto_text_DoWork(object sender, DoWorkEventArgs e)
        {
            Invoke(new Action(() =>
            {
                tsb_update.Enabled = false;
                tsb_delete.Enabled = false;
                tsb_insert.Enabled = false;
                btn_search.Enabled = false;
            }));
            bgw_load_auto_text_do_work_process(1, 10, 10, e);
            bgw_load_auto_text_do_work_process(11, 17, 17, e);
            bgw_load_auto_text_do_work_process(30, 34, 22, e);
            bgw_load_auto_text_do_work_process(35, 95, 83, e); 
            //textboxes of index 44 to 105 have this crash error
            bgw_load_auto_text_do_work_process(97, 106, 100, e);
            //
        }

    Saturday, April 6, 2019 6:50 AM

All replies

  • Hi,
    is your AutoCompleteStringCollection thread-saved? You instantiate the AutoCompleteStringCollection in other thread then UI-thread.


    --
    Viele Grüsse / Best Regards
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 6, 2019 8:56 AM
  • Hello,

    Since this is a forum for VB.NET and this is a C# question I'm moving your question to the C# forum.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, April 6, 2019 9:41 AM
    Moderator
  • i have checked that its not a background thread problem ; even if i run the autocomplete process in the ui thread, the same crash happens
    • Edited by thken Saturday, April 6, 2019 11:29 AM
    Saturday, April 6, 2019 11:28 AM
  • Hi

    Thank you for posting here.

    For your question, could you provide the related definitions about the following keywords?

    pstr_database_column_name_list

    pstr_text_box_name_list

    tspb_status

    We are waiting for your update.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 8, 2019 3:13 AM
    Moderator