C# adding value/index to items in an array and keeping track in Console how often they have been clicked
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
For an assignment I'm stuck on this part. I'm as beginner a beginner programmer can be.
So I have an array attached to a listbox. This is the declaration of the array:
number = new decimal[5];
number[0] = 0.10m;
number[1] = 0.20m;
number[2] = 0.30m;
number[3] = 0.40m;
number[4] = 0.50m;
And this is how I attached the array to the listbox
lstNumbers.Items.Add(number[0]);
lstNumbers.Items.Add(number[1]);
lstNumbers.Items.Add(number[2]);
lstNumbers.Items.Add(number[3]);
lstNumbers.Items.Add(number[4]);
Since it's a listbox, these items(/values?) will be clicked on. I need to give each number their own index. Number[0] is 0.10 so index should be 010. How should I do this?
Another problem I'm having trouble solving is this: Each time you click on any of these items, it's kept track in an array and in the console. Example, if you click 0.10 once, this appears in Console: "0.10, 1 time." Click 3 times, Console should say: "0.10, 1 time. 0.10, 2 times. 0.10, 3 times."
I was told an associative array could help, but I'm not sure how this should look like. Tried searching for it, but mostly it's for PHP and the few for C# go a bit too complicated.
Here's to hoping anyone is able to help me out a little.
c#
add a comment |
For an assignment I'm stuck on this part. I'm as beginner a beginner programmer can be.
So I have an array attached to a listbox. This is the declaration of the array:
number = new decimal[5];
number[0] = 0.10m;
number[1] = 0.20m;
number[2] = 0.30m;
number[3] = 0.40m;
number[4] = 0.50m;
And this is how I attached the array to the listbox
lstNumbers.Items.Add(number[0]);
lstNumbers.Items.Add(number[1]);
lstNumbers.Items.Add(number[2]);
lstNumbers.Items.Add(number[3]);
lstNumbers.Items.Add(number[4]);
Since it's a listbox, these items(/values?) will be clicked on. I need to give each number their own index. Number[0] is 0.10 so index should be 010. How should I do this?
Another problem I'm having trouble solving is this: Each time you click on any of these items, it's kept track in an array and in the console. Example, if you click 0.10 once, this appears in Console: "0.10, 1 time." Click 3 times, Console should say: "0.10, 1 time. 0.10, 2 times. 0.10, 3 times."
I was told an associative array could help, but I'm not sure how this should look like. Tried searching for it, but mostly it's for PHP and the few for C# go a bit too complicated.
Here's to hoping anyone is able to help me out a little.
c#
1
What kind of listbox is it? Winforms? WPF?
– Robert Harvey♦
Nov 24 '18 at 16:59
1
Givingnumber[0]an index of 010 is impossible. The first item in the list box has an index of 0, the second item has an index of 1 and so on.
– preciousbetine
Nov 24 '18 at 17:18
Sorry, did not see the comments till now. Still new with the website. It's a WPF.
– Yusifer
Nov 24 '18 at 21:46
add a comment |
For an assignment I'm stuck on this part. I'm as beginner a beginner programmer can be.
So I have an array attached to a listbox. This is the declaration of the array:
number = new decimal[5];
number[0] = 0.10m;
number[1] = 0.20m;
number[2] = 0.30m;
number[3] = 0.40m;
number[4] = 0.50m;
And this is how I attached the array to the listbox
lstNumbers.Items.Add(number[0]);
lstNumbers.Items.Add(number[1]);
lstNumbers.Items.Add(number[2]);
lstNumbers.Items.Add(number[3]);
lstNumbers.Items.Add(number[4]);
Since it's a listbox, these items(/values?) will be clicked on. I need to give each number their own index. Number[0] is 0.10 so index should be 010. How should I do this?
Another problem I'm having trouble solving is this: Each time you click on any of these items, it's kept track in an array and in the console. Example, if you click 0.10 once, this appears in Console: "0.10, 1 time." Click 3 times, Console should say: "0.10, 1 time. 0.10, 2 times. 0.10, 3 times."
I was told an associative array could help, but I'm not sure how this should look like. Tried searching for it, but mostly it's for PHP and the few for C# go a bit too complicated.
Here's to hoping anyone is able to help me out a little.
c#
For an assignment I'm stuck on this part. I'm as beginner a beginner programmer can be.
So I have an array attached to a listbox. This is the declaration of the array:
number = new decimal[5];
number[0] = 0.10m;
number[1] = 0.20m;
number[2] = 0.30m;
number[3] = 0.40m;
number[4] = 0.50m;
And this is how I attached the array to the listbox
lstNumbers.Items.Add(number[0]);
lstNumbers.Items.Add(number[1]);
lstNumbers.Items.Add(number[2]);
lstNumbers.Items.Add(number[3]);
lstNumbers.Items.Add(number[4]);
Since it's a listbox, these items(/values?) will be clicked on. I need to give each number their own index. Number[0] is 0.10 so index should be 010. How should I do this?
Another problem I'm having trouble solving is this: Each time you click on any of these items, it's kept track in an array and in the console. Example, if you click 0.10 once, this appears in Console: "0.10, 1 time." Click 3 times, Console should say: "0.10, 1 time. 0.10, 2 times. 0.10, 3 times."
I was told an associative array could help, but I'm not sure how this should look like. Tried searching for it, but mostly it's for PHP and the few for C# go a bit too complicated.
Here's to hoping anyone is able to help me out a little.
c#
c#
asked Nov 24 '18 at 16:56
YusiferYusifer
172
172
1
What kind of listbox is it? Winforms? WPF?
– Robert Harvey♦
Nov 24 '18 at 16:59
1
Givingnumber[0]an index of 010 is impossible. The first item in the list box has an index of 0, the second item has an index of 1 and so on.
– preciousbetine
Nov 24 '18 at 17:18
Sorry, did not see the comments till now. Still new with the website. It's a WPF.
– Yusifer
Nov 24 '18 at 21:46
add a comment |
1
What kind of listbox is it? Winforms? WPF?
– Robert Harvey♦
Nov 24 '18 at 16:59
1
Givingnumber[0]an index of 010 is impossible. The first item in the list box has an index of 0, the second item has an index of 1 and so on.
– preciousbetine
Nov 24 '18 at 17:18
Sorry, did not see the comments till now. Still new with the website. It's a WPF.
– Yusifer
Nov 24 '18 at 21:46
1
1
What kind of listbox is it? Winforms? WPF?
– Robert Harvey♦
Nov 24 '18 at 16:59
What kind of listbox is it? Winforms? WPF?
– Robert Harvey♦
Nov 24 '18 at 16:59
1
1
Giving
number[0] an index of 010 is impossible. The first item in the list box has an index of 0, the second item has an index of 1 and so on.– preciousbetine
Nov 24 '18 at 17:18
Giving
number[0] an index of 010 is impossible. The first item in the list box has an index of 0, the second item has an index of 1 and so on.– preciousbetine
Nov 24 '18 at 17:18
Sorry, did not see the comments till now. Still new with the website. It's a WPF.
– Yusifer
Nov 24 '18 at 21:46
Sorry, did not see the comments till now. Still new with the website. It's a WPF.
– Yusifer
Nov 24 '18 at 21:46
add a comment |
2 Answers
2
active
oldest
votes
Ok lets start with the first. If you are using a windows form you can set the project to have a console output along with your windows form.
This is achieved by:
Right click your project -> Select Properties.Change the output type to Console Application.

