|
10!
|
|
|
|
|
PIEBALDconsult wrote: 10!
That is 5 each.
|
|
|
|
|
Observe the coding principle of DRY (don't repeat yourself), in fact don't repeat what others have already done either. If at all possible, try using the FileHelpers library. It does nearly everything you could possibly need on CSV processing. I recently used this and found it great.
|
|
|
|
|
|
Personally, I prefer a separation of responsibilities -- one method to read the data, another to split it, another to parse it, etc.
Some times I allow CSV or other text file lines to be commented-out (e.g. first character a semi-colon) -- I don't want to split or parse them.
I haven't really looked at FileHelpers, but does it support newlines within values?
|
|
|
|
|
I haven't worked with FileHelpers for a few months, and have lost touch a bit, but AFAIR it caters for both your requirements. As for SoR, I also think it allows you to handle your own reading and pass it data only for splitting.
|
|
|
|
|
Please ignore my earlier solution - I have been playing with the one Brady supplied, and I am impressed how easily and how well it works:
Add a reference to FileHelper.
Add a class:
using System;
using FileHelpers;
namespace GUITester
{
[DelimitedRecord(",")]
public class Customer
{
#region Constants
#endregion
#region Fields
#region Internal
#endregion
#region Property bases
#endregion
#region FileHelper interaction
public int Id;
public string Location;
[FieldQuoted(), FieldConverter(ConverterKind.Date, "yyyy, MM/dd")]
public DateTime AccessDate;
[FieldQuoted(), FieldConverter(ConverterKind.Date, "yyyy, MM/dd")]
public DateTime CreateDate;
#endregion
#endregion
#region Properties
#endregion
#region Regular Expressions
#endregion
#region Enums
#endregion
#region Constructors
#endregion
#region Events
#region Event Constructors
#endregion
#region Event Handlers
#endregion
#endregion
#region Public Methods
#endregion
#region Overrides
#endregion
#region Private Methods
#endregion
}
}
Add code to your form:
using FileHelpers;
...
FileHelperEngine engine = new FileHelperEngine(typeof(Customer));
Customer[] customers = engine.ReadFile(@"F:\Temp\Records.txt") as Customer[];
foreach (Customer c in customers)
{
Console.WriteLine(c.Id + ": ");
Console.WriteLine(" >" + c.Location);
Console.WriteLine(" >" + c.AccessDate);
Console.WriteLine(" >" + c.CreateDate);
} And by George it works!
I am very impressed indeed. If you use it, vote the article a five, it deserves it...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
I take it you're a regions fan.
|
|
|
|
|
Yes, to an extent. It's my standard boilerplate class file template - it just means I can collapse everything I'm not interested in at the moment without having too many tabs open at the top of the IDE.
Can't stick it when you get regions inside a method though...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
I have a problem with pop windows. When I Open Pop up page, do actions on that and go back to parent page. In Parent page if user click on logout or if session expired,how to close pop up windows.
|
|
|
|
|
The answer depends on the whether it is Windows or Web? Since you have posted to the general C# forum and have not specified which you are using it makes it difficult to give an accurate answer.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I have a ListBox with SelectionMode.MultiExtended .
I add an item programatically and set it as selected. I want it to be the sole selection.
I have tried Clear() on all the Selectedxxx collections, set all the individual Selectedxxx properties to values which should deselect any current selections. I have tried iterating over the SelectedIndices and using SetSelected(x, false) all to no avail, whatever was selected before the addition remains selected highlighted afterwards.
I have resolved the problem by setting the SelectionMode to SelectionMode.One , SelectedIndex to -1, making the addition reverting the SelectionMode and selecting the added item.
Code:
this.lboxEntries.SelectionMode = SelectionMode.One;
this.lboxEntries.SelectedIndex = -1;
this.data.Add(this.txtEditEntry.Text);
RefreshDataBoundControls();
this.lboxEntries.SelectedIndex = data.Count - 1;
this.lboxEntries.SelectionMode = SelectionMode.MultiExtended;
This seems unnecessarily convoluted.
Any one have a more direct method?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
modified on Friday, October 22, 2010 1:08 PM
|
|
|
|
|
Nah. There is that little thingy called documentation[^]; one should regularly scan the list of class members, there's always something useful to be learned...
Henry Minute wrote: a more direct method?
You did ask for a method!
|
|
|
|
|
There's method in your madness, Luc!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Yeah, my methodology exceeds my madness by far. I'll try and keep it that way.
|
|
|
|
|
|
I see nothing wrong with ClearSelected() , here is a little test:
public override void Test(int arg) {
ListBox lb=new ListBox();
lb.SelectionMode=SelectionMode.MultiExtended;
for (int i=0; i<10; i++) lb.Items.Add(i.ToString());
show(lb);
lb.SetSelected(4,true);
show(lb);
for (int i=0; i<10; i+=3) lb.SetSelected(i, true);
show(lb);
lb.ClearSelected();
show(lb);
lb.Items.Add("aha");
lb.SetSelected(lb.Items.Count-1, true);
show(lb);
}
private void show(ListBox lb) {
string s="lb holds "+lb.Items.Count+" items; selected indices: ";
foreach (int index in lb.SelectedIndices) s+=" "+index;
log(s+".");
}
yielding:
17:09:03.395 CPTest.log-59 lb holds 10 items; selected indices: .
17:09:03.415 CPTest.log-59 lb holds 10 items; selected indices: 4.
17:09:03.437 CPTest.log-59 lb holds 10 items; selected indices: 0 3 4 6 9.
17:09:03.462 CPTest.log-59 lb holds 10 items; selected indices: .
17:09:03.481 CPTest.log-59 lb holds 11 items; selected indices: 10.
so I can only suggest your RefreshDataBoundControls() of which no details were revealed, must be the culprit.
|
|
|
|
|
Henry, I thought Luc's answer was fine - I just checked:
1) New form, dump a ListBox, call it lbTest.
2) Set SelectionMode to MultiExtended
3) Add a Button, call it butClear.
4) Form Load event:
private void frmNewTest_Load(object sender, EventArgs e)
{
lbTest.Items.Add("Hello1");
lbTest.Items.Add("Hello2");
lbTest.Items.Add("Hello3");
lbTest.Items.Add("Hello4");
lbTest.Items.Add("Hello5");
lbTest.Items.Add("Hello6");
lbTest.Items.Add("Hello7");
}
5) Button click event:
private void butClear_Click(object sender, EventArgs e)
{
lbTest.ClearSelected();
}
6)Select many lines with Shift and Control.
7) Press button.
My selection and highlight disappear, as I would expect.
What are you doing that is so different?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
OriginalGriff wrote: I thought
You mean you knew.
|
|
|
|
|
Luc Pattyn wrote: You mean you knew.
If I'd known, I wouldn't have thrown together a test!
I was pretty damn sure, but hey! Sometimes you have to code it to find out if it works...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
OriginalGriff wrote: What are you doing that is so different?
The mystery must reside in RefreshDataBoundControls(). If the data source knows about selections, it is simply reinstating them, that's my guess anyway. We weren't shown any relevant code!
|
|
|
|
|
Having just tried your example, I have arrived at a similar conclusion to yourself.
If not the RefreshDataBoundControls() itself, then the fact that the ListBox is DataBound is almost certainly the culprit.
private void RefreshDataBoundControls()
{
CurrencyManager cm = (CurrencyManager)BindingContext[this.data];
cm.Refresh();
}
this.data is a List<string> .
If you or Griff can find a solution, or find that there is no problem on your systems I would appreciate knowing. As I said in the OP, I have resolved it but would prefer a more elegant method.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I'm willing, but I'm afraid I need more code. What is BindingContext[]? How/where/when is the ListBox bound to the List?
|
|
|
|
|
Binding to list box
public List<string> Data
{
get
{
return this.data;
}
set
{
if (this.data != value)
{
this.data = value;
this.lboxEntries.DataSource = value;
}
}
}
BindingContext[] is built in part of the CurrencyManager which Forms/Controls have.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Sorry for the brevity of my last reply but I was half way through preparing my Dinner.
More context.
This is a UserControl containing a ListBox , a TextBox and three Button s (Add, Edit, Delete)
When user selects an item in the ListBox it is copied to the TextBox where they can edit it and hit Edit button to change content of LB. Replace text in TextBox hit Add button to add new text to LB. Hit Delete deletes all selected items from LB
private List<string> data;
private bool adding = false;
public EntryEditor()
{
InitializeComponent();
}
private void RefreshDataBoundControls()
{
CurrencyManager cm = (CurrencyManager)BindingContext[this.data];
cm.Refresh();
}
#region EntryEditor PROPERTIES .........................................
public List<string> Data
{
get
{
return this.data;
}
set
{
if (this.data != value)
{
this.data = value;
this.lboxEntries.DataSource = value;
}
}
}
#endregion
private void lboxEntries_SelectedValueChanged(object sender, EventArgs e)
{
this.btnEdit.Enabled = false;
this.btnDelete.Enabled = false;
if (!this.adding)
{
this.txtEditEntry.Text = "";
}
switch (this.lboxEntries.SelectedItems.Count)
{
case 0:
break;
case 1:
if (this.lboxEntries.SelectedIndex >= 0)
{
this.txtEditEntry.Text = this.lboxEntries.SelectedValue.ToString();
this.btnEdit.Enabled = true;
this.btnDelete.Enabled = true;
}
break;
default:
this.btnDelete.Enabled = true;
break;
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
this.data[this.lboxEntries.SelectedIndex] = this.txtEditEntry.Text;
RefreshDataBoundControls();
}
private void btnDelete_Click(object sender, EventArgs e)
{
string[] selectedItems = new string[this.lboxEntries.SelectedItems.Count];
this.lboxEntries.SelectedItems.CopyTo(selectedItems, 0);
foreach (string s in selectedItems)
{
this.data.Remove(s);
RefreshDataBoundControls();
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.adding = true;
try
{
this.lboxEntries.ClearSelected();
this.lboxEntries.Update();
this.data.Add(this.txtEditEntry.Text);
RefreshDataBoundControls();
this.lboxEntries.SelectedIndex = data.Count - 1;
}
finally
{
this.adding = false;
}
}
}
btnAdd_Click is the problem area.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|