Hi All
I have a listbox with a list like below
1. 05/7/11
2. 05/07/11
3. 09/8/11
4. 05/11/11
Using the code below that shuld check for a single digit month
string regExp1 = "(?
string arr;
for (int a = 0; a < 99; i++)
{
arr = arrStrUSA[a];
if (arr == regExp1)
{
arr.Insert(4, "0");
listBox2.Items.Add(arr);
}
else
{
listBox2.Items.Add(arr);
}
}
I thought I could iterate through the listbox adding a 0 to the front of the ones with a single digit for a month but nothing comes.
What I need to do is add 0 to line 1, 3 so they look like lines 2, 4
then delete "
Thanks in advance
Mike
11 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
listBox1.Items.Add("05/11/11
private void button1_Click(object sender, EventArgs e)
{
//assuming that each item in the list box begins with the date you are wanting to move to listbox2
//and the format is as per your example, then the following code will do what you are after.
//notice that I am not inserting or replacing anything, just doing the job that needs to be done.
DateTime t;
string name;
for (int i = 0; i < listBox1.Items.Count; i++)
{
//first get the string beginning with the name ( Glenda ) to the end of the string
name = listBox1.Items[i].Substring(listBox1.Items[i].IndexOf("=/>") + 3);
//discard everything past the name from the first occurrence of "<", this leaves just the name
name = name.Substring(0, name.IndexOf("<"));
//now get the date part by extracting the string from pos 0 to the first occurrence of "<" in the listbox item string
t = DateTime.Parse(listBox1.Items[i].Substring(0, listBox1.Items[i].IndexOf("<")));
//Now add to list box 2
listBox2.Items.Add(t.ToString("dd/MM/yy") + ","+ name);
//or can be don in one go
listBox2.Items.Add(((DateTime)t = DateTime.Parse(listBox1.Items[i].Substring(0, listBox1.Items[i].IndexOf("<")))).ToString("dd/MM/yy") + ","+ name));
}
}
I have a listbox with lines looking like this: 05/9/11
And would like them to look like: 05/09/11,Glenda
If I can get a 6 digit date I can delete every thing else
also if I use:
string21.Insert(10, "a");
why does it go in at number 3 I'm a bit lost
Thanks in advance Mike
If it is known that the start of the list item to the first "<" is in fact a date then anything from the "<" to the end of the line can be discarded so getting a substring of the date component eliminates the need to do a replace.
Casting the substring to a DateTime then formatting is gauranteed to return the date string in the final format that you want regardless of region.
i.e. dd/MM/yy, dd/MMM/yy, dd/MM/yyyy, ddd d, MM, yyyy
can't get simpler than this
listBox1.Items.Add(((DateTime)t = DateTime.Parse(s[i].Substring(0, s[i].IndexOf("<")))).ToString("dd/MM/yy"));
I like simple, it it keeps programs simple and makes them more efficient with less overheads.
Cheers

Perhaps, the easiest way of all is this one:
s[0] = "05/7/11
s[1] = "05/11/11
s[3] = "09/8/11
s[4] = "05/11/11
//assuming that each item in the list box begins with the date you are wanting to move to listbox2
//and the format is as per your example, then the following code will do what you are after.
//notice that I am not inserting or replacing anything, just doing the job that needs to be done.
DateTime t;
for (int i = 0; i < s.Length; i++)
{
t = DateTime.Parse(s[i].Substring(0, s[i].IndexOf("<")));
listBox2.Items.Addt.ToString("dd/MM/yy"));
}
//or this way
DateTime t;
for (int i = 0; i < s.Length; i++)
listBox1.Items.Add((t = DateTime.Parse(s[i].Substring(0, s[i].IndexOf("<")))).ToString("dd/MM/yy"));
Unless of course using RegEx is essential for you to use, just a couple of alternatives

I Have a ListBox that has 100 or so items.
some are like this:
05/7/11
and some are like this
05/11/11
But I all the single digit months to have a 0 in front so its like this:
05/07/11
Can you see the difference in the date format, I need them to say 07 not 7 because when
I get to a month with 2 digits its goes wrong
I can delete the tags at the end, also the middle but need to set the date format so it
Reads 05/07/11 that way I have a solid base to start deleting
Using this only outputs the first item lots of times
string regExp1 = "(?
string arr;
for (int a = 0; a < 1; a++)
{
arr = listBox1.Items[a].ToString();
if (Regex.IsMatch(arr, regExp1))
{
arr = arr.Insert(3, "0");
arr = arr.Replace("
listBox2.Items.Add(arr);
}
listBox2.Items.Add(arr);
}
ps That Expresso is cool tool thank you for guidance there.
Thanks Vulpes.
Mike.

theLizardPosted Apr 26, 2012, 6:17 PM
VulpesPosted Apr 26, 2012, 3:55 PM
You therefore need to replace this expression, wherever it occurs, with listBox1.Items[i].ToString() to avoid the errors.
MIke LumleyPosted Apr 26, 2012, 3:04 PM
I've got these for the referance :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
Am I missing something out because both your code looks like it would work!!!Ahhhh
Thanks Mike
theLizardPosted Apr 25, 2012, 6:38 PM
listBox1.Items.Add("05/7/11