C# UWP Undo button - c#

Ok so i created an app to manage Score's for 2 teams. APP Layout. As you can see i have Team A and Team B, the 0 below adds the points for overall score, while below that you have a history of what points where earned each round.
when you press the go button the points from the 2 Textboxes go do the addition for all the score and add the points of that round in the lists.
As you can see i created an Undo button. so if for example i press the Go button by accident i can just tap on my undo button to undo button to undo my mistake. the thing is i don't know what code to write in the click event of undo button.
private void Undo_Click(object sender, RoutedEventArgs e)
{
}
Note: my lists are binded to a class i made. so each list displays the the property it needs through observablecollection.
class List
{
public int ListA { get; set; }
public int ListB { get; set; }
}
UPDATE:
private void Undo_Click(object sender, RoutedEventArgs e)
{
var lastState = Lists.Last();
int teamAScore, teamBScore, listA, listB;
// this way i got the Active scores.
int.TryParse(CurrentScoreA.Text, out teamAScore);
int.TryParse(CurrentScoreB.Text, out teamBScore);
// this way i got the last score that i want to remove.
listA = lastState.ListA;
listB = lastState.ListB;
// here i remove the last score from the Active one.
teamAScore = teamAScore - listA;
teamBScore = teamBScore - listB;
// And here i replace my Active score with
// the new one that has the last states removed.
CurrentScoreA.Text = teamAScore.ToString();
CurrentScoreB.Text = teamBScore.ToString();
// this one removes the last states of the list
// so this way i can always remove the last part of my lsit
// from both my active score and list till i go back to 0.
Lists.Remove(lastState);
}
Thnx a lot to the 2 guys that answered my questions below and by reading them and trying to execute them i found my solution!!!! :)

