Keeping a listBox's selectedIndex updated while navigating with keyboard - c#

I have this code:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (listBox1.SelectedItem != null)
{
item = listBox1.SelectedItem.ToString();
}
}
Then in the Load event of the Form where the listBox1 is on in the designer I did:
private void NewForm_Load(object sender, EventArgs e)
{
this.Size = new Size(416, 506);
this.Location = new Point(23, 258);
listBoxIndexs();
this.listBox1.SelectedIndex = 0;
}
listBoxIndexs() is:
private void listBoxIndexs()
{
for (int i = 0; i < Form1.test.Count; i++)
{
listBox1.Items.Add(Form1.test[i]);
}
}
In the Load event, I did:
this.listBox1.SelectedIndex = 0;
So when I make Show to this Form i see the listBox when index 0 is already selected.
The problem is when im using my keys arrows up and down to move between the items i see only the Frame around the items moving up down the selectedIndex is all the time keep on 0.
How can i fix it?
I tried to add after the this.listBox1.SelectedIndex = 0; also listBox1.Select(); but it didn't help.
I'm using visual studio c# 2010 pro .net 4 Client Profile.

Make sure your ListBox.SelectionMode is set to One.
If it is set to MultiSimple or MultiExtended, selections will only occur when the user uses the spacebar or the mouse button to select the item. The arrow keys in these modes just moves the selection marquee.

Related

Devexpress Repository ComboBoxEdit loosing cursor position. Edit.SelectionStart Not being assigned

I am having an issue with my comboBoxEdit in a gridcontrol. I am using Winforms Devepress 11.2. I clicked in the text of an existing Repository comboBoxEdit, and typed in "appear backwards", however, it displayed as "sdrawkcab raeppa" like it would in a mirror.
There are some posts about this topic but none of the solutions seem to work
The reason for this is the following
foreach (GridColumn column in this.gvNotes.Columns)
{
var columnEdit = column.ColumnEdit;
if (columnEdit != null)
{
column.ColumnEdit.EditValueChanged += this.PostEditValueChanged;
}
}
private void PostEditValueChanged(object sender, EventArgs e)
{
this.gvNotes.PostEditor();
}
This PostEditor ensures the save button is enabled when the user is still in the current cell. The user does not need to leave the cell or change column for the changes to be posted to the grid.
So this is what I did:
private void PostEditValueChanged(object sender, EventArgs e)
{
ComboBoxEdit edit = this.gridNotes.FocusedView.ActiveEditor as ComboBoxEdit;
if (edit != null)
{
int len = edit.SelectionLength;
int start = edit.SelectionStart;
gridNotes.FocusedView.PostEditor();
edit.SelectionLength = len;
edit.SelectionStart = start;
}
This did not solve the problem of the cursor resetting to the start position. Edit.SelectionStart is not being assinged to the len value.Even though len changes to 1 edit.SelectionStart remains at 0
Does anyone know what event needs to be handled to not loose the cursor position?
This is what I did and seemed to set the caret position
global: private int carretPosition = 0;
editvaluechanged handler
SaveCaretPosition(editValue.ToString().Length);
this.gridView1.PostEditor();
this.gridView1.ShowEditor();
this.SetCaretPosition(this.gridView1.ActiveEditor as DevExpress.XtraEditors.TextEdit);
private void SaveCaretPosition(int position)
{
carretPosition = position;
}
private void SetCaretPosition(TextEdit edit)
{
edit.SelectionStart = carretPosition;
}

ListView do not autoscroll to selected item if its position changed

I have a ListView with about 100 items. I have some item selected from past and now I need to scroll down to find another item, but in that time - refresh occurs and it just automatically scroll back to last selected item.
Is there any possibility how can I turn it off or better dont scroll if I'm currently scrolling?
Thanks for responds :)
I don’t know if this will help but here the settings that do NOT have the issue with scrolling back to the selected item when a new item is added and sorted. However, I use the sort setting and do not call sort or refresh.
Create a new form and place ListView and a Timer on there. On the ListView change ONLY these properties.
Set View to Details
Click Columns add a single Column (Adjust the width to make it wider)
Set Sorting to Ascending
Set the timer Enabled to true
Set interval to 2000 (2 secs)
Double click the Tick event to create a handler
Now add the following code
public Form1()
{
InitializeComponent();
for (int x = 0; x < 100; x++)
{
listView1.Items.Add("Item #" + x);
}
}
private int y = 10;
private void timer1_Tick(object sender, EventArgs e)
{
listView1.Items.Add("Item #" + y + "b");
y += 10;
}
Now when you run this you can select any item and scroll anywhere and it will not jump back to the selected item when a new item is added. However, it will scroll down one line if the item it adds is above …
Perhaps this gets you close enough that you can play with it further to get what you need.
But that is the best I could do w/o getting into sub-classing the ListView Control using the Win32 API somehow.
This is kind of a roundabout solution (and probably a little late), but it seems to work.
I'm using EnsureVisible() on the most recent added item to have auto-scrolling happen, which is called within the timer1_Tick method. But I'm using an "inspectingList" flag that will keep auto-scrolling from happening when you click on a list item. When focus moves away from the listview, scrolling continues as normal.
Part of the solution is what jross said to do, which is create a dummy form to test with:
private void Form1_Load(object sender, EventArgs e)
{
listView1.Columns.Add("Scroll Control", 100);
for(int i = 0; i < 100; i++)
{
listView1.Items.Add(string.Format("Item #{0:000}", i));
}
timer1.Interval = 1000;
timer1.Start();
}
private int newItem = 10;
private bool inspectingList = false;
private void timer1_Tick(object sender, EventArgs e)
{
listView1.Items.Add(string.Format("Item #{0:000}", newItem));
newItem += 10;
if (!inspectingList)
{
listView1.Items[listView1.Items.Count - 1].EnsureVisible();
}
listView1.Refresh();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
inspectingList = true;
if (listView1.SelectedIndices.Count > 0)
{
listView1.Items[listView1.SelectedIndices[0]].EnsureVisible();
}
}
private void listView1_Leave(object sender, EventArgs e)
{
inspectingList = false;
}

