C#: How to add images in listview? - c#

Such a decent question,how do I add images(I googled,but they say I have to add approximately 50 lines of code to do that,why?)?

A big problem with WinForms is that it is just a small abstraction over the native windows UI controls. As long as WinForms supports the required functionality, such as the icons on ListView as Tim mentioned, it is relatively easy to accomplish the task but as soon as WinForms does not support the functionality you want, you need to implement it almost from scratch. Luckily List controls in WinForms provide a relatively easy way to draw your own content using the owner drawing functionality.
Easier way is to have a look at WPF which is much more versatile framework when it comes to requiring more specialized functionality. Though this requires .Net framework 3.0 or greater and has a bit steeper learning curve.

It depends where on the item you want the image to appear.
To set an icon to the left of each item, first set the control's LargeImageList and/or SmallImageList properties, then set the item's ImageIndex property.
To display an image anywhere else on an item, you'll need to use the list view control's owner drawing functionality. There's an example in MSDN, although this may be the same 50 lines of code you've already found.

Related

c# - changing the appearance of my windows form

I am developing a windows form. I want to change the physical appearance of the form and its controls. I was able to use the Skincrafter demo, however this is making the application substantially heavier. I am not satisfied with how "heavy" it feels when navigating through tabs, moving the window, etc. It's fairly simple program and I do not have this issue when not using skincrafter. When not using it, the GUI is very responsive and I want to keep it that way.
I've tried many different skins and the results are very similar in every case.
So I will not be using skincrafter unless someone points me out to possible reasons for this slower GUI. I followed their tutorial and didn't do anything else. Simple process. Before: fast GUI - after: slow GUI.
Can anyone recommend me another way to change the appearance of my controls/form? Is there way to programatically change the appearance of controls and form, without relying on a third party skinning software?
Note that I do not want my users to change skins. I want to apply one and that's it.
thank you
Steve
Edit:
The project is far from done so I am going to give WPF some real good thoughts so I don't end up doing this when I have a 4x times as large as it is right now.
I've already start converting and it shouldn't be much of problem. Found timer and serialport cannot be front from the toolbox and timer are replaced by dispatchtimer. No big deal so far.
One another thing that makes me want to work with WPF is that I may have the need to make graphs (plotting) to show temperature over time. I use serialport to communicate with an external USB controller (virtual COM port) that reads a bunch of temperature sensors and fan speeds. I suspect graphing to be better with WPF.
One last thing: it's just about colors/fonts but more about looks of the buttons, looks of the actual window (border shapes). What I wanted to do was something similar that is done with html and css. I do want to have custom labels to give them the look of a "digital display" for real time temperatures and fan speed measurements.
again thanks all for the help provided!
In the distant past, I added lightweight skinning to WinForms projects by subclassing the out-of-the-box controls (Label, TextBox, etc), and having that subclass read in appearance configuration (e.g. color, font) from an XML file (one could also use app.config).
That approach was easy to implement, and I saw no visible performance degradation.
If you just want a consistent, new look and don't need to enable the end user to pick from a variety of skins, the subclassing approach is very straightforward. For example, to modify the look of a Label:
public MyLabel : Label
{
MyLabel()
{
this.ForeColor = Colors.Blue;
}
}
Then, everywhere you currently have a Label in your code, use a MyLabel instead.
If you are starting a new project, I would highly recommend using WPF instead. It provides a much more flexible UI framework. One key feature is that it separates the look from the UI implementation.

What is the equivalent of a UITableView for WinForms

Is there any component that mimics or provides functionality similar to the iOSs UITableView? I want a nice list, with smooth animations on ordering, delete , custom items in list?
Is there any 'ready-made' open source or commercial component that gives me an equivalent for ordinary WinForms (no XAML/WPF)?
The basic WinForms ListBox has completely no animations or any kind of "nice looking appearance". You can check that pack http://www.telerik.com/products/winforms.aspx or devart analogue.
Since you're going to WPF (congratulations for that), you may want to have a look at the ReorderListBox control in the WPF Bag of Tricks.
It supports reordering by dragging items and the like. You can find the full source code and many interesting examples in the Bag.

Create an Object Editor in C# for WinForms

