I am new to C#, and am trying to create a basic project in which the user enters Client details (name, phone, email) and selects their location by a radio button as one out of three locations. The locations are radio buttons within the menu item, 'MenuLocClient'.
Then the user clicks 'Add Client' to see the Client's name to appear in the list box,'LstClients'. When the client name is selected from the list box, and the user clicks 'Show Client' the window, Client details is brought up with the client information (name, phone, email, location) like so:
I have my client class defined as:
namespace WpfApp_project
{
public partial class Client
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Location { get; }
public Client(string n, string p, string e, string l)
{
Name = n;
Phone = p;
Email = e;
Location = l;
}
}
}
In my ClientDetails window:
public partial class ClientDetails : Window
{
public ClientDetails(Client c)
{
InitializeComponent();
LblNameClient.Content = c.Name;
lblPhoneClient.Content = c.Phone;
lblEmailClient.Content = c.Email;
blLocClient.Content = c.Location;
}
}
In my main Window:
public partial class MainWindow : Window
{
// My client list
List<Client> ClientList = new List<Client>();
public MainWindow()
{
InitializeComponent();
}
// method to select location via radio button. it would be something like this?
public void radio_selected(…)
{
string selected;
if (RBLocE.isChecked) { selected = “Edinburgh”};
else if (RBLocG.isChecked) { selected = “Glasgow”};
else if (RBLocO.isChecked) { selected = “Other”};
Client c = …
c.Location = selected;
}
// method to create a new client on click
private void newClient(object sender, RoutedEventArgs e)
{
Client c = new Client(boxClientName.Text, boxClientPhone.Text, boxClientEmail.Text, ...); # add selected for location?
BoxClientName.Clear();
boxClientPhone.Clear();
boxClientEmail.Clear();
#rb.Checked = false;?
ClientList.Add(c);
lstClients.ItemsSource = null;
lstClients.ItemsSource = ClientList;
}
// method to show details of selected client
private void showDetails(object sender, RoutedEventArgs e)
{
Client c = lstClients.SelectedItem as Client;
if (c != null)
{
ClientDetails w = new ClientDetails(c);
w.Show();
}
}
Can someone please help me how to define my methods in MainWindow to add Location as selected by a radio button?
CodePudding user response:
There are many ways to do this. Normally I would bind to an enum with a value converter as shown in this answer.
However as you are not using binding at all I'm going to assume you are at an earlier stage of learning and this can be done with a simple if/else in code behind. In fact you mostly already did this in your radio_selected method, you just need to modify it slightly to return the value.
public string GetSelectedLocation()
{
string selected = string.Empty;
if (RBLocE.isChecked) { selected = “Edinburgh”};
else if (RBLocG.isChecked) { selected = “Glasgow”};
else if (RBLocO.isChecked) { selected = “Other”};
return selected;
}
Then when creating your new client:
Client c = new Client(boxClientName.Text, boxClientPhone.Text,
boxClientEmail.Text, GetSelectedLocation());