alkhanate458
alkhanate458

Reputation: 31

How to make a textbox receive changes from comboboxes and another textbox?

Essentially I am taking the inputs from these ComboBoxes shown below: enter image description here

Along with the changes from this textbox: enter image description here

And place both of these changes towards another, sole textbox. For instance, if I have my file name format adjusted the following way from the first screenshot, and I type in my client/company name as "apples", the textbox for "File Name" should output to this:

enter image description here

I want the user to type in their preferred "client name" and make my program add those changes automatically, without compromising the values/inputs from the ComboBoxes and "Client Name". I tried to look online for something like this, but the solutions provided made very little sense, or were just too confusing for me to understand. Any help will be greatly appreciated!

Upvotes: 0

Views: 53

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

This is fairly broad; there are many ways it could be solved but I think I'd make it fairly simple:

I'd have the comboboxes in a list in left to right order:

Dim combos = { combobox1, combobox2, combobox3, combobox4, combobox5, combobox6 }

I'd have the replacements in a dictionary in any order, so this Dictionary is essentially a list of KeyValuePairs, the Key is what we find, and the Value is what we replace it with:

Dim repl = New Dictionary(Of String, String) From _
{ _
   {"Client Name", _companyClientName.Text}, _
   {"Month", DateTime.Now.ToString("MMM")}, _
   {"Year", DateTime.Now.ToString("yyyy")}, _
   {"Please Select", ""} _
} 

And perform a set of replacements in a loop:

filenameTextBox.Clear()

For Each c as ComboBox in combos
    'to track if we perform any replacement 
    Dim changed = False

    'for each r in the list of replacements
    For Each r as KeyValuePair(Of String, String) in repl

        'if the text in the combo is something we replace
        If c.Text = r.Key Then

             'append a replacement-performed version
            fileNameTextBox.AppendText(c.Text.Replace(r.Key, r.Value))
            changed = True 'track that we made a change
            Exit For 'don't make any more replacements
        End If
    End For

    'if we didn't change anything, just put the text of the combo in literally
    If Not changed Then fileNameTextBox.AppendText(c.Text)

End For
    
    

All this code would go in a method and then event handlers for "combo selected item changed" and/or "company name text box text chnaged" would call the method

Upvotes: 1

Related Questions