Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a simple GridView with several names:

- Alex
- Thomas
- Kate
- George

I want to update a TextBox after a selection in the GridView.

If have this code:

Name = GridView1.SelectedRow.Cells(0).Text

NameSelection.Text = Name

The NameSelection.Text (TextBox) shows Alex when I click on Alex. But after I click at Thomas, the name Alex disappears. Is it possible to update the TextBox and keep the old values after each click?

For example the TextBox need to show: AlexThomas after the user click on Alex and after that on Thomas.

Sorry for my bad English.

Thanks in advance.

What I have tried:

Search on Microsoft website and Google
Posted
Updated 4-Sep-20 0:51am
v2
Comments
CHill60 4-Sep-20 6:27am    
Do you mean the textbox should have "Alex" first then "AlexThomas" afterwards?
Coder4EU 4-Sep-20 6:46am    
Hello, yes that is exactly what I mean. Maybe I need store the names in a list?
CHill60 4-Sep-20 6:53am    
Have a look at the solution I have literally just posted. If it's not quite what you need then let me know and we can have another look. Your idea of listbox instead of a textbox might actually be a better solution

1 solution

If you just want to add the name onto the end of the textbox then it is as simple as
VB
NameSelection.Text &= Name
Note the "&" in front of the "=". See &= Operator - Visual Basic | Microsoft Docs[^]
That will give the results
Alex
then
AlexThomas
To make it a little nicer try setting the Multiline Property[^] of your text box, and the scrollbars properties (same link) and use
VB
NameSelection.Text &= Environment.NewLine & Name
which will give results
Alex
Thomas
Note the extra line which you will need to trim [^] out

Note that strings are immutable, which means you cannot change them. You actually get a new string created each time. That can be quite wasteful if you are doing this a lot. In that instance have a look at the StringBuilder Class (System.Text) | Microsoft Docs[^] to build the new string(s) - probably not needed in this case, just letting you know.
 
Share this answer
 
Comments
Coder4EU 4-Sep-20 7:26am    
Thank you. The &= is the solution.
CHill60 4-Sep-20 7:28am    
My pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900