2
Answers

How to create Resizable controls

Abdul Asif

Abdul Asif

17y
3.8k
1

Hello,

I am working on C# project 2.0. I have data grid control and list box control and text box control on the form. I need the following functionalities for these controls.

                       

I want to make above controls resizable at runtime.

User should be able to resize the controls by dragging their borders.

Can anyone please help me with this.

p.s. Please show some codes if you can. Thanks a lot in advance. I need it for my job

 

Answers (2)
0
Abdul Asif

Abdul Asif

NA 27 0 17y
Thanks a Lot!
0
Scott Lysle

Scott Lysle

NA 25.7k 18.8m 17y

Well, there are several ways to do it (Win API, using a separate rectangle, etc).  Depending upon what you are really doing, you might be able to configure each control's anchor properties to achieve the desired effect as well.  Possibly the easiest would be to respond to the controls mouse up, mouse down, and mouse move events.  This is not perfect but it could get you started:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ResizableControls
{
     public partial class Form1 : Form
     {
          bool rtbAllowDragging;
          bool dgvAllowDragging;
 
          public Form1()
          {
               InitializeComponent();
               rtbAllowDragging = false;
               dgvAllowDragging = false;
          }
 
          private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
          {
               if(rtbAllowDragging)
               {
                    richTextBox1.Height = richTextBox1.Top + e.Y;
                    richTextBox1.Width = richTextBox1.Left + e.X;
               }
          }
 
          private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
          {
               rtbAllowDragging = false;
               richTextBox1.Cursor = Cursors.Default;
          }
 
          private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
          {
               rtbAllowDragging = true;
               richTextBox1.Cursor = Cursors.SizeAll;
          }
 
          private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
          {
               dgvAllowDragging = true;
               dataGridView1.Cursor = Cursors.SizeAll;
          }
 
          private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
          {
               if (dgvAllowDragging)
               {
                    dataGridView1.Height = dataGridView1.Top + e.Y;
                    dataGridView1.Width = dataGridView1.Left + e.X;
               }
          }
 
          private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
          {
               dgvAllowDragging = false;
               dataGridView1.Cursor = Cursors.Default;
          }
     }
}