Assign listview selected item content to flowdocument name - c#

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.

Related

I need to add the selected item of a combo box to the end of selected item in the list box

This is the data in the list box, from a database
Johnie Black
Sarah Smith
Carol Willis
Maggie Dubois
This is the data in the combo Box
(M)
(F)
I want to select a name in the listbox then when I proceed to select the gender from the comboBox the value I select must be added to the end of the name that is selected
example.
Carol Willis(F)
This is what I have tried:
private void Form1_Load(object sender, EventArgs e) { this.namesTableAdapter.Fill(this.namesDataSet.names);
comboBox1.Items.Add("(M)");
comboBox1.Items.Add("(F)");
comboBox1.SelectedIndex = 0;
listBox1.SelectedIndex = 0;
}
//The code above loads the items into the comboBox
//For the lisbox I connected to the database using the option "Use Data Bound Items"
Any form of help will be appreciated
This should point you into the right direction:
public ListBox lbNames = new ListBox();
public ComboBox cbxGender = new ComboBox();
// combobox selected index changed event
private void cbxGender_SelectedIndex_Changed(object sender, EventArgs e)
{
// check if there are selected items
if(lbNames.SelectedItems.Count == 1 && cbxGender.SelectedItem != null)
{
// replace previous added gender
Regex.Replace(lbNames.SelectedItem.ToString(), #".+(\([MF]\))", "");
// append new gender
lbNames.Items[lbNames.SelectedIndex] = lbNames.SelectedItem.ToString() + cbxGender.SelectedItem.ToString();
}
}
Wasnt tested, just a hint.
listBox1.Items[listBox1.SelectedIndex] = listBox1.Items[listBox1.SelectedIndex] + comboBox1.SelectedItem.ToString();
Something like this could do the trick:
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in listView.SelectedItems)
{
if (comboBox.SelectedItem != null)
item.Text += " " + comboBox.SelectedItem.ToString();
}
}
Don't forget to double click/add the "SelectedIndexChanged" event in the properties of your ComboBox on form.

C# ComboBox SelectedItem.toString() not returning expected results

I have a form that allows a user to add players to a roster, by entering the player name and selecting, from a combo box, the division to which the player belongs.
When time comes to add the player to my TreeView control, the node that should display the division selected displays this text instead: System.Data.DataRowView
I got the code to implement this through MSDN here: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem%28v=vs.110%29.aspx
Here's the code in the load function of the form, to fill the combo box:
private void frm_add_players_Load(object sender, EventArgs e)
{
Divisions divs = new Divisions();
Players players = new Players();
DataTable dtDivisions = divs.GetActiveDivisions(); //divisions combo box
DataTable dtPlayers = players.GetPlayersByTourID(this.tourID);
//set the forms datatable
this.dt_players = dtPlayers;
//fill the combo box
this.cmbo_divisions.DataSource = dtDivisions;
this.cmbo_divisions.DisplayMember = "title";
this.cmbo_divisions.ValueMember = "ID";
this.cmbo_divisions.SelectedIndex = -1;
this.cmbo_divisions.Text = "Select a Division";
//set treeview imagelist
this.tview_roster.ImageList = tview_imagelist;
this.tview_roster.ImageIndex = 1; //division icon
//fill treeview
foreach (DataRow dr in dtPlayers.Rows)
{
FillPlayerTreeview(dr);
}
//expand treeview
this.tview_roster.ExpandAll();
this.ActiveControl = this.txt_player_name;
}
Here I call the function to add the player to the TreeView:
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, selItem.ToString());
}
And here is the function that adds the player:
private void AddPlayerToTreeView(string playerName, string division)
{
TreeNode[] tns = this.tview_roster.Nodes.Find(division, false); //try to find the division, if exists
TreeNode tn = new TreeNode();
if (tns.Length > 0) //division exists - add player
{
tn = this.tview_roster.Nodes[tns[0].Index].Nodes.Add(playerName, playerName);
tn.ImageIndex = 0; //player icon
}
else //division doesn't exist - add division, then add player
{
tn = this.tview_roster.Nodes.Add(division, division);
tn.ImageIndex = 1; //division icon
AddPlayerToTreeView(playerName, division);
}
}
And the result is this:
I'm not sure why it won't work.. and I'm at a loss. Any help would be appreciated.
Well, well... maybe something like the following.
Access the combo's data source, which is a DataTable, and extract selected row and column value using selected index. Maybe add some error handling, too.
private void btn_add_Click(object sender, EventArgs e)
{
var data = cmbo_divisions.DataSource as DataTable;
var row = data.Rows[cmbo_divisions.SelectedIndex];
var selected = row["title"].ToString();
AddPlayerToTreeView(txt_player_name.Text, selected);
}
Try this :
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.SelectedItem as string);
}
ToString() will get the type name, but in that case the SelectedItem is a string.
Try with:
private void btn_add_Click(object sender, EventArgs e)
{
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.Items[cmbo_divisions.SelectedIndex].Text);
}
EDIT: Updated to a better way

updating the listview subitem text after an event

I have listview that may change text on event like shown :
private void OnEvent(object sender, AutomateConnectionEventArgs e)
{
if (aListView.InvokeRequired)
{
Invoke(new MethodInvoker(delegate()
{
Traitement(e);
}));
}
else
{
Traitement(e);
}
}
private void Traitement(AutomateConnectionEventArgs e)
{
ListViewItem item = aListView.FindItemWithText(e.idAutomate);
if (e.notification.Equals("connect"))
{
aListView.Items[item.index].SubItems[5].Text = DateTime.Today.ToShortDateString();
aListView.Items[item.index].SubItems[7].ForeColor = Color.Green;
aListView.Items[item.index].SubItems[7].Text = "Connected";
aListView.Items[item.index].SubItems[8].Text = DateTime.Now.ToLongTimeString();
}
}
It's rise no error and when follow this with breakpoint in visual studio it's run correctly but the listview won't change the text in the form
Just Create new ListViewItem object with new values and Update the old ListeviewItem with new ListViewItem object.

object sender listviewitem .text property

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

Pass the values from ListView control to text boxes in c#

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.

Categories

Resources