Set ListBox item data format c# - c#

Sorry if the title make you feel confusing because of my bad english.
I have a ListBox which contain many items which have time format
(For example: 00:02:22:33)
I want to convert this time format into minutes
For example: 00:02:22:33 -> 02 hours = 120 minutes
33 seconds = 33/60 = 0.55 minutes
So result is 120+22+0.55 = 142.55
What i'm trying is writing a method like:
public static void Timeconvert(ListBox l)
{
foreach (var item in l.Items)
{
int x, int y, int z; //It just to show you my thought
if(item.format = 00:x:y:z)
{
int result = x*60 +y + z/60 ;
item = result.Tostring();
}
}
}
I'm new to C# so i explained as detail as i could, so please help me :(

Just parse the string as span of time and use TotalMinutes property.
var time = TimeSpan.Parse("00:02:22:33");
var convertedToMinutes = time.TotalMinutes; //Returns 142.55
This will update your list items
for (int i = 0; i < listBox1.Items.Count; i++)
{
TimeSpan time = TimeSpan.Parse(listBox1.Items[i].ToString());
listBox1.Items[i] = time.TotalMinutes;
}
Alternately, TryParse() can be used for handling strings in an incorrect format: if (TimeSpan.TryParse(listBox1.Items[i].ToString(), out time)) { listBox1.Items[i] = time.TotalMinutes; }

