1

I have a ListView (auftraegeView). Of this ListView I whish to change the FontSize of its Items through Ctrl + MouseWheel aka. a simple zoom like in excel or a browser.

In the form's ctor I subscribed my method to the event

        this.MouseWheel += scrollZoom;

My EventHandler calculates the new FontHeight and applies it, if it doesn't exceed the bounds. The RowHeight is always kept a little bigger, finally I resize the columns so the zoom also works on the horizontal scale.

private void scrollZoom(object sender, MouseEventArgs e)
    {
        if(Control.ModifierKeys != Keys.Control)
            return;

        int currFontHeight = ListViewFontHeight;

        int delta = (e.Delta)/120;

        int newFontHeight = currFontHeight + delta;

        if(newFontHeight < 1 || newFontHeight > 150)
            return;

        ListViewFontHeight = newFontHeight;
        ListViewRowHeight = ListViewFontHeight + 4;

        auftraegeView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
    }

ListViewFontHeight gets the Font.Height of the first Item. (Value is identical across all Items, so the first is as good as any.)

The set is where the issue seems to be (see below). My idea is that I just go through each Item and change the Font.

    private int ListViewFontHeight
    {
        get { return auftraegeView.Items[0].Font.Height; }

        set
        {
            foreach (ListViewItem line in auftraegeView.Items)
            {
                line.Font = new Font(line.Font.FontFamily, value);
            }
        }
    }

ISSUE / QUESTION
Regardless of the direction I scroll in, the FontSize only increases till it hits the ceiling. The rest works fine (setting ListViewRowHeight, detecting the event at all,...).
What might be causing this?

Mark
  • 419
  • 1
  • 6
  • 21

2 Answers2

4

Try this:

delta = (e.Delta > 0? 1 : -1);

to be on the safe side for different mouse settings.

This works for me:

 float delta = (e.Delta > 0 ? 2f : -2f);
 listView1.Font = new Font (listView1.Font.FontFamily, listView1.Font.Size + delta);
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Added that, but that would just mean that on different mouses "smaller" and "bigger" are switched, not "you can only make it bigger". I found the Issue though. – Mark Apr 28 '14 at 07:26
  • 1
    I found [this related answer](https://stackoverflow.com/a/25738281/7332500) helpful also: "If you can't see the 'MouseWheel' event on a component, then you need to create it manually" – Nick Allan Apr 09 '19 at 09:33
0

Found it myself:

In the ListViewFontHeight - property the get accessor used Item.Font.Height instead of Item.Font.Size

private int ListViewFontHeight
    {
        get { return (int)auftraegeView.Items[0].Font.Size; } //works now
Mark
  • 419
  • 1
  • 6
  • 21