Add content to listview in runtime - c#

I'm looking for some help to add text to a listview with four columns. One way to add text to a listview is like this:
var item1 = new ListViewItem(new[] {"text1", "text2", "text3", "text4"});
lvRegAnimals.Items.Add(item1);
But I wonder how this would be done in runtime, when a user clicks a button to add new content to all the columns? I'm new to this and I preciate some help. Thanks!

Add your code to the button click event handler, some thing like this
void Btn_Click(Object sender,EventArgs e)
{
var item1 = new ListViewItem(new[] {"text1", "text2", "text3", "text4"});
lvRegAnimals.Items.Add(item1);
}

Related

Items aren't showing up in ListView

So, I created a ListView and am currently trying to add items once a button is clicked. But, that doesn't seem to be working for me: the items I'm adding (at least that's what I think I'm doing) aren't showing up in the ListView. What seems to be the problem here? Any and all help would be appreciated.
Methods
private void setListViewItem(string value) {
ListViewItem item = new ListViewItem(value);
this.listView1.Items.Add(item);
}
private void button1_Click(object sender, EventArgs e) {
setListViewItem(textBox1.Text);
}
ListView properties
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {this.columnHeader1});
this.listView1.Location = new System.Drawing.Point(13, 87);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(277, 91);
this.listView1.TabIndex = 9;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
You need to add columns to make your data show up since you have listView1.View set to Details, if you set listView1.View to List, it will work without having to add columns.
You can check this example ListView.View to see how to add data into a Detail view.
You forgot to populate the ItemSource. Please check this link, it should help: ListView, data binding and ItemTemplate

how to create combobox onchange with programmatically

basically, i create a combobox from toolbox and drag it into Form. but this time, i create a combobox with programmatically like this..
ComboBox filterKeyComboBox = new ComboBox() { Left = 100, Top = 22, Width = 150, DropDownStyle = ComboBoxStyle.DropDownList, DataSource = null };
then i fill it with this..
filterKeyComboBox.Items.Clear();
bs.DataSource = fieldTable;
filterKeyComboBox.DataSource = bs;
filterKeyComboBox.DisplayMember = "Value";
then i want to create eventhandler where every i change selected item from that combobox...
i tried like this..
filterAktifComboBox.SelectedIndexChanged += (sender, e) =>
{
Console.WriteLine("Test onchange selected item");
};
i dont know why, thats "Test onchange selected item" not printed in log.
how to create event item change in combobox with programmatically?
What exactly is "filterAktifComboBox" that you are giving the event?
Shouldn't you be doing it like this:
filterKeyComboBox.SelectedIndexChanged += (sender, e) =>
{
Console.WriteLine("Test onchange selected item");
};
Wich sets the event to
filterKeyComboBox

Is it wrong to set ListBox DataSource property to null in order to change list Items?

I found that Items.Clear does not always clear a listbox when the listbox has been filled via a DataSource. Setting the DataSource to Null allows it to be cleared with Items.Clear().
Is this the wrong way to do it this way? Is my thinking a bit wrong to do this?
Thanks.
Below is the code I prepared to illustrate my problem. It includes one Listbox and three buttons.
If you click the buttons in this order everything Everything works:
Fill List With Array button
Fill List Items With Array button
Fill List Items With DataSource button
But if you click the "Fill List Items With DataSource" button first, clicking on either of the other two buttons causes this error: "An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll" with "Items collection cannot be modified when the DataSource property is set."
Comments?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnFillListWithArray_Click(object sender, EventArgs e)
{
string[] myList = new string[4];
myList[0] = "One";
myList[1] = "Two";
myList[2] = "Three";
myList[3] = "Four";
//listBox1.DataSource = null; <= required to clear list
listBox1.Items.Clear();
listBox1.Items.AddRange(myList);
}
private void btnFillListItemsWithList_Click(object sender, EventArgs e)
{
List<string> LStrings = new List<string> { "Lorem", "ipsum", "dolor", "sit" };
//listBox1.DataSource = null; <= required to clear list
listBox1.Items.Clear();
listBox1.Items.AddRange(LStrings.ToArray());
}
private void btnFillListItemsWithDataSource_Click(object sender, EventArgs e)
{
List<string> LWords = new List<string> { "Alpha", "Beta", "Gamma", "Delta" };
//listBox1.DataSource = null; <= required to clear list
listBox1.Items.Clear();
listBox1.DataSource = LWords;
}
}
According to Microsoft it looks like setting the Datasource to Null then Clearing the list is acceptable.
Source: http://support.microsoft.com/kb/319927
If your listbox is bound to a datasource, then that datasource becomes the 'master' of the listbox. You then don't clear the listbox, but you need to clear the datasource.
So if the listbox is bound to LWords, you do Lwords.clear() and the listbox would be cleared.
And that is correct behaviour, because that is what being databound is all about.
If you set the datasource to null, you are basically telling the listbox that it is no longer databound. And of course as a side effect of that it becomes empty.
But depending on the situation you might not want the listbox just to be cleared, but you might want to clear the datasource and the listbox both.
Suppose you want to clear LWords via your GUI, and that LWords is the source of your listbox, you press a button and you set the datasource to null, you see the listbox becoming empty, thinking that LWords is not empty, but LWords is not empty at all, and then in this situation that would be a bug.

How to add dynamic textbox in C# Combobox

I want to add "other" option in my combobox lists, and when we selected it then dynamically a text box should appear and asks for other value like other talent things.
Thanks in advance,
Vengadesh
You may need code like following :)
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox.Text.Equals("Other"))
this.yourTextBox.Visible = true;
else
this.yourTextBox.Visible = false;
}
Try it,
there is no way to add TextBox in a ComboBox.
instead of that we can add items in the combobox.
here the combobox is the object of ComboBox class
combobox.Items.Add(new ComboBoxItem()
{
Text="Other"
});

Updating the value of a subItem in a ListViewItem inside a listview c# (Winforms)

I would like to update the data contained in a ListviewItem contained itself in a ListView.
The idea is when a row of the listview is selected, I click on a button and the data is updated .
I have this code :
ListView listView1 = new System.Windows.Forms.ListView();
ListViewItem lv1 = new ListViewItem("me");
lv1.SubItems.Add("my brother");
listView1.Items.Add(lv1);
Button myB = new System.Windows.Forms.Button();
private void myB_Click(object sender, EventArgs e)
{
listView1.SelectedItems[0] ....... ;
}
I know how to go any further to acces to modify the value of "my brother" to "my sister".
Thanks in advance.
Check if something is selected then access the first ListViewItem in the SelectedItems
if(listView1.SelectedItems != null)
{
ListViewItem item = listView1.SelectedItems[0];
item.SubItems[0].Text = "Sister";
}
this is the reference on MSDN for ListViewSubItem class

Categories