You may try following:
var regex = new System.Text.RegularExpressions.Regex(#"00:\d{2}:\d{2}:\d{2}");
foreach (var item in l.Items)
{
if (regex.IsMatch(item))
{
item = TimeSpan.Parse(item).TotalMinutes.ToString();
}
}

Related

how to find average with strings c#

I need to set a variable as the average of 3 other variables, which are numbers but they are set as strings. How do I do this? I'm using c#, visual studio, windows forms.
The variable i'm trying to set is called skiTime, the variables i'm using to get the average are called skiTime1, skiTime2 and skiTime3.
basically i need the c# version of: skiTime = (skiTime1 + skiTime2 + skiTime3) / 3
The code where I start (declare? I don't know the word to use) the variables
List<string> skiTime1 = new List<string>();
List<string> skiTime2 = new List<string>();
List<string> skiTime3 = new List<string>();
string skiTime
The code where i set the value for the variables:
using (StreamReader sr = new StreamReader("pupilSkiTimes.txt"))
{
string line = "";
while ((line = sr.ReadLine()) != null)
{
string[] components = line.Split("~".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
skiTime1.Add(components[2]);
skiTime2.Add(components[3]);
skiTime3.Add(components[4]);
}
sr.Close();
}
I need to display skiTime1, skiTime2 and skiTime3 in a data grid view, so i think they need to be strings, if i'm not mistaken. skiTime will only be used in another calculation so maybe it can be turned into an int. I don't really know what i'm doing and only got this far because of tutorials, help.
I can post the whole code if this question is too confusing or doesn't have enough information.
public string CalculateAverage(List<string> skiTime1, List<string> skiTime2, List<string> skiTime3)
{
List<string> allValues = new List<string>();
allValues.AddRange(skiTime1);
allValues.AddRange(skiTime2);
allValues.AddRange(skiTime3);
float totalcount = 0;
float average = 0;
foreach (var value in allValues)
{
totalcount = totalcount + float.Parse(value);
}
average = totalcount / allValues.Count();
return average.ToString();
}
Function for returning the average value
Now call the function where u need like:
string skiTime = CalculateAverage(skiTime1, skiTime2, skiTime3);
You need to parse the strings to decimals then calculate the average:
List<decimal> avgTime = new List<decimal>();
for (var i = 0; i < skiTime1.Length; i++) {
var avg = (decimal.Parse(skiTime1[i]) + decimal.Parse(skiTime2[i]) + decimal.Parse(skiTime3[i])) / 3;
avgTime.Add(avg);
}

Adding a new line in "string.join()" when formatting arrays?

Hello I am a newbie programmer, I am trying to format arrays in a way where there is a line break at the end of a specific set of arrays. I currently have 4 separate arrays in which I want to arrange each item in the array to a specific pattern. I have accomplished this task but now I am stumped because they are all in one line. Let me give you an example: (I am doing this on a datagridview by the way)
(This is what I want to happen)
Binder Clips Small pc 12 1260
Selleys All Clear pc 12 2400
(This is what I am getting)
Binder Clips Small pc 12 1260 Selleys All Clear pc 12 2400
This is my code:
//I get these from a datagridview
var items = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[1].Value.ToString().Trim())
.ToArray();
var units = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[2].Value.ToString().Trim())
.ToArray();
var quantity = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[6].Value.ToString().Trim())
.ToArray();
var prices = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[8].Value.ToString().Trim())
.ToArray();
//this is what I use to sort out the pattern that I want the arrays to be in
string[] concat = new string[items.Length * 4];
int index = 0;
for (int i = 0; i < items.Length; i++)
{
concat[index++] = items[i];
concat[index++] = units[i];
concat[index++] = quantity[i];
concat[index++] = prices[i];
}
// and this is where I am stuck because I can just put \n, it would ruin the format even more
cartitems.Text = string.Join(" ", concat);
I also tried doing something like this:
int j = 0;
string str = "";
foreach (var item in concat)
{
str += item;
if (j <= concat.Length - 1)
{
if (j % 3 == 0)
str += " ";
else
str += "\n";
}
j++;
}
It kinda gets the job done but the line breaks are all over the place.
This is what my projects look like so you can get a better gist on where am I getting the data from the 4 arrays:
basically the product name, unit, quantity and line total
and lastly I am storing in on a label so I can see how it the formatting looks like:
that about sums up my problem, I really hope you can help a newbie like me out, I have a feeling the answer is quite simple and I am just un-experienced.
As a general rule, you should keep your data structures (how the data is stored, implemented here as an array of values) separate from the data representation (in this case, written to a list box).
C# is an object-oriented language, so we might as well take advantage of that, right?
Create a class for your items.
class Item
{
public string Name { get; set; }
public string Unit { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
override public string ToString() {
return $"{Name} {Unit} {Quantity} {Price}";
}
}
This is how you load your array.
Item[] concat = new Item[items.Length];
int index = 0;
for (int i = 0; i < items.Length; i++) {
concat[index++] = new Item {
Name = items[i],
Unit = units[i],
Quantity = quantity[i],
Price = prices[i]
};
}
and this is how you can add the list of items to a listbox.
foreach(Item item in concat) {
listBox.Items.Add(item);
}

Get col[0] values while col[1] is equal to previous col[1] iteration

I'm using a rate sheet from the US fed reserve site, which contains two cols
[0] == ShortDate (ex "1/1/1962")
[1] == Rate for that date in [0]
So the file looks like: 1/1/1962,3.8
Each time this utility runs, it gets/opens the file, removes all data for dates On and Before 5/31/1997. It then writes to a stream (in my case, another csv file) with the following headers: rowid,beginningdate,expirationdate,rate
The problem I have is, part of the requirement is that if the rate (col [1]) has the same value for some consecutive days, then don't list those days specifically. Instead, write the date of the first consecutive rate, then write the last date before the rate changes again.
My problem/question is:
How do I check against each iterated col[1] value before and after during a loop so I can write correct the beginning and expiring dates? I was hoping to bump the index of a value by one so I could get the values in the next row, but having a hard time doing that even.
I've tried my research at using as many Linq-based queries I could but I can't find anything that works and just need another pair of eyes / another idea.
Here is what I'm doing so far:
int count=0;
var rateContents = File.ReadAllLines(resFile);
List<DateTime> lstDT = new List<DateTime>();
List<string> lstRate = new List<string>();
List<string> d = new List<string>();
foreach (var x in rateContents)
{
lstDT.Add(Convert.ToDateTime(x.Split(',')[0]));
lstRate.Add(x.Split(',')[1]);
}
int recordsOn_and_after6_1_97 = lstDT.Where(z => Convert.ToDateTime(z) >= Convert.ToDateTime("6/1/1997")).Count();
int recordsbefore6_1_97 = lstDT.Where(z => Convert.ToDateTime(z) < Convert.ToDateTime("6/1/1997")).Count();
lstRate.RemoveRange(0, recordsbefore6_1_97);
lstDT.RemoveRange(0, recordsbefore6_1_97);
using (StreamWriter sw = new StreamWriter(desktoppath + "somefile.csv"))
{
while (count <= (recordsOn_and_after6_1_97 - 1))
{
//sw.WriteLine("Index Number,Effective Date,Expiration Date,Rate");
d.Add(Convert.ToDateTime(lstDT[count].ToShortDateString()) + "," + lstRate[count]);
count++;
}
}
count = 0;
/*Below is where I am stuck. Realistically I think I'd want to do something like: var variable = argh.val[count+1]; or something, but I can't get that to work either. I'm missing something simple at this point.*/
foreach (var argh in d.Select((val, ind) => new { Value = val, Index = ind}))
{
//var curdt = Convert.ToDateTime(argh.Split(',')[0]).ToShortDateString();
//var currate = argh.Split(',')[1].ToString();
var curdt = argh.Value.Split(',')[0];
var currate = argh.Value.Split(',')[1];
var x = currate.CompareTo(currate[count + 1].ToString()).ToString() ;
Console.WriteLine(x.ToString());
//Console.WriteLine("val +1: " + curdt + " --- rate: " + currate.TakeWhile(z => z <= 10).ToString());
count++;
//Console.WriteLine(prevcurdt +" --- "+ currate);
}
Please be gentle, I'm still learning new things dang near daily :-)
Thank you!
Here is my idea to filter the rates as you want them. Basically just a simple for loop which looks at the rates either side of the current one - if the next rate isn't the same, we know the current date is the expiration date of whatever entry we're currently on. If the last rate isn't the same, we know it's a new entry.
public class RateEntry
{
public DateTime StartDate { get; set; }
public DateTime ExpirationDate { get; set; }
public decimal Rate { get; set; }
}
class Program
{
const string DATE_FORMAT_IN = "yyyy-MM-dd";
const string DATE_FORMAT_OUT = "yyyy-MM-dd";
static void Main()
{
var inputRateDataRaw = File.ReadAllLines(#"c:\temp\RATES_IN.csv");
DateTime startDate = new DateTime(1997, 05, 31);
// parse the input dates and rates
var rateDataFiltered = inputRateDataRaw
.Select(rateData =>
{
var dataComponents = rateData.Split(',');
DateTime rateDate = DateTime.ParseExact(dataComponents[0], DATE_FORMAT_IN, null);
decimal rate = decimal.Parse(dataComponents[1]);
return new RateEntry() { StartDate = rateDate, Rate = rate };
})
.Where(a => a.StartDate > startDate)
.OrderBy(a => a.StartDate)
.ToList();
List<RateEntry> rateRanges = new List<RateEntry>();
for (int i = 0; i < rateDataFiltered.Count; i++)
{
RateEntry next = ((i + 1) == rateDataFiltered.Count) ? null : rateDataFiltered[i + 1];
RateEntry last = (i == 0) ? null : rateDataFiltered[i - 1];
RateEntry now = rateDataFiltered[i];
// if this is the first rate, or if the last rate isn't this rate, this is a new entry.
if (last == null || last.Rate != now.Rate)
rateRanges.Add(now);
// if the next rate isn't this one, then the current entry expiration is this one's start date.
if (next == null || next.Rate != now.Rate)
rateRanges.Last().ExpirationDate = now.StartDate;
}
// write the data out
using (StreamWriter writer = new StreamWriter(#"c:\temp\RATES_OUT.csv"))
{
writer.WriteLine("ROWID;STARTDATE;EXPIRATIONDATE;RATE");
for (int i = 0; i < rateRanges.Count; i++)
{
RateEntry rateEntry = rateRanges[i];
writer.WriteLine($"{i};{rateEntry.StartDate.ToString(DATE_FORMAT_OUT)};{rateEntry.ExpirationDate.ToString(DATE_FORMAT_OUT)};{rateEntry.Rate}");
}
}
Console.ReadKey();
}
};

Why the checking condition in the inner loop never true?

for (int i = 0; i < ExtractLinks.FilteredLinks.Count; i++)
{
for (int x = 0; x < lockedLinks.Count; x++)
{
if (ExtractLinks.FilteredLinks[i].Contains(lockedLinks[x]))
{
string h = "found";
}
}
}
I want to check if the same link exist in both Lists.
FilteredLinks and lockedLinks both type List
I used a break point and in the FilteredLink i saw this link for example:
http://rotter.net/forum/scoops1/112341.shtml
And in lockedLinks this link:
http://rotter.net/cgi-bin/forum/dcboard.cgi?az=read_count&om=112341&forum=scoops1
Both links lead to the same page. So that's mean both are the same.
So in this case it should stop in the break point i added on : string h = "found";
But since it's never stop there i guess something is wrong with my IF condition.
In case it was found i want to remove the link from the FilteredLinks and also from the lockedLinks to remove what suppose to be the same links from both lists. The links are not identical in the address but they are leading to same page so i consider them as the same/identical and they both should be removed.
If you know the uri format then you can extract the id from the different uri types and compare those:
private static string GetArticleIdFiltered(string filtered)
{
var uri = new Uri(filtered);
return Path.GetFileNameWithoutExtension(uri.LocalPath);
}
private static string GetArticleIdLocked(string locked)
{
var uri = new Uri(locked);
var queryParams = HttpUtility.ParseQueryString(uri.Query);
return queryParams["om"];
}
for (int x = 0; x < lockedLinks.Count; x++)
{
var lockedArticle = GetArticleIdLocked(lockedLinks[x]);
var filteredId = GetArticleIdFiltered(ExtractLinks.FilteredLinks[i]);
if (lockedArticle == filteredId)
{
string h = "found";
}
}
If you know the id is always numeric you can parse it more accurately and compare ints instead of strings.

What is the best way to code up a Month and Year drop down list for ASP.NET?

I have an internal application that I needs to have a drop down list for two date type elements: Month and Year. These values are not in a database or other repository of information.
I know I could just setup a list with the values I need by adding them to a dictionary like object (I need to correlate the Month to the numerical representation, January => 01):
var months = new Dictionary<String,String>();
months.Add("01", "January");
...
The drop down list for the year will be a bit easier as I can just choose a starting year and iterate up to the current or current+1 year in a generic list.
Is there a better way to handle these data elements? Something built in, or a good design pattern that I should be implementing?
You could use this to get a list of all the Month names and loop through it.
CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
You can use it like this...using the index of the Month as the value for your dropdown
var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
for (int i = 0; i < months.Length; i++)
{
ddl.Items.Add(new ListItem(months[i], i.ToString()));
}
Extending #Jesse Brown's answer...
With a using System.Globalization directive, I have the following code:
for (int x = 0; x < 12; x++)
{
cboMonth.Items.Add
(
(x+1).ToString("00")
+ " "
+ CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(x)
);
}
This produces a dropdown list that looks like:
01 January
02 February
03 March
...
12 December
A further refinement might be to make the displayed month the current month by adding:
cboMonth.Text = DateTime.Now.Month.ToString("00")
+ " "
+ CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(DateTime.Now.Month);
After the for loop.
Here is my solution, which is very similar to #jesse-brown's solution (the accepted answer)
VB.NET:
In a global functions class:
Public Shared Function GetMonthList() As Generic.Dictionary(Of String, String)
Dim months As New Generic.Dictionary(Of String, String)()
For m As Int32 = 1 To 12
months.Add(String.Format("{0:0#}", m), CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m))
Next
Return months
End Function
On the ASPX page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ddMonth.DataSource = GlobalFunctions.GetMonthList()
ddMonth.DataValueField = "Key"
ddMonth.DataTextField = "Value"
ddMonth.DataBind()
End Sub
This implementation is in VB.NET because that happens to be what this webapp is using (legacy), however thank you very much for the examples in C# (my preferred language), I'm posting the VB.NET here to help the VB.NET community as well.
Below code is for load month dropdown as Select
private void LoadMonth()
{
ddlmonth.Items.Add(new ListItem("Select", 0.ToString()));
var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
for (int i = 0; i < months.Length-1; i++)
{
ddlmonth.Items.Add(new ListItem(months[i], (i+1).ToString()));
}
}
For ASP.NET MVC this is what I'm doing.
Note I prefer to use a codebehind for things like this - its still part of the view and there's nothing wrong with the view constructing a SelectList.
PaymentControl.ascx
<%= Html.DropDownList("ExpirationMonth", ExpirationMonthDropdown)%> /
<%= Html.DropDownList("ExpirationYear", ExpirationYearDropdown)%>
PaymentControl.ascx.cs
public partial class PaymentControl : ViewUserControl<CheckoutModel>
{
public IEnumerable<SelectListItem> ExpirationMonthDropdown
{
get
{
return Enumerable.Range(1, 12).Select(x =>
new SelectListItem()
{
Text = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[x - 1] + " (" + x + ")",
Value = x.ToString(),
Selected = (x == Model.ExpirationMonth)
});
}
}
public IEnumerable<SelectListItem> ExpirationYearDropdown
{
get
{
return Enumerable.Range(DateTime.Today.Year, 20).Select(x =>
new SelectListItem()
{
Text = x.ToString(),
Value = x.ToString(),
Selected = (x == Model.ExpirationYear)
});
}
}
}
public void bind_month()
{
var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
ddl_month.Items.Insert(0, new ListItem("--Select--", "0"));
for (int i = 0; i < months.Length-1; i++)
{
ddl_month.Items.Add(new ListItem(months[i],(i+1).ToString()));
}
}
Somebody has asked that they wanted a numeric representation of the months. Here's my codebehind:
// DOB MONTHS
dob_month1.Items.Insert(0, new ListItem("Select", ""));
var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
for (int i = 1; i < months.Length; i++)
{
dob_month1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
It's basically the same as the answer code with minor adjustments.
Result:
/Try this code to bind all month in dropdown list without using database/
public void GetMonthList(DropDownList ddl)
{
DateTime month = Convert.ToDateTime("1/1/2000");
for (int i = 0; i < 12; i++)
{
DateTime NextMont = month.AddMonths(i);
ListItem list = new ListItem();
list.Text = NextMont.ToString("MMMM");
list.Value = NextMont.Month.ToString();
ddl.Items.Add(list);
}
//ddl.Items.Insert(0, "Select Month");
ddl.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}
You can try this...
Ref Here:
http://allinworld99.blogspot.com/2015/01/bind-monthsyear-dropdownlist-c-aspnet.html
for (int i = 1; i <= 12; i++)
{
drpBMonth.Items.Add(new System.Web.UI.WebControls.ListItem(DateTimeFormatInfo.CurrentInfo.GetMonthName(i), i.ToString()))};

Categories