ComboBox text change failure - c#

I am calling the following on ToolStripMenu item click :
protected void windowNewMenu_Click(object sender, EventArgs e)
{
if (_selectedThings.Contains(((ToolStripMenuItem)sender).Tag))
{
selectedItemsPropertyGrid.SelectedObject = ((ToolStripMenuItem)sender).Tag;
selectedItemsComboBox.SelectedText = (((ToolStripMenuItem)sender).Tag.GetType()).ToString();
}
}
However the second line that is supposed to change does not seem to produce any visible changes to the ComboBox. Any Suggestions?

SelectedText is for highlighting a portion of the text, not selecting an item in the list. For that, you need SelectedItem.
Or you can just use:
selectedItemsComboBox.Text = ...

Related

ObjectListView get object with contextmenu

Hy!
I would like to create an ObjectListView, where you can delete items with a ContextMenu.
So basicly I used to delete it by getting OLV.SelectedIndex, and then deleting from the list OLV is based on, and re-setting the OLV objects.
Then I realized, if I sort the OLV, then delete an item, it deletes another item, since the selected item index does not equals the index in the list.
With OLV CellRightClick event I can get the object behind the clicked item (e.Model),but I dont know how to pass it to the ContextMenu click event handler.
Subjects is a List.
private void subjectListView_CellRightClick(object sender, BrightIdeasSoftware.CellRightClickEventArgs e)
{
if (subjectsListView.SelectedIndex != -1)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Delete", new EventHandler(DeleteItem));
subjectsListView.ContextMenu = cm;
}
}
void DeleteItem(object sender, EventArgs e)
{
//get the Subject object, which was clicked on
Subjects.RemoveAt(subjectsListView.SelectedIndex);
subjectsListView.SetObjects(Subjects);
}
So basically I want to get the object (not the index) when the ContextMenus "Delete" item is clicked.
Also, I feel like there is an easier way to do this.
Thanks for the answer.
I would just assign an appropriate ContextMenuStrip from the designer to the ObjectListView.ContextMenuStrip property and then handle the click of the corresponding "Delete" click like this:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) {
if (objectListView1.SelectedObject != null) {
objectListView1.RemoveObject(objectListView1.SelectedObject);
}
}
Or is there a requirement I am missing from your question?

C# Winforms: Set text in ComboBox based on selected Item

I'm new to c# and I'm now learning how to trigger events based on some form actions.
This is part of view:
private void comboGoodsName_TextChanged(object sender, EventArgs e)
{
controller.selectName(comboGoodsName.Text);
}
public void nameChanged(object sender, MeasurementEventArgs e)
{
comboGoodsName.TextChanged -= comboGoodsName_TextChanged;
comboGoodsName.Text = e.value;
comboGoodsName.TextChanged += comboGoodsName_TextChanged;
}
And this is part of controller:
public void selectName(string name)
{
model.Name = name.Split('|')[0].Trim();
if (name.Contains(" | "))
{
string code = name.Split('|')[1].Trim();
model.NameCode = code;
}
}
The scenario is as follows:
I want to have a ComboBox with some items in it (doesn't matter what's the source). Items are combination of name and code in following format: NAME | CODE. When I enter some text in ComboBox (type it in), comboGoodsName_TextChanged is triggered, which in turn calls selectName which sets model's property, which in turn raises an event which is observed by nameChanged. This works fine, as expected (puts NAME in ComboBox and CODE to TextBox - not shown as not relevant). Problem shows up when I select item from ComboBox drop-down list. When I select item, instead of showing NAME in ComboBox, I see NAME | CODE.
Edit: In the model, property is set correctly, which I confirmed by printing its value. So, issue is related only to displaying proper value in ComboBox.
Try this:
private void comboGoodsName_SelectedIndexChanged(object sender, EventArgs e)
{
// if combobox has selected item then continue
if (comboGoodsName.SelectedIndex > -1)
{
// split the selecteditem text on the pipe into a string array then pull the first element in the array i.e. NAME
string nameOnly = comboGoodsName.GetItemText(this.comboGoodsName.SelectedItem).Split('|')[0];
// handing off the reset of the combobox selected value to a delegate method - using methodinvoker on the forms main thread is an efficient to do this
// see https://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker(v=vs.110).aspx
this.BeginInvoke((MethodInvoker)delegate { this.comboGoodsName.Text = nameOnly; });
}
}

get the clicked item from a IsItemClickEnabled ListView in C# and WinRT

how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.

ListView Item select in winform

I want to select item in a ListView upon clicking. I also want to know what I clicked.
I work on winforms with c#.I also want to know How I can clicking the all row?
Just handle the Click event on the list and use the ListView.SelectedItems property to get what items are selected:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0];
}
u can use MouseEventArgs and get the mouse location check if it exists inside the selected item bound , that means the click was made on the selected item .
EDIT :
example :
private void myList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (myList.SelectedItems.Count >= 1)
{
ListViewItem item = myList.SelectedItems[0];
//here i check for the Mouse pointer location on click if its contained
// in the actual selected item's bounds or not .
// cuz i ran into a problem with the ui once because of that ..
if (item.Bounds.Contains(e.Location))
{
MessageBox.Show("Double Clicked on :"+item.Text);
}
}
}
also if you use xaml for window then you must add MouseUp="listView1_Click" attribute to ListView tag

Prevent user from deselecting an item in a ListBox?

I've got a ListBox with a bunch of items in it. The user can click an item to edit its contents. How do I prevent the user from deselecting all items? i.e., the user shouldn't be able to have nothing selected.
There is a case missing in your situation, which is when the list is cleared you will reselect an item there is no longer on the list. I solve this by adding an extra check.
var listbox = ((ListBox)sender);
if (listbox.SelectedItem == null)
{
if (e.RemovedItems.Count > 0)
{
object itemToReselect = e.RemovedItems[0];
if (listbox.Items.Contains(itemToReselect))
{
listbox.SelectedItem = itemToReselect;
}
}
}
I then put this inside a behaviour.
I'm not sure if there is a direct way to disable deselecting an Item, but one way which would be transparent to the user is to keep track of the last selected Item, and whenever the SelectionChanged event is raised and the selected index is -1, then reselect the last value.
This Works for Sure to Prevent User from Deselect... Add those 2 Events to your checkedListBox1 and set the Property CheckOnClick to "True" in Design Mode. (MSVS2015)
private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
private void checkedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
To disable on or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
ListItem item = new ListItem(yourvalue, yourkey);
item.Attributes.Add("disabled","disabled");
lb1.Items.Add(item);
One solution, as suggested by amccormack:
private void hostsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(lstHosts.SelectedItem == null)
{
if(e.RemovedItems.Count > 0)
{
lstHosts.SelectedItem = e.RemovedItems[0];
}

Categories

Resources