ListView OwnerDraw height of items - c#

I have a listview with OwnerDraw = true and Details view mode.
I want when the list contains 3,4 or 5 items the height of each item to be listview.Height/3 ,4 or 5 without scrolling.
I know that the height of item depends on size of the font.
This doesn't work
listView.Font = new Font("Arial", listView.Height / nOfItems);
I draw the text at DrawItem event with constant font size: e.Graphics.DrawString(....).

What I do successfully is to adjust the height of the list view by assigning an image list with a dummy image with the appropriate height I want to have.
So e.g. to have rows with each 40 pixel high, I assign an image with 40 pixel height to the list view inside an image list.
You do not have to actually draw the image, it is sufficient to assign it and do owner drawing then. It looks like e.g. the list in the background of this screenshot (it is a CMS I wrote in .NET).

you r right.
actually when you change the ImageSize property in ImageList, i have seen the ImageList goes empty. so for your problem You have to refill the ImageList with desire ImageSize again. try it it could solve your problem.

Related

Image shrinking in a label of c# win forms

I am designing a c# .NET 4.5 win form.While inserting an image in a label, the image is behaving differently.I have two labels and i insert 2 different image in them.In the first one, i am setting image using an Image property and in the second one, i am just using the ImageList.Now the funny thing is that for the second label, its image is shrinking while for the first label its not. Why is it happening so??Please advise. Image is attached herewith for better understanding:
In the image, you can see that image of first label is showing correctly, while that of second label not
The issue is that the Image and ImageList properties work pretty differently. While for the Image, it will display the image as it is, ImageList has a property called ImageSize. This property affects the size of the stored images in the Label ImageList at compile time such that all images in the ImageList will have the size of ImageSize in the runtime.
Your image shrinking in when you use ImageList is likely caused by this. The default value of ImageSize is 16x16.
And you also cannot change the ImageSize at runtime, since it will be like replacing the list, all images will not be displayed(!).
So, if you display your images using ImageList, all your images must be of the same size. Or else, you have to put your images in the Resources.resx such that the image size will not be changed and somehow access to the images there at run time.

Why is the ListBox's DesiredSize.Width always zero?

I am creating a custom control in C#, and want to have a grid cell that contains a ListBox, which can be hidden or shown as desired. Hiding it is easy, I just set the Width to zero, however when I want to show it, I need to know the width that the ListBox would like to use.
I thought that DesiredSize.Width would give me this vale, but it's always zero, even after calling Measure(). Here is the code I'm using to show the ListBox...
_lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
_lb.Width = _lb.DesiredSize.Width;
Any ideas how I find the desired width?
If your ListBox is in the cell of a grid, give that grid column a name. Then in code, you can access the .ActualWidth property of that grid column and use that value to set the width of your ListBox.
That assumes of course that the width of your grid column is not set to Auto, because that would still give you a 0 value.
_lb.Width = myGridColumn.ActualWidth
You might need to subtract a little bit from the column width to make your control fit nicely.
EDIT
One thing that I've found is that the ListBox must have items added to it before it will return anything other than 0 when it is measured.
string myItem = "Don't ask for a bath in Athabaska";
_lb.Items.Add(myItem);
_lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double width = _lb.DesiredSize.Width;
As long as the ListBox has already been added to the window/usercontrol/grid, the above code returns a value of 227.53 for the width variable; using my defaults for font family and size.
If the ListBox has not been added to the window, or it doesn't have any items in it, it will return 0 for the .DesiredSize.Width property.
Also, if the .Visibility property is set to Collapsed instead of Hidden, the width will be 0.
Don't set the width to 0 when starting. Leave the width alone initially, set the .Visibility to Hidden. It will render to the needed width, but won't be shown. Then you can measure it and start playing around with the width.

Why Image size is NaN (wpf) and how to resize via mouse wheel?