How to have an array that uses string as index? Simple use a Dictionary.
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
number["030"] = 0.30m;
number["040"] = 0.40m;
number["050"] = 0.50m;
this.myListBox.Items.Add(number["010"]);
this.myListBox.Items.Add(number["020"]);
this.myListBox.Items.Add(number["030"]);
this.myListBox.Items.Add(number["040"]);
this.myListBox.Items.Add(number["050"]);
Last point is very straight forward. Attach a SelectIndexChanged event to your listbox and keep track of the selected item:
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var item = this.myListBox.SelectedItem.ToString();
if(!tracking.Select(x=> x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
The variable tracking has been declared as following:
public partial class Form1 : Form
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public Form1()
{
InitializeComponent();
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
EDIT
As the dictionary cannot be used and also the application is WPF here is another solution.
Again you can use the first step showed to have a WPF solution with a console output:
Right click your project -> Select Properties.Change the output type to Console Application.
To have a collection that uses an object as an index is to use an Hashtable.
Here is your main:
public partial class MainWindow : Window
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public MainWindow()
{
InitializeComponent();
Hashtable numbers = new Hashtable();
numbers.Add("010", 0.10m);
numbers.Add("020", 0.20m);
numbers.Add("030", 0.30m);
numbers.Add("040", 0.40m);
numbers.Add("050", 0.50m);
List<decimal> items = new List<decimal>();
items.Add(Convert.ToDecimal(numbers["010"]));
items.Add(Convert.ToDecimal(numbers["020"]));
items.Add(Convert.ToDecimal(numbers["030"]));
items.Add(Convert.ToDecimal(numbers["040"]));
items.Add(Convert.ToDecimal(numbers["050"]));
lbTodoList.ItemsSource = items;
}
private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = this.lbTodoList.SelectedItem.ToString();
if (!tracking.Select(x => x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
}
Your XAML should be as simple as:
<Grid>
<ListBox Name="lbTodoList" HorizontalAlignment="Left" Height="154" Margin="145,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbTodoList_SelectionChanged"/>
</Grid>
Remeber to Attach a SelectIndexChanged event to your listbox and keep track of the selected item.
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
add a comment |
Thanks for the help so far, I'm getting closer to completing my assignment. Nice since it's due to midnight today.
Got a (hopefully final) question.
So I got two listboxes right now. ListBox 1 has an enum attached. In this enum are items that have a value (different for each item). We also have the second listbox where these numbers appear in. What I'm trying to figure out:
Item1 from Listbox1 has a value of 1.70. If you click 0.50 in Listbox2 4 times, then the total value should be 2.00. When the value of the numbers is equal or greater then the value of Listbox1, a message will appear in a label and if you went over the value of Listbox1, then it will also say how many of the other numbers (in this case, 1 of 0.10 and 1 of 0.20) will be returned.
PS: Those numbers are actually coins and I'm trying to code a distribution machine (so the items are food/drinks) but I changed stuff a little to make sure I could learn from the given answers.
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
1
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460411%2fc-sharp-adding-value-index-to-items-in-an-array-and-keeping-track-in-console-how%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ok lets start with the first. If you are using a windows form you can set the project to have a console output along with your windows form.
This is achieved by:
Right click your project -> Select Properties.Change the output type to Console Application.

How to have an array that uses string as index? Simple use a Dictionary.
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
number["030"] = 0.30m;
number["040"] = 0.40m;
number["050"] = 0.50m;
this.myListBox.Items.Add(number["010"]);
this.myListBox.Items.Add(number["020"]);
this.myListBox.Items.Add(number["030"]);
this.myListBox.Items.Add(number["040"]);
this.myListBox.Items.Add(number["050"]);
Last point is very straight forward. Attach a SelectIndexChanged event to your listbox and keep track of the selected item:
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var item = this.myListBox.SelectedItem.ToString();
if(!tracking.Select(x=> x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
The variable tracking has been declared as following:
public partial class Form1 : Form
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public Form1()
{
InitializeComponent();
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
EDIT
As the dictionary cannot be used and also the application is WPF here is another solution.
Again you can use the first step showed to have a WPF solution with a console output:
Right click your project -> Select Properties.Change the output type to Console Application.
To have a collection that uses an object as an index is to use an Hashtable.
Here is your main:
public partial class MainWindow : Window
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public MainWindow()
{
InitializeComponent();
Hashtable numbers = new Hashtable();
numbers.Add("010", 0.10m);
numbers.Add("020", 0.20m);
numbers.Add("030", 0.30m);
numbers.Add("040", 0.40m);
numbers.Add("050", 0.50m);
List<decimal> items = new List<decimal>();
items.Add(Convert.ToDecimal(numbers["010"]));
items.Add(Convert.ToDecimal(numbers["020"]));
items.Add(Convert.ToDecimal(numbers["030"]));
items.Add(Convert.ToDecimal(numbers["040"]));
items.Add(Convert.ToDecimal(numbers["050"]));
lbTodoList.ItemsSource = items;
}
private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = this.lbTodoList.SelectedItem.ToString();
if (!tracking.Select(x => x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
}
Your XAML should be as simple as:
<Grid>
<ListBox Name="lbTodoList" HorizontalAlignment="Left" Height="154" Margin="145,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbTodoList_SelectionChanged"/>
</Grid>
Remeber to Attach a SelectIndexChanged event to your listbox and keep track of the selected item.
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
add a comment |
Ok lets start with the first. If you are using a windows form you can set the project to have a console output along with your windows form.
This is achieved by:
Right click your project -> Select Properties.Change the output type to Console Application.

How to have an array that uses string as index? Simple use a Dictionary.
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
number["030"] = 0.30m;
number["040"] = 0.40m;
number["050"] = 0.50m;
this.myListBox.Items.Add(number["010"]);
this.myListBox.Items.Add(number["020"]);
this.myListBox.Items.Add(number["030"]);
this.myListBox.Items.Add(number["040"]);
this.myListBox.Items.Add(number["050"]);
Last point is very straight forward. Attach a SelectIndexChanged event to your listbox and keep track of the selected item:
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var item = this.myListBox.SelectedItem.ToString();
if(!tracking.Select(x=> x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
The variable tracking has been declared as following:
public partial class Form1 : Form
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public Form1()
{
InitializeComponent();
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
EDIT
As the dictionary cannot be used and also the application is WPF here is another solution.
Again you can use the first step showed to have a WPF solution with a console output:
Right click your project -> Select Properties.Change the output type to Console Application.
To have a collection that uses an object as an index is to use an Hashtable.
Here is your main:
public partial class MainWindow : Window
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public MainWindow()
{
InitializeComponent();
Hashtable numbers = new Hashtable();
numbers.Add("010", 0.10m);
numbers.Add("020", 0.20m);
numbers.Add("030", 0.30m);
numbers.Add("040", 0.40m);
numbers.Add("050", 0.50m);
List<decimal> items = new List<decimal>();
items.Add(Convert.ToDecimal(numbers["010"]));
items.Add(Convert.ToDecimal(numbers["020"]));
items.Add(Convert.ToDecimal(numbers["030"]));
items.Add(Convert.ToDecimal(numbers["040"]));
items.Add(Convert.ToDecimal(numbers["050"]));
lbTodoList.ItemsSource = items;
}
private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = this.lbTodoList.SelectedItem.ToString();
if (!tracking.Select(x => x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
}
Your XAML should be as simple as:
<Grid>
<ListBox Name="lbTodoList" HorizontalAlignment="Left" Height="154" Margin="145,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbTodoList_SelectionChanged"/>
</Grid>
Remeber to Attach a SelectIndexChanged event to your listbox and keep track of the selected item.
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
add a comment |
Ok lets start with the first. If you are using a windows form you can set the project to have a console output along with your windows form.
This is achieved by:
Right click your project -> Select Properties.Change the output type to Console Application.

How to have an array that uses string as index? Simple use a Dictionary.
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
number["030"] = 0.30m;
number["040"] = 0.40m;
number["050"] = 0.50m;
this.myListBox.Items.Add(number["010"]);
this.myListBox.Items.Add(number["020"]);
this.myListBox.Items.Add(number["030"]);
this.myListBox.Items.Add(number["040"]);
this.myListBox.Items.Add(number["050"]);
Last point is very straight forward. Attach a SelectIndexChanged event to your listbox and keep track of the selected item:
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var item = this.myListBox.SelectedItem.ToString();
if(!tracking.Select(x=> x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
The variable tracking has been declared as following:
public partial class Form1 : Form
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public Form1()
{
InitializeComponent();
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
EDIT
As the dictionary cannot be used and also the application is WPF here is another solution.
Again you can use the first step showed to have a WPF solution with a console output:
Right click your project -> Select Properties.Change the output type to Console Application.
To have a collection that uses an object as an index is to use an Hashtable.
Here is your main:
public partial class MainWindow : Window
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public MainWindow()
{
InitializeComponent();
Hashtable numbers = new Hashtable();
numbers.Add("010", 0.10m);
numbers.Add("020", 0.20m);
numbers.Add("030", 0.30m);
numbers.Add("040", 0.40m);
numbers.Add("050", 0.50m);
List<decimal> items = new List<decimal>();
items.Add(Convert.ToDecimal(numbers["010"]));
items.Add(Convert.ToDecimal(numbers["020"]));
items.Add(Convert.ToDecimal(numbers["030"]));
items.Add(Convert.ToDecimal(numbers["040"]));
items.Add(Convert.ToDecimal(numbers["050"]));
lbTodoList.ItemsSource = items;
}
private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = this.lbTodoList.SelectedItem.ToString();
if (!tracking.Select(x => x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
}
Your XAML should be as simple as:
<Grid>
<ListBox Name="lbTodoList" HorizontalAlignment="Left" Height="154" Margin="145,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbTodoList_SelectionChanged"/>
</Grid>
Remeber to Attach a SelectIndexChanged event to your listbox and keep track of the selected item.
Ok lets start with the first. If you are using a windows form you can set the project to have a console output along with your windows form.
This is achieved by:
Right click your project -> Select Properties.Change the output type to Console Application.

How to have an array that uses string as index? Simple use a Dictionary.
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
number["030"] = 0.30m;
number["040"] = 0.40m;
number["050"] = 0.50m;
this.myListBox.Items.Add(number["010"]);
this.myListBox.Items.Add(number["020"]);
this.myListBox.Items.Add(number["030"]);
this.myListBox.Items.Add(number["040"]);
this.myListBox.Items.Add(number["050"]);
Last point is very straight forward. Attach a SelectIndexChanged event to your listbox and keep track of the selected item:
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var item = this.myListBox.SelectedItem.ToString();
if(!tracking.Select(x=> x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
The variable tracking has been declared as following:
public partial class Form1 : Form
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public Form1()
{
InitializeComponent();
Dictionary<string, decimal> number = new Dictionary<string, decimal>();
number["010"] = 0.10m;
number["020"] = 0.20m;
EDIT
As the dictionary cannot be used and also the application is WPF here is another solution.
Again you can use the first step showed to have a WPF solution with a console output:
Right click your project -> Select Properties.Change the output type to Console Application.
To have a collection that uses an object as an index is to use an Hashtable.
Here is your main:
public partial class MainWindow : Window
{
List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
public MainWindow()
{
InitializeComponent();
Hashtable numbers = new Hashtable();
numbers.Add("010", 0.10m);
numbers.Add("020", 0.20m);
numbers.Add("030", 0.30m);
numbers.Add("040", 0.40m);
numbers.Add("050", 0.50m);
List<decimal> items = new List<decimal>();
items.Add(Convert.ToDecimal(numbers["010"]));
items.Add(Convert.ToDecimal(numbers["020"]));
items.Add(Convert.ToDecimal(numbers["030"]));
items.Add(Convert.ToDecimal(numbers["040"]));
items.Add(Convert.ToDecimal(numbers["050"]));
lbTodoList.ItemsSource = items;
}
private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = this.lbTodoList.SelectedItem.ToString();
if (!tracking.Select(x => x.Key == item).Any())
{
tracking.Add(new KeyValuePair<string, int>(item, 1));
Console.WriteLine(item + " has been selected once");
}
else
{
var currentItem = tracking.SingleOrDefault(x => x.Key == item);
var value = currentItem.Value;
tracking.Remove(currentItem);
value++;
tracking.Add(new KeyValuePair<string, int>(item, value));
Console.Clear();
Console.WriteLine(item + " has been selected " + value + " times");
}
}
}
Your XAML should be as simple as:
<Grid>
<ListBox Name="lbTodoList" HorizontalAlignment="Left" Height="154" Margin="145,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbTodoList_SelectionChanged"/>
</Grid>
Remeber to Attach a SelectIndexChanged event to your listbox and keep track of the selected item.
edited Nov 24 '18 at 22:41
answered Nov 24 '18 at 18:00
Alex LeoAlex Leo
8872414
8872414
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
add a comment |
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
Thanks for the reply! It's a .wpf file. As for dictionary, we've seen it but the lector told us that dictionary (and switches) are not necessary for the program. I don't know if there's an alternative?
– Yusifer
Nov 24 '18 at 21:00
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
what about a List<KeyValuePair<string, decimal>> ? can you use that?
– Alex Leo
Nov 24 '18 at 21:09
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
That should be possible.
– Yusifer
Nov 24 '18 at 21:47
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
I think here you need an hashtable :-)
– Alex Leo
Nov 24 '18 at 22:16
add a comment |
Thanks for the help so far, I'm getting closer to completing my assignment. Nice since it's due to midnight today.
Got a (hopefully final) question.
So I got two listboxes right now. ListBox 1 has an enum attached. In this enum are items that have a value (different for each item). We also have the second listbox where these numbers appear in. What I'm trying to figure out:
Item1 from Listbox1 has a value of 1.70. If you click 0.50 in Listbox2 4 times, then the total value should be 2.00. When the value of the numbers is equal or greater then the value of Listbox1, a message will appear in a label and if you went over the value of Listbox1, then it will also say how many of the other numbers (in this case, 1 of 0.10 and 1 of 0.20) will be returned.
PS: Those numbers are actually coins and I'm trying to code a distribution machine (so the items are food/drinks) but I changed stuff a little to make sure I could learn from the given answers.
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
1
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
add a comment |
Thanks for the help so far, I'm getting closer to completing my assignment. Nice since it's due to midnight today.
Got a (hopefully final) question.
So I got two listboxes right now. ListBox 1 has an enum attached. In this enum are items that have a value (different for each item). We also have the second listbox where these numbers appear in. What I'm trying to figure out:
Item1 from Listbox1 has a value of 1.70. If you click 0.50 in Listbox2 4 times, then the total value should be 2.00. When the value of the numbers is equal or greater then the value of Listbox1, a message will appear in a label and if you went over the value of Listbox1, then it will also say how many of the other numbers (in this case, 1 of 0.10 and 1 of 0.20) will be returned.
PS: Those numbers are actually coins and I'm trying to code a distribution machine (so the items are food/drinks) but I changed stuff a little to make sure I could learn from the given answers.
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
1
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
add a comment |
Thanks for the help so far, I'm getting closer to completing my assignment. Nice since it's due to midnight today.
Got a (hopefully final) question.
So I got two listboxes right now. ListBox 1 has an enum attached. In this enum are items that have a value (different for each item). We also have the second listbox where these numbers appear in. What I'm trying to figure out:
Item1 from Listbox1 has a value of 1.70. If you click 0.50 in Listbox2 4 times, then the total value should be 2.00. When the value of the numbers is equal or greater then the value of Listbox1, a message will appear in a label and if you went over the value of Listbox1, then it will also say how many of the other numbers (in this case, 1 of 0.10 and 1 of 0.20) will be returned.
PS: Those numbers are actually coins and I'm trying to code a distribution machine (so the items are food/drinks) but I changed stuff a little to make sure I could learn from the given answers.
Thanks for the help so far, I'm getting closer to completing my assignment. Nice since it's due to midnight today.
Got a (hopefully final) question.
So I got two listboxes right now. ListBox 1 has an enum attached. In this enum are items that have a value (different for each item). We also have the second listbox where these numbers appear in. What I'm trying to figure out:
Item1 from Listbox1 has a value of 1.70. If you click 0.50 in Listbox2 4 times, then the total value should be 2.00. When the value of the numbers is equal or greater then the value of Listbox1, a message will appear in a label and if you went over the value of Listbox1, then it will also say how many of the other numbers (in this case, 1 of 0.10 and 1 of 0.20) will be returned.
PS: Those numbers are actually coins and I'm trying to code a distribution machine (so the items are food/drinks) but I changed stuff a little to make sure I could learn from the given answers.
answered Nov 25 '18 at 12:04
YusiferYusifer
172
172
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
1
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
add a comment |
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
1
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
I am glad I have been of some help. As this is another question you should really make another post. Also it would be nice if you can accept my answer.
– Alex Leo
Nov 25 '18 at 12:30
1
1
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
Accepted it! And I'll make another post, thanks for the suggestion.
– Yusifer
Nov 25 '18 at 12:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460411%2fc-sharp-adding-value-index-to-items-in-an-array-and-keeping-track-in-console-how%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What kind of listbox is it? Winforms? WPF?
– Robert Harvey♦
Nov 24 '18 at 16:59
1
Giving
number[0]an index of 010 is impossible. The first item in the list box has an index of 0, the second item has an index of 1 and so on.– preciousbetine
Nov 24 '18 at 17:18
Sorry, did not see the comments till now. Still new with the website. It's a WPF.
– Yusifer
Nov 24 '18 at 21:46