Products Technologies Demo Docs Blog Support Company

Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More

Additional to the built-in zooming features, more tricks can be implemented on top. Some users may wish to disable the existing behavior and customize the behavior according to their requirement. This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.

Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More

Disable CTRL + MOUSE WHEEL

The zoom factor in TXTextControl.TextControl can be set using CTRL + MOUSE WHEEL. Disabling this built-in functionality is possible using a simple trick. The MouseWheel event event itself cannot be canceled. Therefore you have to cast its arguments to HandledMouseEventArgs event. Then, you can handle the event, when key events are fired. Simply use an isKeyDown flag in the KeyUp and KeyDown event.

//create isKeyDown flag
private bool _isKeyDown = false;

private void Form1_Load(object sender, EventArgs e) { 
  //subscribe to MouseWheel event
  textControl1.MouseWheel += TextControl1_MouseWheel;
}

private void TextControl1_MouseWheel(object sender, MouseEventArgs e) {
  //check if key has been pressed
  if (_isKeyDown == true) {
    //if yes handle the event
    ((HandledMouseEventArgs)e).Handled = true;       
  }         
}

private void textControl1_KeyDown(object sender, KeyEventArgs e) {  
  //set flag to true when key is pressed
  _isKeyDown = e.Control;         
}

private void textControl1_KeyUp(object sender, KeyEventArgs e) {  
  //set flag to false when key has been released
  _isKeyDown = e.Control;
}

Zoom Using Keyboard Only

Since zooming via the CTRL + MOUSEWHEEL has now been disabled, zooming using the keyboard only, can be added easily. In the KeyDown event, check if CTRL + Plus has been pressed. If yes, simply increase the current zoom factor by ten. If CTRL + Minus has been pressed, decrease the current value by ten. Since the zoom factor cannot be less than ten, Math.Max method is used to ensure that the value is never less than ten. Now, users can zoom in and out by pressing and holding CTRL and Plus or Minus.

private void textControl1_KeyDown(object sender, KeyEventArgs e) {
  //increase zoom by ten percent when pressing CTRL + Plus 
  int currZoom = textControl1.ZoomFactor;

  if (e.Control && (e.KeyCode == Keys.Oemplus)) {
     textControl1.Zoom(currZoom + 10);
  }

  //decrease zoom by ten percent when pressing CTRL + Minus
  if (e.Control && (e.KeyCode == Keys.OemMinus)) {
     textControl1.Zoom(Math.Max(10, currZoom - 10));
  }
}

Set Zoom Level to Default

To reset the zoom back to the default value, press and hold CTRL + 0. This can be implemented in the KeyDown event with this code:

private void textControl1_KeyDown(object sender, KeyEventArgs e) {
   //reset the zoom back to the default (100%) when presing Ctrl + 0
   if (e.Control && (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)) {
       textControl1.Zoom(100);
   }
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Windows Forms

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See Windows Forms products