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

I'am Writting a Code in C#, WPF. My Program is to Add ComboBox Selected Item and Textbox Value, When i Click ADD Button Both ComboBox and Textbox value should be added to listBox in Same Row.

How i Write a code for this ??.


What I have tried:

I have 1 ComboBox to Select Country Code, And 1 Textbox to Enter Mobile Number According to Selected Country Code. and One ADD Button To add Both [ComboBox and TextBox] value to ListBox
Updated 9-Dec-19 22:27pm

It is assumed here that your ListBox is called

your TextBox is called

and your ComboBox is called
containing values like this

then you could insert the complete number like this:
var completePhoneNumber = $"{ComboBoxCountryCodes.SelectedItem} {TextBoxPhoneNumber.Text}";


See also $ - string interpolation - C# reference | Microsoft Docs[^]
Share this answer
Member 14672509 10-Dec-19 4:19am    
This code was not working.. It shows Error messages as below :
Error 1 Unexpected character '$'
Error 2 Invalid expression term ''
Error 3 ; expected
Error 4 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
F-ES Sitecore 10-Dec-19 4:32am    
var completePhoneNumber = string.Format("{0} {1}", ComboBoxCountryCodes.SelectedItem, TextBoxPhoneNumber.Text");
TheRealSteveJudge 10-Dec-19 4:50am    
I tested the code and it works. Maybe he is using an old version of C#.
F-ES Sitecore 10-Dec-19 4:59am    
Yeah $ is fairly new notation. TBH though if you need someone to tell you how to concatenate strings together then you need to be spending time learning the basics!
TheRealSteveJudge 10-Dec-19 4:36am    
Maybe you are using an old version of C#.
Microsoft Docs says: "This feature is available starting with C# 6."
Then you should use what F-ES Sitecore suggested.
if you are using MVVM pattern in your code , it is going to be very easy for you to manage this scenrio.
<combobox itemssource="{Binding Items}" selectedvalue="{Binding SelectedItem, Mode=TwoWay}">
same for textbox
<textbox binding="{Binding Name}">

MainViewModel which is your data context of your control.
which contains Items type of ObservableCollection
property SelectedItem, Name
and EnterCommand type of Icommand.

on execute method of EnterCommand delegate, you get the current selected value and name from text box.

I hope that help you to start with
Share this answer

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