I need to find, or create an editor that will handle text and images as objects. For instance I have a 3 line string of text, to be able to move it around and position it within a canvas, also the ability to add an image, and possibly resize it within that canvas. and take the result, and save it, whether I get the the offsets and positions manually, of each of the objects (preferable) , or get the entire canvas as an image, to be able to save and print.
Rulers would be great... Im not trying to re-create Photoshop, but the idea is similar.
I will be doing this in a C# WinForms application, it does seem however that a WPF solution might be better suited, and I think I can have a WPF control within winforms...
Any direction or advice would be greatly appreciated.
Forget winforms. It doesn't support anything. Your best bet is to do it in WPF and if you need, you can integrate it into an existing winforms application via the ElementHost.
Please see my similar answers/samples about this:
https://stackoverflow.com/a/15580293/643085
https://stackoverflow.com/a/15469477/643085
https://stackoverflow.com/a/15821573/643085
Also, see this example with support for zoom, panning and resizing functionality:
https://stackoverflow.com/a/16947081/643085
They're all MVVM based and have some interesting features.
You can easily customize these samples and add ANY type of elements:
images,
geometries,
usable interactive UI elements with functionality (TextBoxes, ComboBoxes, whatever),
text,
videos,
FlowDocuments,
or whatever that's visible on screen)
by adding additional data items and their corresponding DataTemplates.

How to change ListView background image layout?

I've tried to find a way to change the layout of the background image on the ListView control.
I've searched around but haven't got any straight answer.
I can see that there is a ListView property BackgroundImageLayout which is supposed to do the job. But when i change it nothing really happens beside that the BackgroundImageLayoutChanged event is triggered.
How it is possible to change the BackgroundImageLayout on the ListView control? Is there any possible workaround to achive my goal?
PS.: I know about 3rd party controls that 'fix' this problem, but I'm looking for a solution that would do the task without any additional components.
ListView is a native Windows control. Yes, it does support a background image, the LVM_SETBKIMAGE message takes care of it. It however doesn't support the boilerplate Winforms BackgroundImage support, notable lacking is BackgroundImageLayout, the property that Winforms implements for the Control class and implements when the ControlStyles.UserPaint is turned on. It is not for native Windows controls, they paint themselves.
The guy that wrote the Winforms wrapper class for ListView did the next best thing, he added a new property to the ListView class called BackgroundImageTile. A layout option that the native Windows control does implement. Which leaves you with just the two layout options that the native control supports. Tile or don't tile.
That same guy did some pretty heroic things to make the Winforms wrapper class behave reasonably. The code is filled with hacks to work around the native control's quirks. Awesome work. His life would have been a lot easier if the Windows team guy would have the luxury to make the Winforms guy's life easier. But it doesn't work that way, ListView has been around a lot longer than Winforms. And wasn't designed that well from the getgo, Microsoft had pretty significant growing pains around that time.
Fast forward and change the rules so you don't depend on legacy code: WPF, Silverlight, WinRT.
According to the MSDN, 'ListView.BackgroundImageLayout Property':
"The API supports the .NET Framework infrastructure and is not
intended to be used directly from your code."
and
This property has no effect on the layout of the background image of the ListView control, therefore it is not shown in the designer or by IntelliSense.
Use the BackgroundImage property to set the background image. See msdn for more info:
That is unless you have something fancy you are trying to do with it?
EDIT: As per a previous thread HERE: the short answer is that you can't. =(
If you desparately want it, try creating a blank image that is the dimensions of your ListView. Add to this image YOUR image and add at it a position that is in the middle. There may be something similar in nature to this in under Bitmaps or Images.

C# + Custom graphics on controls?

I was just wondering if there are ways of creating my own custom winforms controls?
I've been plundering with Visual Studio 2008 now trying to do some c# apps. And the GUI end up looking terrible because of the standard winforms limitations.
And I noticed that I can add images to buttons for example, but ther's no hover effect. Or, the hover effect makes the whole button area gray. I don't want any of that, I just want to either create my own graphics for the controls or find some free (opensource perhaps) controls that already exist.
Any light on any of this, anyone? :)
You can write complete Winforms controls from scratch, doing all the painting and input processing yourself - you just create a class derived from 'Control' and get on with it.
There's a fair bit to making a first-class control which integrates nicely with the VS designer, so people tend to derive their custom control from an existing control which has most of the behaviour they want.
Here would be a good place to start: http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
I'm puzzled, you are doing WinForms development, yet in comments say you have done many months of WPF development, but WPF is not good enough because it is not cross platform.
How is WinForm more cross platform, and have you seen how ugly WinForms looks under mono on a Mac as it's draw via X11.
If you want style and cross platform, go for Flex or Silverlight, as your already know WPF I'd go the Silverlight route.
It's cross platform, and has all the beauty of WPF, but at the cost of reduced functionality out of the box.
First - may be more pragmatic to look at WPF, or hosting some WPF elements inside winforms (which is supported - like so). Other than that - you can do all your own painting if you want; but it is a lot of work.
Any reason why you don't use WPF? You have much more more UI control if you went down the route.
If you must go with WinForms then there are many commercial solutions like DevExpress. If you really want you own look and feel it'll be alot of work.
Yes. You can create your own controls. It is called a User Control. Just select Add->and User Control.
WPF is a good alternative if you want your windows form to look fancy.

Categories