Question: In C# I am trying to display data from two listboxes in a new form. How do I combine both If in the private void

In C# I am trying to display data from two listboxes in a new form.

How do I combine both If in the private void calculateButton_Click(object sender, EventArgs e)

so it whould only open 1 new form with the data from both listboxes listed?

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace DormAndMealPlanCalculator { public partial class MainForm : Form { List LocationList; List MealList; public MainForm() { InitializeComponent();

Dorm locationInfo;

LocationList = new List();

LocationList.Add(locationInfo = new Dorm("Allen Hall", 1500)); dormListBox.Items.Add(locationInfo.Location);

LocationList.Add(locationInfo = new Dorm("Pike Hall", 1600)); dormListBox.Items.Add(locationInfo.Location);

LocationList.Add(locationInfo = new Dorm("Farthing Hall", 1800)); dormListBox.Items.Add(locationInfo.Location);

LocationList.Add(locationInfo = new Dorm("University Suites", 2500)); dormListBox.Items.Add(locationInfo.Location);

Meal mealInfo;

MealList = new List();

MealList.Add(mealInfo = new Meal("7 meals per week", 600)); mealListBox.Items.Add(mealInfo.MealOptions);

MealList.Add(mealInfo = new Meal("14 meals per week", 1200)); mealListBox.Items.Add(mealInfo.MealOptions);

MealList.Add(mealInfo = new Meal("Unlimited meals", 1700)); mealListBox.Items.Add(mealInfo.MealOptions);

}

private void calculateButton_Click(object sender, EventArgs e) { if (dormListBox.SelectedIndex != -1) { var viewInfo = new TotalChargesForm(LocationList[dormListBox.SelectedIndex]).ShowDialog(); } if (mealListBox.SelectedIndex != -1) { var viewInfo = new TotalChargesForm(MealList[mealListBox.SelectedIndex]).ShowDialog(); } }

private void exitButton_Click(object sender, EventArgs e) { // Close the form. this.Close(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!