I want to load an image and put it into a viewbox. The image is displayed correctly, but, when I'm going to get it's width and height, it's both NaN.
This is my image load code :
Image img = new Image();
img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("1.png");
Viewbox vb = new Viewbox();
vb.Width = img.Width;
vb.Height = img.Height;
vb.Child = img;
cnv.Children.Add(vb);
The reason I want to get the image width and height is so I could resize it (via viewbox resize) later inside the application.
Any idea how to get the image dimension?
And this is how I'm going to resize it (via mouse wheel)
private void cnv_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (vb != null)
{
vb.Width += Mouse.MouseWheelDeltaForOneLine;
vb.Height += Mouse.MouseWheelDeltaForOneLine;
}
}
And it returns error and said that vb.Width is not a valid value.
Questions to sum this up :
How to get the image width / height in wpf?
How to resize the viewbox (which will also result in image resize) via mouse wheel? For example, if I scroll up the wheel, the width and height is added by 1, and if I scroll down the wheel, the width and height is reduced by 1.
Thank you
P.S. Viewbox vb; and Image img; is a global variable, I just shorten the script down.
P.S.S. I know if the Viewbox width and height initialized by a number, lets say 100 and 100, it will work, I just wanna know how to get the image original size.
EDIT :
The resize can be achieved by detecting whether it's scrolled up or down by detecting e.Delta > 0 or e.Delta < 0
(Source : http://social.msdn.microsoft.com/Forums/vstudio/en-US/170c4fd0-1441-4d83-903d-594af75f8fb4/detect-mouse-scroll)
It seems as though the Image object is not fully loaded at that stage. I believe that you can use the Width and Height properties of the ImageSource class instead. That should be fully loaded at this stage.
ImageSource.Width Property
ImageSource.Height Property
For other users, you can also possibly find the values that you want from the ActualWidth and ActualHeight properties of the Image class (inherited from the FrameworkElement class) instead of the more usual Width and Height properties.
FrameworkElement.ActualHeight Property
FrameworkElement.ActualWidth Property
The image original size can only be obtained in Pixels while your WPF controls are measured in Device Independent Pixels so you're going to have to make a translation from Pixels to Device Independent Pixels and back wherever necessary.
That said, to obtain the width and height for a png file, you can load it into a BitmapImage instead of Image and query the PixelWidth and PixelHeight properties for the BitmapImage.
Again, be aware that this is the only place in your code where you're going to be dealing with Pixels, all Width and Height properties (including ActualWidth and ActualHeight, only set after the control is fully rendered) are measured in Device Independent Pixels. This can be misleading because in a lot of situations this happens to be a 1:1 conversion but this is not guaranteed to be the case.
More info can be found here: http://msdn.microsoft.com/en-us/library/windows/desktop/ff684173%28v=vs.85%29.aspx

How can I make the image size smaller on a button?

I have a button and I want image (.ico file) and text to exist on it. My problem is that I want button's height to be small, but I can't manage to "shrink" the image as much as I want to. The result is to have a piece of image visible on the button and not the hole image. At the image property the image size is fixed (48x48) and the option is grey so I can't change it. How can I make this image to be 16x16?
Try buttonname.BackgroundImageLayout = ImageLayout.Stretch; or change this property in designer.
My solution was to use an ImageList control. You can define the size the images are to be displayed (e.g. I set the ImgageList ImageSize property to 16x16) and then set the button.ImageList and ImageIndex properties instead of the Image property.
I think if you use the Paint event of the Button you can draw any image in any size that you want. if it isn't possible to re-size the image you can do that in this way.
If you have the Image with size 16*16 then set these Button properties at design time.
1) TextImageRelation - ImageBeforeText
2) TextAlign - MiddleRight
3) ImageAlign - MiddleLeft
4) Set the
ImageList imageList = new ImageList();
imageList.ImageSize = new Size(30, 30); // specify size you want
If you are using DevExpress Simple Button, you should set BackgroundImageLayout = ImageLayout.Zoom and set backcolor of button to Transparent (from Appereance->Backcolor)

imagelist with label, why does it keep shrinking images

I am using an imagelist with a label control, basically doing custom rolleover affect. However, if I use an imagelist and try and write the image to the label it keeps shrinking the image to 16x16, even when the image in the list is actually 21 high by 65 wide, why is this and how can I stop it, basically display image at its default size?
The problem comes from the ImageList. It has a property ImageSize that has a big impact on the List itself.
If you take a look at design time into the images of your ImageList, they will all show up their real width and height values. But by compile time the images will be stored within the list using the given ImageSize. So all images within a ImageList will be already stretched to the given size at runtime.
Attention: If you change the ImageSize at runtime all images will be removed from the list!
To solve your problem you have two possibilities:
If all of your images are of the same size, set this size to your ImageList at design-time.
If you have images of different sizes you should add them to your Resources.resx file and access them from there, cause here they will be saved as is and access is also fairly easy like Properties.Resources.MyPictureFile

Categories