I have a button that programmatically adds tabs with a listview attached to a tabcontrol.
I'm trying to access the listview i created so i can add a listview item(s) to it.
Here is my method that creates the tab with the list view
private void AddTabPage(string tabName)
{
ListView lv = new ListView();
lv.Name = String.Format("listView{0}", tabName);
lv.Dock = DockStyle.Fill;
lv.GridLines = true;
lv.View = View.Details;
lv.Columns.Add("Property", -2);
lv.Columns.Add("Value", -2);
TabPage tPage = new TabPage(tabName);
tPage.Name = String.Format("tab{0}", tabName);
tPage.Controls.Add(lv);
tabControl1.TabPages.Add(tPage);
tabControl1.SelectedTab = tPage;
}
As you can see i have made the control names dynamic. Example (listviewComputer1, tabComputer1) Now how would i go about accessing the listview after it has been created?
Note: I can't add the listview item(s) at the time of creating the listview
Get TabPage by name, then get ListView control from its controls:
var lv = tabControl1.TabPages[tabName].Controls
.OfType<ListView>()
.First();
Now you can add items to ListView:
lv.Items.Add(new ListViewItem(new []{ "Foo", "42" }));
Related
I have a form with a button and a tabcontrol. If I click on the button, it adds a new tab with a listbox inside the richtextbox of the tabcontrol, but once I move through the tabs, the listbox is only visibile/available on the newest tab and not in the previous tabs. Anyway ideas on how to fix this please? Below is my code:
private void button1_Click(object sender, EventArgs e)
{
if (tabControl1.Visible == false)
{
tabControl1.Visible = true;
listBox1.Visible = true;
}
TabPage tp = new TabPage();
RichTextBox rtb = new RichTextBox();
int tc = (tabControl1.TabCount + 1);
rtb.Controls.Add(listBox1);
tp.Text = "New " + tc.ToString();
tabControl1.TabPages.Add(tp);
rtb.Dock = DockStyle.Fill;
tp.Controls.Add(rtb);
return;
}
Your code is adding the same listbox to the new richtextbox you are creating; when the last added richtextbox is hidden (by moving to a different tab), the listbox is also hidden. You have two options:
Create a listbox for each richtextbox
Handle the selection change for the tab control and reattach the listbox to the currently visible richtextbox.
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
I am using silverlight and coding in c# and i have to create a scrollbar on a list displayed.
Currently the list is displayed without scrollbar but it should contain one scrollbar as well.
My code to display the List is:
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
lines.SelectedIndex = lines.Items.Count - 1;
"lines" variable contains my ListBox and i tried to achieve it throught scrollViewer but i dont know how to proceed further.
(Please note that i have to create selection changed event on selecting the items on GUI displayed on button click) so the code for scrollbar must not influence that procedure. And i have to code in c# only not in xaml. Thanks in advance for the help.
When you are puttig items Inside ScrollViewer You need to set the Content of ScrollViewer to ListBox,
change your code like below,
//Create ScrollViewer which is a child of Grid
var scroll = new ScrollViewer();
//add to the grid
childGrid.Children.Add(scroll);
//create lisbox and set it to the content of scrollviewer which is a Child of ScrollViewer
var listBox = new ListBox();
scroll.Content = listBox;
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
I am using a ListView with a few fixed-size columns.
The text in the rows may be too large to fit in the column, so I would like to make it so that when the user hovers over the ListViewItem, it shows a tooltip with more text.
I tried setting the ToolTipText property of the ListViewItem:
ListViewItem iListView = new ListViewItem("add");
iListView.ToolTipText = "Add Expanded";
myListView.Items.Add(iListView);
Unfortunately, it didn't seem to work. How can I get ListViewItems to show ToolTips?
Set the ListView's ShowItemToolTips property to true.
Use ListViewItem.ToolTipText Property
// Declare the ListView.
private ListView ListViewWithToolTips;
private void InitializeItemsWithToolTips()
{
// Construct and set the View property of the ListView.
ListViewWithToolTips = new ListView();
ListViewWithToolTips.Width = 200;
ListViewWithToolTips.View = View.List;
// Show item tooltips.
ListViewWithToolTips.ShowItemToolTips = true;
// Create items with a tooltip.
ListViewItem item1WithToolTip = new ListViewItem("Item with a tooltip");
item1WithToolTip.ToolTipText = "This is the item tooltip.";
ListViewItem item2WithToolTip = new ListViewItem("Second item with a tooltip");
item2WithToolTip.ToolTipText = "A different tooltip for this item.";
// Create an item without a tooltip.
ListViewItem itemWithoutToolTip = new ListViewItem("Item without tooltip.");
// Add the items to the ListView.
ListViewWithToolTips.Items.AddRange(new ListViewItem[]{item1WithToolTip,
item2WithToolTip, itemWithoutToolTip} );
// Add the ListView to the form.
this.Controls.Add(ListViewWithToolTips);
this.Controls.Add(button1);
}