updating the listview subitem text after an event - c#

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.

Related

MenuItem Click event handler not called

I´m building a ContextMenu on the fly, like this
readinstance = null;
ContextMenu cMenu = new ContextMenu();
for (int i = 0; i < instances.Length; i++) {
string text = String.Format("{0} - {1}", instances[i].Id, instances[i].FormName);
MenuItem item = new MenuItem(text, new EventHandler(cMenuitem_Click));
item.Tag = instances[i];
cMenu.MenuItems.Add(item);
}
cMenu.Show((Button)sender, new Point(0, 0));
cMenu.Dispose();
if (readinstance == null)
throw new Exception("Must select some instance");
and the handler is
void cMenuitem_Click(object sender, EventArgs e)
{
MenuItem item = (MenuItem)sender;
readinstance = (FormPrintingStorage)item.Tag;
}
The menu displays correctly, but when I click some of the options, the handler is not called, so readinstance remains null, and the exception throws. As a side note, when I click any of the options, the menu disappears.
I cannot see what is wrong with my code. Any help will be appreciated.
I´m answering my own question, because I tried more ways.
The first one was to replace the ContextMenu with a ListView and an "Ok" button, at no luck, because the wait loop needed a Thread.Sleep. No comments.
The solution was to implement a new dialog with an empty list view an the Ok button. Some of the relevant code follows. Note that only TreeViewItem/s are moved between the main form and the dialog.
ListViewItem _result = null;
public ListViewItem Result { get { return _result; } }
public List<ListViewItem> Source
{
set
{
listView1.Items.Clear();
foreach (ListViewItem item in value)
listView1.Items.Add(item);
listView1.View = View.List;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (_result == null)
return;
DialogResult = DialogResult.OK;
Close();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListView list = (ListView)sender;
ListView.SelectedIndexCollection indices = list.SelectedIndices;
if (indices.Count == 0)
return;
_result = list.Items[indices[0]];
}
Getting the Result, the main form may do anything it wants with the Tag member. In fact, I´m using the same dialog for two different purposes in the same form.

Disable button when the selected value in comboBox changed

I have a button and a comboBox. The comboBox has 2 value, 'yes and no' I want to disable the button if the selected value is no while i want to enabled if the value selected is yes what would I do, I dont know where will I put the code and also my code seems wrong.
if (ComboBoxCustType.SelectedIndex = 0)
{
Button1.Enabled = false;
}
else
Button1.Enabled = true;
Since you didn't specify WinForms or WPF, this is for WinForms.
You have to create an event on the ComboBox.SelectedIndexChanged and inside the event handler, you write your code to handle the selecteditem text.
public Form1()
{
comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
InitializeComponent();
CheckSelction();
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckSelction();
}
void CheckSelction()
{
if (comboBox1.SelectedItem != null)
{
var item = comboBox1.SelectedItem.ToString();
button1.Enabled = item == "yes";
}
else
button1.Enabled = false;
}

Listview to textview

I want to when i'm clicking one item in my list view to send the in a textview
I'm using this code but nothing happens
listview1.ItemClick += listview1_SetOnClickListener;
}
void listview1_SetOnClickListener(object sender, EventArgs e)
{
TextView textview1= FindViewById<TextView>(Resource.Id.PanelCaptionSelect);
ListView listView1 = FindViewById<ListView>(Resource.Id.PanelCaptions);
String list;
list = listView1.SelectedItem.ToString();
textview1.Text = list;
SOLVED!
lvDetail.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
{
string selectedFromList = lvDetail.GetItemAtPosition(e.Position).ToString();
lbltext.Text = selectedFromList;
};

Assign listview selected item content to flowdocument name

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.

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