(Wpf APP College work) Need help

I have this Activity Planner made up and an part of the assignment when i click on one of the activity’s and click on the add button it goes over to the Selected Activity’s and gets removed form the All activity list and vise versa but dont know how to do this.

Not looking for a full solution( But If you show me one thats fine) but maby a few pointers on how this may be done.

 private void AddButton_Click(object sender, RoutedEventArgs e)
        {

        }

This is my code so far:

public partial class MainWindow : Window
    {
        ObservableCollection<Expensive> expenses;
        Random rng = new Random();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var e1 = new Expensive()
            {
                Category = "Travel",
                Amount = 19.95m,
                ExpenseDate = new DateTime(2019, 6, 30)

            };
            var e2 = new Expensive()
            {
                Category = "Entertainment",
                Amount = 30.47m,
                ExpenseDate = new DateTime(2015, 7, 14)

            };
            var e3 = new Expensive()
            {
                Category = "Office",
                Amount = 2.99m,
                ExpenseDate = new DateTime(2020, 2, 27)

            };

          
            // add to a list
            expenses = new ObservableCollection<Expensive>();
            

            for (int i = 0; i < 10; i++)  // Displays 10 random expenses
            {
                expenses.Add(getRandomExpensive());
            }
            


            // Display list in listbox
            LbxExpensives.ItemsSource = expenses;

           
        }

        private Expensive getRandomExpensive()
        {
            string[] categories = { "Office", "Travel", "Entertainment" };
            int randomNumber = rng.Next(0, 3);
            string category = categories[randomNumber];

            // get random amount
            randomNumber = rng.Next(1, 10001);
            decimal randomAmount = (decimal)randomNumber / 100;

            // get random date
            randomNumber = rng.Next(0, 31);
            DateTime randomDate = DateTime.Now.AddDays(-randomNumber); // give that date in the last 30 days

            Expensive e1 = new Expensive(randomAmount, randomDate, category);

            return e1;

        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void TakeawayButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }

Thanks

Activity’s what? The apostrophe indicates possession; what is possessed? Or do you mean activities (multiple)? Look at the form. It says “activities” not “activity’s”, so I will assume you mean plural, not possession.

Perhaps the following is what you are trying to say.

When I select one of the activities then click on the “>>” button it is then added to the Selected Activities and is removed from the All Activities list and vise versa.

Since this is a class assignment, it will help you best to help you solve the problem yourself. First, to ask a question, include all relevant information. You do not tell us what type of control the All Activities and Selected Activities are. So do this. Determine what type of control they are then find the relevant Microsoft documentation for the control and post a link to the (class) documentation.

1 Like

Sorry for the late reply.
I was looking and I think this is documentation

But don’t know if it’s right or not

I am not sure what question you are answering but assuming I understand then the answer is ListBox Class. The ListBox.SelectedItems Property is good for determining what item is, or what items are, selected.

Another critical question is how you are accessing the items in the ListBox. This is nearly certainly important since in WPF we nearly always bind the control (the ListBox) to something. Do you know what the ListBox is bound to? Do you understand the question?

Well I will answer that question for you. The LbxExpensives’s ItemsSource property is set to expenses, so the control is bound to expenses, which is an ObservableCollection. So look at the code used to add the items to expenses; do you see that? You can do something like that to add items to the Selected Activities.

First you need to create an ObservableCollection for the Selected Activities if it does not exist. I do not see that in your code. Then you can just add items to it when relevant. Also, look for a method to delete items from ObservableCollections; use that to remove items from the All Activities. I could look it up but it will really help for you to know how to do that. I think that those two methods are the important parts.

What else do you need help with for that assignment?

Got it to add to the second listbox but i cant seem to get the Second listbox when the remove button is hit to add the item back to the first listbox


error: Cannot convert from ‘string’ to CA2.Expensive,
‘string’ is not assignable to parameter type ’ CA2.Expensive

CA2.zip (544.3 KB)
There a link to my project if you would like to have a look

Thanks

You have LbxExpensives bound to a collection and that is good but one big problem is that you do not have SelectedActivities bound to a collection. So in other words you have:

ObservableCollection<Expensive> expenses;

And:

LbxExpensives.ItemsSource = expenses;

But you do not have something like that for SelectedActivities. You can and definitely should be consistent.

Since you are using the ObservableCollection class you do not need the BlindNewList method.

In the AddButton_Click method you are accessing the LbxExpensives and SelectedActivities controls directly. You should not do that. You should add and delete items to and from the corresponding collection; expenses for LbxExpensives and whatever collection you create for SelectedActivities.

The preceding are the major issues. It just might work if you solve them. There are other issues but hopefully they are not preventing the program from working at least somewhat.

My guess is that a prior assignment was to do this without using ObservableCollection and now the assignment is to do the same thing with ObservableCollection and you are not sure how to make the conversion. I hope you understand the preceding well enough that you are now able to continue with the assignment.

1 Like

Thanks for you help, I now have the full project working.

Wonderful. Congratulations. You did good. I helped without actually doing it for you. You deserve credit for figuring it out.

1 Like