Get value from listBox using List - c#

I am using a ListBox and the data is a class:
private class Duration
{
public int Value { get; set; }
public string Label { get; set; }
}
I bind the class in this way:
var durations = new List<Duration>()
{
new Duration() { Value = 5, Label = "5 minutes" },
new Duration() { Value = 10, Label = "10 minutes" },
new Duration() { Value = 15, Label = "15 minutes" },
new Duration() { Value = 30, Label = "30 minutes" },
new Duration() { Value = 45, Label = "45 minutes" },
new Duration() { Value = 60, Label = "1 hour" },
new Duration() { Value = 90, Label = "1 hour and half" }
};
this.listTime.DataSource = durations;
this.listTime.DisplayMember = "Label";
this.listTime.ValueMember = "Value";
Everything works fine and the labels are show.
When i go to read the selected value, I am not able to recover the value of the selected item.
I was expecting to be able to do this:
int value = listTime.SelectedItems[0].Value;
or at least this:
Duration value = listTime.SelectedItems[0];
but this gives me error, what I am doing wrong? How is the right way to get the value of the selected item on the ListBox?

if (listTime.SelectedIndex != -1)
{
var item = listTime.SelectedItem as Duration;
//or as suggested by 'Stu'
var item = (Duration)listTime.SelectedItem;
MessageBox.Show(item.Value.ToString());
}

If you use Listbox this code is Ok:
listTime.Items[0].Value

Related

GroupBy bool and Select values for new list

