|
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.”
|
|
|
|
|
Problem resolved.
Modified the order of code in btnAdd_Click .
this.data.Add(this.txtEditEntry.Text);
RefreshDataBoundControls();
this.lboxEntries.ClearSelected(); <============== Moved
this.lboxEntries.Update(); <===================== Moved
this.lboxEntries.SelectedIndex = data.Count - 1;
Thanks for the help.
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.”
|
|
|
|
|
Nice. I'm still not sure how it all works, and why it did what it did, and doesn't any more.
What would be wrong with:
this.data.Add(this.txtEditEntry.Text);
this.lboxEntries.ClearSelected();
this.lboxEntries.SelectedIndex = data.Count - 1;
?
1. if RefreshDataBoundControls is repopulating the ListBox, the added line should be gone? and if it isn't, what *is* it doing?
2. the Update() only has a visual effect, it should be all right as soon as your current handler is done anyway.
I'm going to investigate later, I'm off now.
|
|
|
|
|
Without the RefreshDataBoundControls() , the new item is not added to the LB.Items therefore it cannot be selected and the last line (of your snippet) throws an ArgumentOutOfRangeException
Agree about the Update() part. I didn't have that originally, added in an attempt to 'force' a redraw to get rid of the persistent highlight(s), and forgot to remove it.
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.”
|
|
|
|
|
My mistake. The new line is added to data, not to the ListBox.
|
|
|
|
|
OriginalGriff wrote: What are you doing that is so different?
See my reply to Luc below.
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 am having a like this
Name -Nikhil Bhivgade
Class-6th
-------------------------------------
1 English 100 50 Pass
2 Maths 100 50 Pass
3 Science 100 20 Fail
4 History 100 20 Fail
--------------------------------------
in report footer i want to display failed subject Sr.No and Name
you Failed in 3.Science,4.History
|
|
|
|
|
Use a global variable. Global variable is visible for all the sections of the report. String Append Sr.No and Name of the subject based the status pass or fail.
The formula can be:
FormulaA : [Used in the PageHeader][Display of Formula Suppressed]
Global stringvar FailedDetails;
FailedDetails = "";
FormulaB: [Used in Detail Section][Display of Formula Suppressed]
Global stringvar FailedDetails;
WhileReadingRecords;
if (status = "fail")
FailedDetails = FailedDetails + SrNo + " " + Subject
FormulaC: [Used in Footer Section] [Display of Formula Not Sppressed]
Global stringvar FailedDetails;
FailedDetails;
For more information: Search in google with the keyword - "WhileReadingRecords" and "WhilePrintingRecords"
The above formula do not work by copy paste. You need to make some adjustment based on the Syntax.
Hope it Helps
|
|
|
|
|
It is returning me false
any suggestion
|
|
|
|
|
Hi All,
3 months ago i developed a WPF Application offline,and now i need to host this Application online.
This app gets the function to Add,Edit,Delete,Search (and so on ...) records in the database so it use a Databinding and now i need to host it online so the client can connect on the web to insert or update the data .
Can you suggest me which way is the best to host my App online using the Databinding?
Thanks so much for your attention.
..if you need more details don't hesitate to ask me .
Cheers
|
|
|
|
|
You should take a look at Silverlight[^].
You will obviously have to re-write some of your code, but if you app is already designed as a client/sever app you may find you are able to keep large parts of it. Silverlight at least uses a variant of XAML, so you may be able to reuse some of your GUI code.
You could also consider an XMAL Browser application[^], but you will find your platforms are more restricted to IE & Firefox only and Windows only.
Simon
|
|
|
|
|
WPF apps don't run in a browser. However, Silverlight is a subset of WPF, and most of your stuff should workk with little/no change with the possible exception of database access. Silverlight REQUIRES the use of a web service to access a database, even if the database in question is on the same server as the web server.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|