How to set tooltip for a ListviewItem - c#

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);
}

Related

Visibility.Visible not working

I am creating and binding columns/elements into my datagrid.
I created a DataGridTemplateColumnand in that column i'm inserting a TextBox element.
DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
var dockPanel = new FrameworkElementFactory(typeof(DockPanel));
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
Binding bindText = new Binding("Supplier");
bindText.Mode = BindingMode.TwoWay;
factoryText.SetValue(TextBox.TextProperty, bindText);
//This is not working
factoryText.SetValue(TextBox.VisibilityProperty, Visibility.Visible);
//
cellTemplate.VisualTree = factoryText;
dockPanel.AppendChild(factoryText);
cellTemplate.VisualTree = dockPanel;
Now when I add the following line, it allows me to edit my selected cell, but it also hides my TextBox on my datagrid until I double click on that cell then my element is visible again. Once that cell loses focus, my TextBox is hidden again.
columnFeedbackSupplier.CellEditingTemplate = cellTemplate;
I can use the following line to make my TextBox always visible, but I would not be able to save my edited values into my datagrid.
columnFeedbackSupplier.CellTemplate = cellTemplate;
Is there a way to make my TextBox always visible, while using CellEditingTemplate?

Add listview items to a programmatically made listview

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" }));

How can I highlight a listviewitem in listview by programming in Winform?

Just like clicking on the listviewitem by hand, and it turns blue.
Set selected property of the listview item to true.
myListView.Items[indexNumber].Selected = true;
myListView.Items[indexNumber].Focused = true;
Go this way:
Listview lv = new ListView(); // Maybe lv.MultiSelect = false
// Add your items
lv.Items[<index>].Focused = true;

Retrieve the selected ListBoxItem Tag

How do I retrieve the Tag from a listbox item? I have built a list by pulling values from a file and generated textboxes with the parsed data then I made the textboxes a chile of the border. Then I add the border to the listboxitem. So I want to add a tag with a string value and then retrieve that invisible value using the selected item.
So I set the tag...
//created a border above
ListBoxItem item = new ListBoxItem();
item.Tag = path;
item.Content = myBorder;
listBox.Items.Add(item);
Now when that item is selected and I want to read that tag how could I do that?
Since you mentioned that you are using the text-box you could try something like this
if(lb.SelectedItem != -1)
{
string selectedTagx = ((TextBox)lb.SelectedItem).Tag.ToString();
//if just a listbox item
string selectedTagx = ((ListBoxItem)lb.SelectedItem).Tag.ToString();
}
you can add the following to your window or usercontrol that holds the listbox
public MainWindow()
{
InitializeComponent();
//created a border above
ListBoxItem item = new ListBoxItem();
item.Tag = path;
item.Content = myBorder;
listBox.Items.Add(item);
listBox.SelectionChanged += new SelectionChangedEventHandler(listBox_SelectionChanged);
}
void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string path = (listBox.SelectedItem as ListBoxItem).Tag as string;
}
Where "MainWindow()" is the constructor of your window or usercontrol
You can also add the event handler in xaml instead of in the constructor
<ListBox Height="100" Name="listBox" Width="120"
SelectionChanged="listBox_SelectionChanged"/>

ComboBox data binding

I have a combobox control on form that pull its data (Displays and values) from some datasource. On another side I have table with one row. I want when app is lauching, combobox set selectedvalue or selecteditem to value of one column in above row. And when user has changed combobox it will persist change to row. I have tried to bind SelectedValue to this column, but it doesn't work. Combobox just sets on start to first item. What is problem?
EDIT
This is a Win Forms project.
Here is the binding code:
this.comboBoxCountries = new System.Windows.Forms.ComboBox();
this.countriesBindingSource = new System.Windows.Forms.BindingSource(this.components);
//
// comboBoxCountries
//
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.searchCriteriaBindingSource, "Postcode", true));
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.searchCriteriaBindingSource, "CountryCode", true));
this.comboBoxCountries.DataSource = this.countriesBindingSource;
this.comboBoxCountries.DisplayMember = "Name";
this.comboBoxCountries.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCountries.FormattingEnabled = true;
this.comboBoxCountries.Location = new System.Drawing.Point(190, 19);
this.comboBoxCountries.Name = "comboBoxCountries";
this.comboBoxCountries.Size = new System.Drawing.Size(156, 21);
this.comboBoxCountries.TabIndex = 2;
this.comboBoxCountries.ValueMember = "Code";
this.comboBoxCountries.SelectedValueChanged += new System.EventHandler(this.comboBoxCountries_SelectedValueChanged);
//
// countriesBindingSource
//
this.countriesBindingSource.DataMember = "Countries";
this.countriesBindingSource.DataSource = this.dbDataSetCountries;
//
// dbDataSetCountries
//
this.dbDataSetCountries.DataSetName = "dbDataSetCountries";
this.dbDataSetCountries.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// searchCriteriaBindingSource
//
this.searchCriteriaBindingSource.AllowNew = false;
this.searchCriteriaBindingSource.DataMember = "SearchCriteria";
this.searchCriteriaBindingSource.DataSource = this.dbDataSetSearchCriteria;
this.searchCriteriaBindingSource.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.searchCriteriaBindingSource_BindingComplete);
//
// dbDataSetSearchCriteria
//
this.dbDataSetSearchCriteria.DataSetName = "dbDataSetSearchCriteria";
this.dbDataSetSearchCriteria.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
EDIT2
As I have mentioned in my comment below, I have another textbox that is binded to another DataMember of same binding source and textbox working fine. It's appear with appropriate value. When I change DataMember on same datamember on which I set selectedvalue property of combobox binding it's also show a good result and work properly.
Thanks in advance!
Take a look at the DisplayMember and ValueMember properties of the combobox. You need to tell the ComboBox what member from the datasource to display in the drop down, and what value to give when SelectedValue is requested.
It sounds like your ComboBox is bound to a static list while your rows are not. You might consider using a BindingSource that you set the ComboBox and the DataGridView's DataSource to. That way when the DGV navigates to a new row, the ComboBox will be updated with the value for the new row.
Here is a link to the ComboBox on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx
I find it out. So for managing with this issue you should remove SelectedValue databinding from visual studio data bound menu and put an appropriate code to add this databinding in some place after filling all bindingsources:
private void MainForm_Load_1(object sender, EventArgs e)
{
this.searchCriteriaTableAdapter1.Fill(this.dbDataSetCountries.SearchCriteria);
this.searchCriteriaTableAdapter.Fill(this.dbDataSetSearchCriteria.SearchCriteria);
comboBoxCountries.DataBindings.Add("SelectedValue", this.dbDataSetCountries.SearchCriteria, "CountryCode");
}

Categories