How to set the first item as selected - c#

I have 2 ListView controls here, let's say Listview1 and Listview2, respectively. What I would like to achieve is that I want the first item in Listview2 to be selected & highlighted whenever Listview1's SelectionChanged event is triggered.
I have tried to use the following line of code to make it happened but I guess it's not correct.
private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Listview2.SelectedIndex = 0;
}
The first item in Listview2 is still not selected & highlighted. Can anyone help? Thanks very much in advance.
Edit:
That line is correct. It didn't work because I placed it before the line of code that was doing dynamic loading. No wonder.... :)

I tried with this code and its running fine.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//listView.Focus();
listView2.Items[0].Selected = true;
}
catch { }
}
But after running its output when i select the first item in "listView1" the item of "listView2" is selected but you can not see it because the focus on Listview1. When you click on the listView2 then you will see a blink of selected item. Ithink there is no way to focus on two listview at the same time. When you will uncomment the "listView.Focus()" then you will see that the selected item is highlighted.

Try
private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
((ListViewItem)Listview2.Items[0].Selected) = true;
}

I cannot see any problem with your code. I think you cannot see the highlighting because ListView2 items are not focused. Make the item Focused and see.

Related

How to prevent a user from selecting an item in a listbox, but still letting code select an item? Prefer not to use enabled = false

I am making a Windows Forms application with algorithms for school and I want to add some nice functionality to display that the algorithm is working well. One of those things is that when the user selects an item in one listbox, the items that are part of that one item get automatically selected in another listbox. This is done by the application.
I would like it if the user could not select another item in the listbox that is automatically monitored, but enabled = false sets the color to gray which makes the text invisible when an item is automatically selected.
Is there any other way to achieve this?
What you could do is
When the program selects an entry in the second list, set a flag
When an entry in the list is selected, read that flag
If the flag is not set, unselect the item
Unset the flag
Code wise, this equates to something like the following (please note that I would not write code like this in a real-world szenario, but to get the gist of it, it should suffice)
private bool _valueIsSetProgrammatically = false;
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
this._valueIsSetProgrammatically = true;
this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (!this._valueIsSetProgrammatically)
{
this.listBox2.SelectedItem = null;
}
this._valueIsSetProgrammatically = false;
}
Please note that this snippet unselects the second listbox. If you'd like to retain the selected item, you could change the second method to
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (!this._valueIsSetProgrammatically)
{
this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}
this._valueIsSetProgrammatically = false;
}
(Technically the flag is not needed in this case, you could simply set the SelectedItem of listBox2 to the SelectedItem of listBox1.)

Giving different alignments to different items added to the same listbox

Yesterday, i asked a question but i think I've not made it clear enough. So I'll try again.
I have two methods:
One for displaying a message from a Client as an item in a listbox(lstMsg).
The other for displaying message from the sender into the same listbox.
My Problem: I want to give the listbox item added to my first method a left align and give the listbox item added to my second method a right align.
Below is the code:
private void MessageCallBack(IAsyncResult aResult)
{
lstMsg.Items.Add("Client: ");
lstMsg.ForeColor = Color.Black;
}
private void btnSend_Click(object sender, EventArgs e)
{
lstMsg.BackColor = Color.AliceBlue;
lstMsg.ForeColor = Color.Blue;
lstMsg.Items.Add("You:");
}

C# ListView prevent selection

I'm in a situation with a multiple select ListView where a maximum of three items may be selected. I currently have following code
private void grassListView_SelectedIndexChanged(object sender, EventArgs e)
{
string landType = this.grassLandTypeComboBox.Text;
if (this.grassListView.SelectedIndices.Count < 4)
{
ArrayList selectedGrassTextures = (ArrayList)((Hashtable)this.paintGrass[landType])["textures"];
selectedGrassTextures.Clear();
foreach (ListViewItem listViewItem in this.grassListView.SelectedItems)
{
selectedGrassTextures.Add(listViewItem.Text);
}
}
else
{
MessageBox.Show("You cannot have more than 3 grasses selected for any given attribute type");
}
}
this code works and in the end my selectedGrassTextures HashTable has only three elements. However the GUI still shows the element as (selected/focused?). So to the user it seems that it is still selected. So I like to prevent this, is there anything I can use to either find the last element clicked on and put the selection of it or the focus. Another way would be if there is an event Before SelectedIndexChanged that I can move my < 4 check into. Is there such an event, I thought ListView.SelectedIndexChanging, but in my IDE it shows up as not existing. I'm using visual studio express and framework 3.5, can't use 4.5 for what I'm doing.
Put this in the ItemSelectionChanged event:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (listView1.SelectedItems.Count > 4) e.Item.Selected = false;
}

Right Click to select items in a ListBox

I'm trying to make a list of items that you can do several actions with by right-clicking and having a context menu come up. I've completed that, no problem whatsoever.
But I'd like to have it so that when you right click on a item, instead of leaving the current item selected, to select the item the mouse is over.
I've researched this and other related questions, and I've tried to use indexFromPoint (which I found through my research) but whenever I right click on a item, it always just clears the selected item and doesn't show the context menu, as I have it set so that it wont appear if there is no selected item.
Here is the code I'm currently using:
ListBox.SelectedIndex = ListBox.IndexFromPoint(Cursor.Position.X, Cursor.Position.Y);
Handle ListBox.MouseDown and select the item in there. Like this:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);
}
this one is working...
this.ListBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.List_RightClick);
private void List_RightClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int index = this.listBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
listBox.Items[index];
}
}
}
Can also get same behaviour by setting a MouseRightButtonUp event on the whole listbox then:
private void AccountItemsT33_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
// If have selected an item via left click, then do a right click, need to disable that initial selection
AccountItemsT33.SelectedIndex = -1;
VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), (sender as ListBox)).OfType<ListBoxItem>().First().IsSelected = true;
}

Combobox cancel dropdown

I've got a combobox that opens a new form window with a datagridview, and I want the users to choose the items through that datagridview rather than through the combobox. I've got this code to achieve that:
private void comboBox1_DropDown(object sender, EventArgs e)
{
valSel.incBox = (ComboBox)sender;
valSel.Show();
if (this.comboBox1.DroppedDown)
{
MessageBox.Show("test");
SendMessage(this.comboBox1.Handle, CB_SHOWDROPDOWN, 0, 0);
}
}
As you see I'm also trying to hide the dropdown of the combobox but it isn't working. I assume it's because the combobox hasn't actually "dropped down" yet, so that part of the code is never run.
Is there an event or something I can cell when the combobox has fully "dropped down" so i can send the message to close it again?
You should be able to simply set the height of the ComboBox to something really small. Last time I looked at it, this determined the height of the popup part (the actual height of the control is determined by the UI/font size).
The more elegant way, however, would be using a custom control that just mimics the appearance of dropdown boxes (I'm rather sure that can be done some easy way).
In comboBox1.Enter set the focus to a different control if condition is met.
private void comboBox1_Enter(object sender, EventArgs e)
{
if (comboBox1.Items.Count < 1)
{
comboBox1.DroppedDown = false;
comboBox2.Focus();
MessageBox.Show("Select a list first");
comboBox2.DroppedDown = true;
}
}
1) create a KeyPress event on ComboBox from the properties.
2) write code
private void cmbClientId_KeyPress(object sender, KeyPressEventArgs e)
{
((ComboBox)sender).DroppedDown = false;
}

Categories

Resources