Pano,
you can create a List<List<List>>(sry your class names doesn't help) that each time the user adds a score you will hold the values in a list like a state. So when you click undo you pop the Last Item from the List of List and replace to your list. Here is an example
List<List<Score>> ScoreStates = new List<List<Score>>();
List<Score> Scores = new List<Score>();
private void Undo_Click(object sender, RoutedEventArgs e)
{
var lastState = ScoreStates.Last();
ScoreStates.Remove(lastState);
Scores = lastState;
}
public class Score
{
public int TeamA { get; set; }
public int TeamB { get; set; }
}

What i meant is:
class List
{
public int ListA { get; set; }
public int ListB { get; set; }
}
Then you create 2 objects of class List one to operate on and the other to keep like a previous state (but it duplicate your objects!!!)
and the button will be:
private void Undo_Click(object sender, RoutedEventArgs e)
{
ClassListObj1.ListA = ClassListObj2.ListA;
ClassListObj1.ListB = ClassListObj2.ListB;
}
or even, but I'm not sure...
private void Undo_Click(object sender, RoutedEventArgs e)
{
ClassListObj1 = ClassListObj2;
}
Remember, you'll have to do ClassListObj2.ListA = ClassListObj1.ListA; or ClassListObj2.ListB = ClassListObj1.ListB; before modifying the lists.
The idea with the struct is better, but it requires me to know more about your app, to elaborate.

Related

Add data from a list to a ListBox C#

I have list, listbox, button and a textbox.
My idea is to make that by clicking on the button, the content of the textbox is added to the list, and then pass the data to a listbox.
My problem is, if you add what I write, but the items that are in the listbox are overwritten by the new one that you insert. and I want only more items added. to the list and to go to the listbox. Thank you very much for your answers. This is the code of my button:
private void button54_Click(object sender, RoutedEventArgs e)
{
List<Playlists> List1 = new List<Playlists>();
List1.Add(new Playlists(textBox3.Text, #rutaalbum));
lbListbox.ItemsSource = List1;
}
I am just making a demo stand in for your Playlists class.
ToString has been overridden in order to display some text in the listbox. Without it, you will only display the class name.
ObservableCollection is used instead of List, so that the listbox updates when a new item is added.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyList = new ObservableCollection<Playlists>();
MyListBox.ItemsSource = MyList;
}
private ObservableCollection<Playlists> MyList { get; }
private void Button_Click(object sender, RoutedEventArgs e)
{
MyList.Add(new Playlists(textBox3.Text));
}
}
public class Playlists
{
public Playlists(string title) { Title = title; }
public string Title { get; }
public override string ToString() { return Title; }
}
It seems , you are always creating items with new list. need to add items to existing list.
use below code. it would be useful.
private void button54_Click(object sender, RoutedEventArgs e) {
lbListBox.Items.Add(new Playlists(textBox3.Text, #rutaalbum));
}

ASP.Net using RadioButtonList / CheckBoxList

I am finishing up a program for my ASP.Net class for an ice cream shop done as a web app, the main page looks like THIS. I need two classes for the Ice Cream ordering:
IceCreamOrder
IceCreamOrderList
currently the only code in my IceCreamOrderList class is
public class IceCreamOrderList
{
List<IceCreamOrder> ICorders = new List<IceCreamOrder>();
public List<IceCreamOrder> ListofOrders { get { return ICorders; } }
public void Add(IceCreamOrder data) { ICorders.Add(data); }
and nothing in the IceCreamOrder class. My code on/around the Submit Order button IS
I've done a simpler program that was just taking text entered into a textbox and adding to a list, but my biggest problem is figuring out how to determine what control is set to true and then add them into a list. I have names for each of the controls as follows:
FlavorsList
ToppingsList
ServeList
Any help and guidance toward finishing this would be most greatly appreciated. and Thanks in advance
If you already using RadioButtonList and CheckBoxList into your order page, you should change event handler btnSubmit_Click as follows:
protected void btnSubmit_Click(object sender, EventArgs e)
{
IceCreamOrder order = new IceCreamOrder();
order.Flavor = FlavorsList.SelectedValue;
foreach (ListItem listItem in ToppingsList.Items)
{
if (listItem.Selected)
order.Toppings.Add(listItem.Value);
}
order.Serve = ServeList.SelectedValue;
// add order to order list
}
public class IceCreamOrder
{
public string Flavor { get; set; }
public List<string> Toppings { get; set; }
public string Serve { get; set; }
public IceCreamOrder()
{
Toppings = new List<string>();
}
}

Put data from text boxes to an array list and to datagridview

I'm trying to put data from text boxes in to an array list and to data grid view
private void button1_Click(object sender, EventArgs e)
{
processing pr = new processing();
pr.process = p.Text;
pr.arrivaltime = Int32.Parse(AT.Text);
pr.bursttime = Int32.Parse(BT.Text);
ArrayList Ar = new ArrayList();
Ar.Add(pr);
dataGridView1.DataSource = Ar;
}
private void BT_TextChanged(object sender, EventArgs e)
{
}
public class processing
{
public string process { set; get; }
public int arrivaltime { set; get; }
public int bursttime { set; get; }
}
The problem now is that I want to make several data stored in an array and shown in a data grid view
Instead of ArrayList use List<processing> which would be type safe.
You need to declare one class level List and then add the items on your button click. Currently you are create a new list on each button click and that is why you end up with a single item in your list.
Also consider using .Net naming conventions for your class processing. It should start with upper case P
List<processing> list = new List<processing>(); // at class level.
private void button1_Click(object sender, EventArgs e)
{
processing pr = new processing();
pr.process = p.Text;
pr.arrivaltime = Int32.Parse(AT.Text);
pr.bursttime = Int32.Parse(BT.Text);
list.Add(pr); //add item to class level list
dataGridView1.DataSource = list; //update the data source
}

populating listbox from textbox

I am trying to get the name of the Investment to appear in the listbox for stock but the code(which I used earlier in the program) doesn't seem to work.
namespace JamesClemens_FinalProject
{
public partial class Form1 : Form
{
ArrayList account;
public Form1()
{
InitializeComponent();
account = new ArrayList();
}
//here we set up our add customer button from the first tab
//when the information is filled in and the button is clicked
//the name on the account will be put in the combobox on the second tab
private void btnAddCustomer_Click(object sender, EventArgs e)
{
try
{
CustomerAccount aCustomerAccount = new CustomerAccount(txtAccountNumber.Text, txtCustomerName.Text,
txtCustomerAddress.Text, txtPhoneNumber.Text);
account.Add(aCustomerAccount);
cboClients.Items.Add(aCustomerAccount.GetCustomerName());
ClearText();
}
catch (Exception)
{
MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK);
}
}
private void ClearText()
{
txtAccountNumber.Clear();
txtCustomerName.Clear();
txtCustomerAddress.Clear();
txtPhoneNumber.Clear();
}
private void cboClients_SelectedIndexChanged(object sender, EventArgs e)
{
CustomerAccount custAccount = account[cboClients.SelectedIndex] as CustomerAccount;
if(custAccount != null)
{
txtAccountNumberTab2.Text = custAccount.GetAccountNumber();
txtCustomerNameTab2.Text = custAccount.GetCustomerName();
txtCustomerAddressTab2.Text = custAccount.GetCustomerAddress();
txtCustomerPhoneNumberTab2.Text = custAccount.GetCustomerPhoneNo();
}
}
This is the code giving me trouble, it keeps saying that "new Stock" does not contain a constructor that takes four arguments.
private void btnAddStock_Click(object sender, EventArgs e)
{
try
{
Stock aStock = new Stock(txtInvestmentID.Text, txtInvestmentName.Text, txtInvestmentSymbol.Text,
int.Parse(txtInvestmentShares.Text));
account.Add(aStock);
lstStock.Items.Add(aStock.GetInvestmentName());
ClearText();
}
catch (Exception)
{
MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK);
}
}
does anyone know why it's not letting me use the Stock class?
Here's the stock class like you asked for.
public class Stock:Investment
{
//attributes
private double stockPrice;
//constructors
public Stock()
{
}
public Stock(string anInvestmentID, string anInvestmentName, string anInvestmentSymbol,
int anInvestmentShare, CustomerAccount aCustomer, double aStockPrice)
: base(anInvestmentID, anInvestmentName, anInvestmentSymbol, anInvestmentShare)
{
SetStockPrice(aStockPrice);
}
//
public Stock(string anInvestmentID, string anInvestmentName, string anInvestmentSymbol,
int anInvestmentShare, CustomerAccount aCustomer, double aStockPrice)
{
SetInvestmentID(anInvestmentID);
SetInvestmentName(anInvestmentName);
SetInvestmentSymbol(anInvestmentSymbol);
SetInvestmentShare(anInvestmentShare);
SetStockPrice(aStockPrice);
}
//set accessors
public void SetStockPrice(double aStockPrice)
{
stockPrice = aStockPrice;
}
//get accessors
public double GetStockPrice()
{
return stockPrice;
}
Please actually read the error messages you get; it makes programming much easier.
The error message is pretty clear:
"new Stock" does not contain a constructor that takes four arguments.
You don't have a constructor in your Stock class that takes four arguments.
Look at the three constructors you posted in your Stock class. Count the number of parameters you need to pass into each constructor. Is there one that accepts four arguments only? I see one that takes no arguments, and two that each take six arguments.
Trying to understand the actual text of error messages will help you a lot in the future writing code. Most (not all, but most) error messages have meaningful content that points you to the actual problem. :)

How can I take items from a listbox and display them in a listview in another form?

I have a listbox full of items for my order.
I want to take all of the items inside my listbox and transfer them into my listview.
Then I want to take my listview and display it in another form (my messagebox).
My new listview:
private void CustomerInfo_Click(object sender, EventArgs e)
{
ListViewItem customers = new ListViewItem(fullName.Text);
customers.SubItems.Add(totalcount.ToString());
customers.SubItems.Add(total.ToString());
customers.SubItems.Add(Address.Text);
customers.SubItems.Add(telephone.Text);
for (int i = 0; i < OrderlistBox.Items.Count; i++)
{
customers.SubItems.Add(OrderlistBox.Items[i].ToString());
}
Customers.Items.Add(customers);
//CLEAR ALL FIELDS
OrderlistBox.Items.Clear();
fullName.Text = "";
Address.Text = "";
telephone.Text = "";
totalDue.Text = "";
totalItems.Text = "";
}
My contextMenuStrip, so when I click on the customer I can get its info (name, address, order, etc.):
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm
{
name = Customers.SelectedItems[0].SubItems[0].Text,
address = Customers.SelectedItems[0].SubItems[3].Text,
telephone = Customers.SelectedItems[0].SubItems[4].Text,
};
myformmessagedialog.ShowDialog();
}
}
My new form, the messagebox where I will display all the info for the client:
public partial class MessageBoxForm : Form
{
public MessageBoxForm()
{
InitializeComponent();
}
public string name;
public string address;
public string telephone;
public ListViewItem order = new ListViewItem();
private void MessageBoxForm_Load(object sender, EventArgs e)
{
lblName.Text = name;
lbladdress.Text = address;
lbltelephone.Text = telephone;
orderListView.Items.Add(order);
}
}
I'm sorry if this seems confusing but I'm just looking for help to go in the right direction. Any help is appreciated.
One way to do this is to put the data that you want to display in some sort of ViewModel, basically a class or set of classes that has the data that you want to display. Then the main form can display it, and you can pass a reference to that ViewModel to the message box and it can display it as well.
In general you want to avoid any kind of code that directly ties controls from different forms together.
The easiest way based on your current setup is to simply pass your list view data across to your MessageBoxForm e.g.
public partial class MessageBoxForm : Form
{
...
public void LoadListView(ListViewItemCollection items)
{
orderListView.Clear();
orderListView.AddRange(items);
}
}
....
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm
{
name = Customers.SelectedItems[0].SubItems[0].Text,
address = Customers.SelectedItems[0].SubItems[3].Text,
telephone = Customers.SelectedItems[0].SubItems[4].Text,
};
myformmessagedialog.LoadListView(Customers.Items);
myformmessagedialog.ShowDialog();
}
}
Basic answer is you don't.
You maintain a collection of items (whatever they are).
You display them in a list box.
You display them in a list view.
If you want say select some from the list box and only move them to the list view.
Then you use the listbox selection to find them in your collections of items, create a list of selected ones then passs that to the form with the listview to display.
Don't use UI controls to store your data and try really hard to never make one form's UI directly dependant on another.
I'm guessing what you'd need (and I could have misunderstood what you are looking for) is a new method in you MessageBoxForm to pass in your Customers object:
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm;
myformmessagedialog.Customers = Customers;
if (myformmessagedialog.ShowDialog() == DialogResult.OK)
{
Customers = myformmessagedialog.Customers;
}
}
}
If so, simply modify your class to be something like this:
public partial class MessageBoxForm : Form
{
public MessageBoxForm()
{
InitializeComponent();
}
private void MessageBoxForm_Load(object sender, EventArgs e)
{
if (Customers != null)
{
// add your code here to add your Customers as needed
}
}
public Customers Customers { get; set; }
}
To access anything from the parent form you need to pass it to the child form so
myformmessagedialog.ShowDialog();
becomes
myformmessagedialog dialog = new myformmessagedialg(this);
dialog.ShowDialog();
and your class constructor becomes this:
public MessageBoxForm(myformmessagedialog parent){
name=parent.fullName.Text;
address=parent.address.Text;
...etc...
InitializeComponent();
}
Though it might be better to just pass in the name, address, etc rather than the whole form, this way is nice for while you are changing things because you have one less place to change to add another variable to pass.

Categories