Deny move within a listbox?

  • Thread starter Thread starter Byron
  • Start date Start date
B

Byron

I'm looking for a way to deny a move from the current listbox item. For
instance, if the user is editing the record associated with the current
listbox item I want to deny a move within the listbox until they have saved
the changes. Is there any way to deny a move from the current item so the
SelectedIndexChanged event never fires no matter move the movement was
attempted? Whether the user uses the cursor keys or mouse to try and leave
the current item I want to be able to block the movement until certain
conditions are met. I suppose I could allow the move, check a condition and
send them back to the previous item if necessary, but I would rather prevent
the departure from the current item in the first place.
 
Hi Byron,

Thank you for posting in the community!

Is your application WebForm based or WinForm based?

And, if your application is WinForm based, you use ListBox control or
ListView control on your form? Only ListView control has the item edit
function, ListBox has no item edit function.

If you was still use the ListBox, can you show me the code how you edit the
listbox item ?

Please provide me the information, so that we can help you better, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
It's a WinForm based application using a ListBox control. The control
serves as a list of items that may be edited and as the user selects items
in the list edit fields are populated with the details of the currenty
selected item. I am not trying to edit within the list, but I want to
prevent the user from moving to another item until I decide they can,
normally preventing the movement because they have not saved changes to the
current item.
 
Hi Byron,

Thanks very much for your feedback.

Ok, I see your concern: you want to disable the item selection in ListBox
control when certain item is editing.

I think you can extend the ListBox control, override WndProc method, then
filter the WM_KEYDOWN and WM_LBUTTONDOWN message.
Do like this:

// The code in the extendedListBox
public bool disable=false;
protected override void WndProc(ref Message m)
{
if(disable==true)
{
if(m.Msg==WM_KEYDOWN)
{
return;
}

if(m.Msg==WM_LBUTTONDOWN)
{
return;
}
}
base.WndProc (ref m);
}

// The code in the test form
private void button1_Click(object sender, System.EventArgs e)
{
extlistbox1.disable=true;
}

private void button2_Click(object sender, System.EventArgs e)
{
extlistbox1.disable=false;
}

In the code, extlistbox1 is an instance of extendedListBox. In the test
form, I disable and enable the "disable" variable in 2 Button.Click event.

=============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Byron,

Does my solution resolve your issue?

If you still have anything unclear, please feel free to post, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I was actually trying to head in a different direction.

I want to leave the list enabled no matter what the current mode is.
Basically I allow all users to browse records in a view-only mode. As they
navigate the listbox the details of the current record are displayed in a
richtext box. If the user has the correct permissions the can select Edit
and the richtext is hidden, edit fields appear populated for editing, and I
set a Boolean IsEditing = true. From this point if the user attempts to
navigate away from the current record, close the form, or close the
application the current record is checked to see if they have changed it.
If they have changed it, they are prompted to Save, Abandon changes, or
Cancel to return to the current record for further editing. If they choose
Cancel, or the save fails, they are returned to the record for further
editing.

This means I have to reposition within the listbox to the one they were on
when they attempted to leave it. What I was hoping for is the ability to
something similar to the Cancel on a form closure to automatically return to
the listbox item it was on prior to the move. I assume though that I will
have to do it programmatically to reposition based on the item index or the
Value property within the list of objects in the listbox.
 
Hi Byron,

Thanks very much for your feedback.

Oh, I think the expression in your reply is just the whole program logic of
your application. In your logic, there are many steps. I think most of the
steps you have fulfilled successfully. Can you show me which step is your
main concern and key obstacle? Then I will help you solve this obstacle.

In your second paragraph, "This means I have to reposition within the
listbox to the one they were on when they attempted to leave it", what does
the last word "it" mean?
There are 2 understandings:
1). "it" means TextBox that is for editing current record.
I think you should refer to TextBox's Validating event, then when your
focus is leaving the TextBox, this event gives you a chance to do something

2). "it" means the currect editing listitem
I think my original reply solution just shows you how to deny move in a
listbox.

If I misunderstand you, please feel free to correct me, and explain more
details to me, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
By "it" I mean the item currently being edited, not any particular field. I
am currently using the SelectedIndexChanged event to retrieve the record the
user selected in the listbox from the database for display and/or editing.
I just want to stop the new record from being loaded and return to the old
one if the old one has pending edits not committed to the database. If the
application recognizes the current record is dirty the user is offered the
chance to save the record and continue on to the one they selected within
the list, abandon the changes to the old record and continue to the record
they selected, or cancel the move and return to editing the previous record.
 
Hi Byron,

Thanks for your feedback.

Ok, I see your scenario. So what is your obstacle? Where is your problem?

Once you told me, I can help you to workaround it. :-)

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Byron,

Does my reply make sense to you? Is your problem resolved?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top