I am trying to access listviewitem selectitem text property via object sender. how do I do this?
private void button1_Click(object sender, EventArgs e)
{
ListViewItem LS = new ListViewItem(#"c:\windows\explorer.exe");
ListViewItem LS1 = new ListViewItem(#"c:\windows\notepad.exe");
listView1.Items.Add(LS);
listView1.Items.Add(LS1);
}
private void listView1_Click(object sender, EventArgs e)
{
// ???? how do I get listviewitem.text property here based on item selected
// ?? am i using the right eventhandler?
}
You can just use the SelectedItems property:
if (listView1.SelectedItems.Count > 0)
listView1.SelectedItems[0].Text // the text of the first selected item
Access it simply by using the ListViewitself
myListView.SelectedItems[0].Text
Related
When i add item to listview like this :
private void button1_Click(object sender, RoutedEventArgs e)
{
ListView1.Items.Add(new ListViewItem() { Content = textBox1.Text });
}
and then i try this in listview selection changed event :
private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
FlowDocument dok = new FlowDocument();
dok.Name = ListView1.SelectedItem.ToString();
// or like this
//dok.Name = ListView1.Selectedvalue.ToString();
}
i am getting error
'System.Windows.Controls.ListViewItem: Joe' is not a valid value for
property 'Name'
adding items like this :
private void button1_Click(object sender, RoutedEventArgs e)
{
ListView1.Items.Add(textBox1.Text);
}
solves that problem but than i can not do something like this :
{
foreach (ListViewItem item in ListView1.Items)
{
if (item.Content.ToString() == textBox1.Text)
{
item.Foreground = Brushes.Red;
item.Background = Brushes.Linen;
item.FontSize = 16;
}
}
i would like to use first option
ListView1.Items.Add(new ListViewItem() { Content = textBox1.Text });
but I how to assign items content as text to document name? Confusing.
I think your core problem here is that you are trying to get the text contained in the ListViewItem, but you are calling ToString() which gets you nothing but the 'string representation' which in your case is
System.Windows.Controls.ListViewItem: Joe
Depending on whether you want the name or value associated with the ListViewItem, you would use different methods.
Method 1
To get the name of a ListViewItem use the .Name property, e.g.:
ListViewItem i = new ListViewItem();
i.Name;
Method 2
If you want to get text value within the ListViewItem use the .Text property, e.g.:
ListViewItem i = new ListViewItem();
i.Text;
Solved the problem :
private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
FlowDocument dok = new FlowDocument();
foreach (ListViewItem item in ListView1.Items)
{
if (item.IsSelected == true)
dok.Name = item.Content.ToString();
}
Always have to be the hard way.
I have two ListBoxes. I want to copy SelectedItem from the first ListBox into Second one.
Why this code does not work ?
private void frm_addDispatchBoard2_Load(object sender, EventArgs e)
{
using(propertiesManagementDataContext db = new propertiesManagementDataContext())
{
var Buildings = db.Buildings.Select(q => new { q.BuildingLandNumber, q.BuildingId });
listBox_allBuildings.DataSource = Buildings;
listBox_allBuildings.DisplayMember = "BuildingLandNumber";
listBox_allBuildings.ValueMember = "BuildingId";
}
}
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex > 0)
{
listBox_selectedBuildings.Items.Add(listBox_allBuildings.SelectedItem);
}
}
The result I got:
try this I am not sure why you are looking for a Contains but if you really need that look at the difference between SelectedValue and SelectedItem
Use this code right here as a test to see if the expected value shows up in a MessageBox
string selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
MessageBox.Show(selected);
this should help you to see the values in the Listbox on the right
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex != -1)
{
var selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
listBox_selectedBuildings.Items.Add(selected);
}
}
I have a SelectionChanged event and works perfectly, but I want to figure out how to "catch" this selected item at the click of a button they need to pass it as parameter to another page and edit this Item. Here's the current code and button SelectionChanged I still implemented because this is what I need.
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
//pega o Carro que foi selecionado
Carro sCar = (Carro)listBox.SelectedItem;
btnEditCar.IsEnabled = true;
btnDeleteCar.IsEnabled = true;
}
else
{
btnEditCar.IsEnabled = false;
btnDeleteCar.IsEnabled = false;
}
}
I need to edit the selectedItem on this button:
private void btnEditCar_Click(object sender, EventArgs e)
{
//Here I need access to the selectedItem on SelectionChanged event.
}
If you could also tell me how to pass the object as parameter would be perfect.
You can do this with binding also
1.Bind ListBoxItem(Carro Object) to the tag of "btnEditCar" in xaml.
Xaml should be like this
<Button Name="btnEditCar" OnClick="btnEditCar_Click" Tag="{Binding}"/>
and now in
private void btnEditCar_Click(object sender, EventArgs e)
{
Carro sCar=(Carro)((sender as FrameworkElement).Tag)
}
This is the good practice,creating a class variable only for temporary purpose is hack
To give a better idea on my comments. Creating a class level variable is like this:
Notice that sCar is declared outside the method, but within the class.
Carro sCar = new Carro();
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
sCar = (Carro)listBox.SelectedItem;
...
private void btnEditCar_Click(object sender, EventArgs e)
{
sCar.ProperyYouWantToChange = "Stuff I want to change"
}
I want to move items back and forth between a ComboBox and a ListBox using C# 2010 (form)
My code seems to work. However, when I move the items back to the ComboBox (from the ListBox) I have a space in between the items. If anyone has a suggestion on how to remove the space between the items in the ComboBox I would greatly appreciate it.
private void stateslistcomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
stateslistBox.Items.Add(statescomboBox.SelectedItem);
statescomboBox.Items.RemoveAt(statescomboBox.SelectedIndex);
}
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
The statescomboBox.Items.Add(item); triggers Another SelectIndexChanged that adds an empty item.
Try
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescombobox.SelectIndexChanged -= stateslistBox_SelectedIndexChanged;
statescomboBox.Items.Add(item);
statescombobox.SelectIndexChanged += stateslistBox_SelectedIndexChanged;
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
alternatively, you can prevent empty items being added.
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
if (!string.IsNullOrEmpty(item)
{
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
}
private void deleteDisplayGamesButton_Click(object sender, EventArgs e)
{
//Game game = new Game(homeTeamComboBox.Text, int.Parse(homeScoreUpDown.Value.ToString()), awayTeamComboBox.Text, int.Parse(awayScoreUpDown.Value.ToString()));
deleteDisplayGamesListView.Items.Clear();
deleteDisplayGamesListView.View = View.Details;
foreach (Game currentgame in footballLeagueDatabase.games)
{
ListViewItem row = new ListViewItem();
row.SubItems.Add(currentgame.HomeTeam.ToString());
row.SubItems.Add(currentgame.HomeScore.ToString());
row.SubItems.Add(currentgame.AwayTeam.ToString());
row.SubItems.Add(currentgame.AwayScore.ToString());
deleteDisplayGamesListView.Items.Add(row);
}
}
I need to pass the values from above ListView control to following text boxes when I use the deleteDisplayGamesListView_SelectedIndexChanged method.
private void deleteDisplayGamesListView_SelectedIndexChanged(object sender, EventArgs e)
{
deleteModifyHomeTeamTxt.Text = "";
deleteModifyHomeScoreUpDown.Text = "";
deleteModifyAwayTeamTxt.Text = "";
deleteModifyAwayScoreUpDown.Text = "";
foreach (Game currentgame in footballLeagueDatabase.games);
{
?-----------------------------
}
}
Futhermore I need to clear the row after inserting to the textboxes,which i selected from the ListView Control.
If you know how to do this please let me know.
You should have access to your deleteDisplayGamesListView from within the SelectedIndexChanged event handler.
This should let you access the ListViewItems and their SubItems.