How can I set an icon for a ListViewSubItem? - c#

In a ListView you can have icons on each item.
When viewing in Details-mode, the icon is shown in the left-most column.
Can I show an icon in some other column?

The ListView control does not support images in sub-items natively. The easiest thing to do is switch to a DataGridView and use a DataGridViewImageColumn. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in the ListView control. To do this set ListView.OwnerDraw = true and handle the ListView.DrawSubItem and ListView.DrawColumnHeader events.
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
// Only interested in 2nd column.
if (e.Header != this.columnHeader2)
{
e.DrawDefault = true;
return;
}
e.DrawBackground();
var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}

Use P/Invoke and send LVM_SETITEM message to the listview (you should set LVS_EX_SUBITEMIMAGES style on control creation or via LVM_SETEXTENDEDLISTVIEWSTYLE), specify the subitem index and the corresponding image index. You will need to do it for every list item you insert.

ObjectListView is an open source wrapper around a .NET Winforms ListView. It supports images on subitems using the p/invoke strategy that that #ligget78 mentioned. It also solves many other common problems with a ListView.
It allows you to make very nice looking ListViews with a minimum effort:
(source: sourceforge.net)

Inherit from ListView and draw your own icons.
public class MyListView : ListView
{
protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
{
base.OnDrawSubItem(e);
}
}

The icon is shown in the "first" column, and this is also the basis for the keyboard prefix search. One possible solution could be to reorder the columns by setting the DisplayIndex of the first column to something else.
listView1.Columns[0].DisplayIndex = 1;
This of course only works if you need an icon in only one column.

There's no .NET support for this.
Have a look at this project.

Take a loot at this:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/
In addition to the accepted answer, you should handle the DrawItem event as well, or it will not work.

Related

Add item to ListBox (ListView) by user

Can i make ListView or ListBox editable by user?
For example: user can add a new item at list (without any buttons).
Can i do that? Maybe give some simple example.
P.S. It is about WPF.
In winforms this is simple with a ComboBox. The Text is added if it is new when the user presses Enter:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
if (!comboBox1.Items.Contains(comboBox1.Text))
comboBox1.Items.Add(comboBox1.Text);
}
There was no WPF tag at first, but the same should be possible in WPF as well..
(Make it editable, set the dropdown to visible and catch the enter key..can't provide code atm)
Update: After a rather quick check it seems WPF can't do it out of the box. I'm (somewhat) surprised that a useful control (an editable listbox) that has bee with Windows since the 90s (at least) is no longer there. But maybe I'm wrong..
There is no suitable method to do this trick without buttons. Try DataGridView instead http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

tabControlPages remove picture

Try to find solution for next - on mouseEnter - show picture on TabControl page and on MouseLeave - clear it.
Currently done - just show pic on tab.
There are a lot of question refered to "How to show" picture - this and this or this ant also a lot of others.
For this I use next way - add imageList and just show pic on mouseEnter -
private void tabControlPages_MouseEnter(object sender, EventArgs e)
{
tabPageAdd.ImageIndex = 0;
}
For removing - try to read about some methods on MSDN - but have found nothing (maybe not found).
As variant think, can put in collection transparent icon and just change it on mouseLeave, but think it's not a perfect solution. If use imageListIcons.Images.Clear(); - it's fully clear ImageList - as and expected.
So, are ther is some solution avaliable for correct way to hide/show icon on tab from tabControl ?
Just set ImageIndex = -1 to remove the tab icon:
private void tabControlPages_MouseLeave(object sender, EventArgs e)
{
tabPageAdd.ImageIndex = -1;
}

Dynamically adjusting the DropDownHeight of a custom ComboBox C#

I have a custom ComboBox with a TreeView inside of it (modified from code here), and am having issues with getting the DropDownHeight to correctly show everything. Not only can I not get the initial DropDownHeight right (tried setting to treeview.height), but I would also like it to resize when a tree is opened to show everything without a scrollbar. Is this possible? Let me know if you need any more information!
This bit of code in the link you provided should work:
private void ShowDropDown()
{
if (dropDown != null)
{
treeViewHost.Width = DropDownWidth;
treeViewHost.Height = DropDownHeight;
dropDown.Show(this, 0, this.Height);
}
}
ComboBox automatically resizes itself based on the content it is rendering.

How to disable CheckedListBox but enable scrolling winforms

I know that the question might seems to easy, but I can't find solution. I have CheckdListBox in my form. I have list of checkboxes inside. If I do that:clbxMyControl.Enabled = false; then I can't scroll to see all items. How to allow scrolling on disabled CheckedListBox?
Thanks!
Instead of disabling the control you should change it's SelectionMode like this:
checkedListBox1.SelectionMode = SelectionMode.None;
The user won't be able to select an item but will be allowed to scroll
You can prevent the user from checking items with the ItemCheck event:
bool listEnabled = true;
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (!listEnabled) e.NewValue = e.CurrentValue;
}
But do make sure that it is obvious that checking no longer works:
private void setListEnabled(bool enabled) {
listEnabled = enabled;
if (listEnabled) checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
else checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
The ListBox has only one handle in the Windows API which is set to enabled or disabled. Therefore there is no native way to enable the scrolling but the items.
You might be able to build your own control that has the requested behavior but you will have to paint it your self from scratch which might be a lot of work or you find a third party control that supports the behavior.
Or you can not use the enabled property but change the background/font color to make it look disabled and set:
checkedListBox1.SelectionMode = SelectionMode.None;
Another alternative might be the DataGridView. It is much more powerful, I'm not sure if it disables the scrollbar, too but if it does you are able to make the cells readonly and color them.
You can use this code to make CheckedListBox scrollable.
clbxMyControl.SelectionMode = SelectionMode.None;
Because Enable=false; make control unscrollable because this make the container of checkbox disabled.

rename control in wpf using c#

if I add control in Microsoft Blend 4 without set Name to this control and I want to set name to it and use it in c# how ?
example I added button using Blend in my layout but without give it a name
I want to give it a name using c# without x:Name="" in xaml
In your place I would give LogicalTreeHelper.GetChildren (this) a chance. It returns a collection of children to Window (this is a handle to Window) Reference MSDN
From there you can try to find your control.
But I think it is easier to try to rewrite the control (or look for another component) so you can have names on the children. That was your problem from the start.
Hope it helps
Gorgen
First, why in the world would you want to do that?
If you do not set a name you have no easy way of accessing the control. However you can get access to the control via relationships to other controls or events that pass a reference, for example the loaded event.
e.g.
private void Menu_Loaded(object sender, RoutedEventArgs e)
{
(sender as Menu).Name = "MainMenu";
}
Or if the control is the child of another control:
(ControlStack.Children[0] as Menu).Name = "MainMenu";
But i cannot think of anything useful that could be achieved by that...
You probably just want to get a reference to the object which you can easily store in a class member. In some cases you can also slice up your XAML using resources.
e.g.
<local:SomethingIWouldLikeToReference x:Key="SomethingIWouldLikeToReference"/>
<local:UserControl x:Name="userControl">
<Stuff>
<MoreStuff Content="{StaticResource SomethingIWouldLikeToReference}"/>
</Stuff>
</local:UserControl>
public MainWindow()
{
InitializeComponent();
MyReference = FindResource("SomethingIWouldLikeToReference") as SomethingIWouldLikeToReference;
}
Example if I have ListView Control and I want to use it to add items and remove items
Make private ListView and initialize it
ListView temp_control_List = new ListView()
then make loaded Eventhandler from Blend so it will be in VS then
private void ListView_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
temp_control_List = sender as ListView;
}
Now you can add and remove to and from the list view control from temp_control_List

Categories