Multiselect picture gallery with winforms c#

I'm trying to create a multiselect picturegallery with winforms.
Currently I have created a flowcontrolpanel that adds images as a selectablepicturebox control.
The selectablepicturebox control is a customer usercontrol that is a blank control with a picturebox and a checkbox on the top right of the picturebox. The picturebox is slightly smaller and centered in the usercontrol.
Clicking on the selectablepicturebox control will turn the background on and off indication selection.
What I want to be able to do is to select a bunch of selectablepicturebox controls and be able to capture the spacebar event to check and uncheck the checkboxes in the selected controls.
The problem is that the flowlayoutpanel never knows to capture the spacebar event.
Does anyone know away of doing this or another technology? I'm happy to use any .net based tech.
Thanks
EDIT:
Here is a link to the code
Are you trying the KeyDown event ?
As per MSDN, This member is not meaningful for this control.
Read here & here. Instead, you may try PreviewKeyDown
Solution: [The GitHub codebase]
[Code Changes]
1. SelectablePictureBox.cs - NOTE the Set Focus
public void SetToSelected()
{
SelectedCheckBox.Checked = true;
PictureHolder.Focus();
}
private void PictureHolder_Click(object sender, EventArgs e)
{
BackColor = BackColor == Color.Black ? Color.Transparent : Color.Black;
// TODO: Implement multi select features;
if ((Control.ModifierKeys & Keys.Shift) != 0)
{
// Set the end selection index.
}
else
{
// Set the start selection index.
}
PictureHolder.Focus();
}
// subscribe to picture box's PreviewKeyDown & expose a public event
public event PreviewKeyDownEventHandler OnPicBoxKeyDown;
private void OnPicBoxPrevKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (OnPicBoxKeyDown != null)
{
OnPicBoxKeyDown(sender, e);
}
}
[Code Changes]
1. FormMain.cs
private void FormMain_Load(object sender, EventArgs e)
{
SensitiveInformation sensitiveInformation = new SensitiveInformation();
int index = 0;
//foreach (var photo in Flickr.LoadLatestPhotos(sensitiveInformation.ScreenName))
for (int i = 0; i < 10; i++)
{
SelectablePictureBox pictureBox = new SelectablePictureBox(index);
// subscribe to picture box's event
pictureBox.OnPicBoxKeyDown += new PreviewKeyDownEventHandler(pictureBox_OnPicBoxKeyDown);
PictureGallery.Controls.Add(pictureBox);
index++;
}
}
// this code does the selection. Query the FLowLayout control which is the 1st one and select all the selected ones
void pictureBox_OnPicBoxKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode != Keys.Space) return;
foreach (SelectablePictureBox item in Controls[0].Controls)
{
if (item.IsHighlighted)
{
item.SetToSelected();
}
}
}

Winforms list display options?

I am building a WinForms program that connects to a DB. On one form I want to display a list of elements recovered from the DB. The elements have to be clickable (radio buttons are an option here), and must have a hover option, as I want some info to appear in a textbox when mouse is hovered over a specific item.
I cannot find an adequate ToolBox control for this. Has anyone got some suggestions? I am using VS2010.
Thanks.
There is no such ready-to-use control in .net framework instead you have to design/create your own using Window custom controls.
Using a standard ListBox, you can just track the mouse position with the MouseMove event.
Example:
int _HoverIndex = -1;
private void listBox1_MouseMove(object sender, MouseEventArgs e) {
int index = listBox1.IndexFromPoint(e.Location);
if (index != _HoverIndex) {
_HoverIndex = index;
if (_HoverIndex == -1)
textBox1.Text = string.Empty;
else
textBox1.Text = listBox1.Items[_HoverIndex].ToString();
}
}
private void listBox1_MouseLeave(object sender, EventArgs e) {
_HoverIndex = -1;
textBox1.Text = string.Empty;
}

Don't show context menu if nothing is selected

I like to have a context menu only show up if an item is actually selected in a listbox in a winforms c# application.
Currently, I am able to select an item if it is right clicked properly, and I can disable the right click menu if nothing is selected, however, I don't want the menu to even show up.
how can this be accomplished?
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
genPassMenu.Enabled = lstPasswords.SelectedIndex > 0;
genPassMenu.Visible = lstPasswords.SelectedIndex > 0;
}
I tried both of those situations on their own, and it only works for enabled.
Perhaps Opening isn't the correct event to choose?
Tx
Try this:
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
//if (lstPasswords.SelectedIndex == -1) e.Cancel = true;
e.Cancel = (lstPasswords.SelectedIndex == -1);
}
Easy,
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
e.Cancel = (lstPasswords.SelectedIndex == 0);
}
I typically set the properties of each context menu item according to its appropriateness for the particular GUI element that is selected. Perhaps by setting the visible attribute on each menu item, rather than the whole menu, you can get the results that you want.
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
//genPassMenu.Enabled = lstPasswords.SelectedIndex > 0;
//genPassMenu.Visible = lstPasswords.SelectedIndex > 0;
e.Cancel = (lstPasswords.SelectedIndex <= 0);
}
I saw when the above did hte opposite I reversed the code slightly. For some reason having the equality also didn't work.

Categories

Resources