Autofill textbox based on the one textbox value and output it another textbox

beingsultanak

New member
Joined
Aug 18, 2020
Messages
1
Programming Experience
Beginner
In the load event I managed to set the autocomplete source of Part Number txtBox. I want to auto fill Description text box based on the value of Part Number textbox. How can i do?
C#:
try
            {
                string query = "select [itmPartNumber],[itemDescr] from [vstItems]";
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                AutoCompleteStringCollection autoPart = new AutoCompleteStringCollection();
                while (sdr.Read())
                {
                    autoPart.Add(sdr.GetString(0));
                } sdr.Close();
                txtPartNum.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtPartNum.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtPartNum.AutoCompleteCustomSource = autoPart;
            }
a.jpg
 
Last edited by a moderator:
Welcome to the forum.

In the future, please put your code in code tags.
 
Well the first question is: Is that first text box really meant to be a textbox, or should it be a (fixed selection) combobox? Is the user allowed to type in an item part number that is not found in the database?

If it is meant to be a combobox, then the common old style WinForms approach is to add items to the combobox such that their display string is the part number, and the tag value of the list item is item description. You then listen to combobox selection changes. Whenever it changes you also set the text value in the other textbox with the value stashed in the tag value.

If it is meant to be a textbox, then it gets a bit harder because there is no unmanaged event that gets fired when the user makes a selection. You'll either have to wait for the text changed event and perform a lookup for a value; or you will have to delve into the Win32 API to get the selection changed notification from the autocomplete common control.

As an aside, since this looks to be brand new code, why are you writing this in WinForms? WinForms is at end-of-life. I would highly recommend doing WPF or Xamarin or UWP instead.
 
My question is why are you dumping code into the load event? Generally this is how errors get swallowed, and its generally not a good design to be using your UI as your model. What will happen if your SQL server or database becomes unresponsive? At some point, it's going to happen.

Your UI should be kept completely separate to the logic doing all the work. You also should have any data processing completed on an independent thread or a background worker; regardless how small that piece of data my be. There really is no excuse for implementing such a poor design model.

I would suggest you make the suggested and needed changes above before going ahead with addressing the current problems you've reported. And I would also advocate completing your project in WPF.
 

Latest posts

Back
Top Bottom