I am trying to get into more complex Linq queries and right away catch a point I am feeling stuck. I have a following list in DB:
ID ELAPSED TIME APPISRUNNING
1 12 TRUE
2 54 TRUE
3 32 FALSE
Where ELAPSED TIME is TimeSpan and APPISRUNNING is a bool.
I would like to build a chart based on these values (https://github.com/beto-rodriguez/LiveCharts2). Chart build fine with this:
Title = "Analytics";
this.ActivityChartSeries = new ISeries[]
{
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
};
Now I somehow need to first GroupBy bool and then select a new List? I have tried following:
IEnumerable<DataRecord> dataRecords = await this.DataStore.GetItemsAsync();
this.ActivityChartSeries = dataRecords
.GroupBy(g => g.AppIsRunning)
.Select(m => new
{ // BELOW IS TOTALLY UNCLEAR FOR ME
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
})
.Select(x =>
new PieSeries<double>
{
Values = new List<double> { x.Values.FirstOrDefault() },
Name = x.Name.FirstOrDefault(),
});
Type of assigned variable:
public IEnumerable<ISeries> ActivityChartSeries
This part is totally unclear for me:
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
How after GroupBy I can create two types of data? Basically I need
"Application Running" and "Values"
"Application is not Running" and "Values"
EDIT:
Code provided by Somar Zein compiles fine:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
However as a result I am getting something like this, why it is reloading in a loop?
Here is result:
enter image description here
EDIT2:
So I have created an example for testing purposes:
Class:
public class DataModel
{
public int Id { get; set; }
public TimeSpan ElapsedTime { get; set; }
public bool AppIsRunning { get; set; }
}
Code:
List<DataModel> records = new List<DataModel>();
records.Add(new DataModel { Id = 1, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 2, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 3, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 4, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 5, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
this.ActivityChartSeries = records
.GroupBy(g => g.AppIsRunning)
.Select(item => new PieSeries<double>
{
Name = item.Key ? "Running" : "Not Running",
Values = new double[] { 2, 4 },
});
I get the same reloading effect, even thou originally provided Example from LiveCharts work fine.
you could try doing something like following:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
hope that could be helpful!

How do I properly update a list property in my database using EF Core CF

This is what I'm thinking, I want a database that creates a row for each item, each row will have a set of columns, these columns might look like this
Id(pk), ItemId, itemBuyPrice, itemSellPrice
However I also want to store a collection of datapoints which resembles a datepoint object which stores things such as long dateInSeconds (epoch time), price at this point etc.
So each item will have a list of datapoints which will be used for graphs.
The issue I'm facing is that, once every day, that collection needs to update because the API updates obviously and displays the most recent date.
How would I update that list using EF Core?
This is what I have so far, I add data like this..
try
{
ctx.Items.Add(new ItemModel
{
ItemId = 2,
Buy = 100,
Sell = 100,
Datapoints = new List<DataPoints>()
{
new DataPoints { ItemId = 2, Time = 100000, Buy = 200, Sell = 200 },
new DataPoints { ItemId = 2, Time = 100000, Buy = 200, Sell = 200 }
}
});
ctx.Items.Add(new ItemModel
{
ItemId = 3,
Buy = 100,
Sell = 100,
Datapoints = new List<DataPoints>()
{
new DataPoints { ItemId = 3, Time = 100000, Buy = 200, Sell = 200 }
}
});
ctx.Items.Add(new ItemModel
{
ItemId = 4,
Buy = 100,
Sell = 100,
Datapoints = new List<DataPoints>()
{
new DataPoints { ItemId = 4, Time = 100000, Buy = 200, Sell = 200 }
}
});
ctx.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
And then I comment that out, to try to update the datapoints for each item with this code
var dp = ctx.Items.Include(x => x.Datapoints).FirstOrDefault(x => x.ItemId == item.ItemId).Datapoints;
foreach (var datapoint in dp)
{
ctx.Datapoints.Remove(datapoint);
ctx.SaveChanges();
ctx.Items.Add(new ItemModel
{
ItemId = 2,
Buy = 100,
Sell = 100,
Datapoints = new List<DataPoints>()
{
new DataPoints { ItemId = 2, Time = 200000, Buy = 210, Sell = 210 },
new DataPoints { ItemId = 2, Time = 200000, Buy = 210, Sell = 210 }
}
});
ctx.Items.Add(new ItemModel
{
ItemId = 3,
Buy = 100,
Sell = 100,
Datapoints = new List<DataPoints>()
{
new DataPoints { ItemId = 3, Time = 200000, Buy = 210, Sell = 210 }
}
});
ctx.Items.Add(new ItemModel
{
ItemId = 4,
Buy = 100,
Sell = 100,
Datapoints = new List<DataPoints>()
{
new DataPoints { ItemId = 4, Time = 200000, Buy = 210, Sell = 210 }
}
});
ctx.SaveChanges();
}
And as of right now, it throws an exception saying that the collection has been modified and it can't continue after it changes the first item.
Here is all the code
https://hatebin.com/eeldpnbeaf
You will have to get the data from the database and update it, something like this:
//
List<Datapoints> pointsToUpdate = new List<DataPoints>()
{
new DataPoints { Time = 100000, Buy = 200, Sell = 200 },
new DataPoints { Time = 100000, Buy = 200, Sell = 200 }
}
}
// You will need the id of the item for wich you want to update the Datapoints collection and the datapoits class should have the itemId
class DataPoints
{
public int Id { get; set; }
public int Time { get; set; }
public int Buy { get; set; }
public int Sell { get; set; }
public int ItemId {get;set;}
}
// update the dataPoints with the new values
// here you need a for loop to update the values
using (MyContext ctx = new MyContext()) {
foreach (var item in pointsToUpdate)
{
// get the points
var dp = ctx.Datapoints.Where(dps => dps.ItemId == item.ItemId).ToList();
foreach (var datapoint in dp)
{
// condition to make the update (if....)
if(item.ItemId == datapoint.ItemId)
{
datapoint.Time = item.Time;
datapoint.Sell = item.Sell;
datapoint.Buy = item.Buy;
ctx.Datapoints.Update(datapoint);
}//endif
}
}//end the for here
//save the changes
ctx.SaveChanges();
}
Something like this. This should work.

How to fill picker with a list

I am updating older code but part of it must stay the same. I have now picker that needs to be filled with list.
My list
public List<TimeoutBetweenSentences> FillTimoutOptions()
{
var newListTimeoutBetweenSentenceses = new List<TimeoutBetweenSentences>()
{
new TimeoutBetweenSentences()
{
Position = 0,
Text = "+ 0 sekund",
Value = 0
},
new TimeoutBetweenSentences()
{
Position = 1,
Text = "+ 1 sekunda",
Value = 1
},
new TimeoutBetweenSentences()
{
Position = 2,
Text = "+ 2 sekundy",
Value = 2
},
new TimeoutBetweenSentences()
{
Position = 3,
Text = "+ 3 sekundy",
Value = 3
},
new TimeoutBetweenSentences()
{
Position = 4,
Text = "+ 4 sekundy",
Value = 4
},
new TimeoutBetweenSentences()
{
Position = 5,
Text = "+ 5 sekund",
Value = 5
},
};
return newListTimeoutBetweenSentenceses;
}
List<TimeoutBetweenSentences> allOptions = FillTimoutOptions();
sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString());
however this displays just as "System collections" DO zou have any idea?
this is adding an entire list as ONE element
sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString());
to add elements of one list to another, use AddRange instead
sentencePausesStepper.Items.AddRange(allOptions.Select(m => m.Text).ToList().ToString());
or better, do this
sentencePausesStepper.ItemsSource = allOptions;
sentencePausesStepper.ItemDisplayBinding = new Binding("Text");

querying a collection with a value greater than 0 while keeping the current month whether the value is 0 or greater than 0

I have this scenario where I need to select the Bar Object with Val greater than 0 while keeping the Object with the current month whether Val is 0 or greater than 0.
var currentMonth = new DateTime(2015, 3, 1); // Just an example
var foo = new List<Bar>()
{
new Bar { Date = '01/01/2015', Val = 40 },
new Bar { Date = '02/01/2025', Val = 30 },
new Bar { Date = '03/01/2015', Val = 0 },
new Bar { Date = '04/01/2015', Val = 2 },
new Bar { Date = '05/01/2015', Val = 5 }
}
// I also need to select the current month whether it's Val is 0 or greater than 0
// Stuck here
var fooResult = foo.Where(f => f.Val > 0);
So the result should be something like this:
{
new Bar = { Date = '03/01/2015', Val = 0 },
new Bar = { Date = '04/01/2015', Val = 2 },
new Bar = { Date = '05/01/2015', Val = 5 }
}
Or if currentMonth is declared as this. var currentMonth = new DateTime(2015, 4, 1);
The result should be something like this:
{
new Bar { Date = '04/01/2015', Val = 2 },
new Bar { Date = '05/01/2015', Val = 5 }
}
Any help would be much appreciated. Thanks
Try this
var fooResult = foo.Where(f => f.Val > 0 || currentMonth.Month ==Convert.ToDateTime(f.Date).Month);

C# array with a specific format

please I need to create a C# array with following exact format:
var sampledata = {
{Day: "Sunday", Quantity: 15},
{Day: "Monday", Quantity: 20},
{Day: "Tuesday", Quantity: 80}
}
can we create something like the above in C#. if yes, please how?
thanks...
var sampledata = new[] {
new { Day = "Sunday", Quantity = 15 },
new { Day = "Monday", Quantity = 20 },
new { Day = "Tuesday", Quantity = 80 }
};
try a Array or a List of anonymous Types (http://msdn.microsoft.com/en-US/en-en/library/bb397696.aspx)
Use object and array together?
class Order
{
public String Day;
public int Quantity;
}
Order[] orderArray = new Order[3];
orderArray[0].Date = "Sunday";
orderArray[0].Quantity = 15;
...

Categories

Resources