S J

S J

  • NA
  • 171
  • 25.1k

how to draw rectangle according to panel width and height and loaction

Dec 2 2020 2:30 AM
hi
 
how to draw rectangle according to panel width and height and loaction.
draw rectangle if exceed panels height and width,adjust the user given height and width to panel size and draw rectangle. after substraction or calculation if get height and width as negetive at that time also need adjust and draw rectangle withine panel only. i wrote somethine like below
 
if ( string.IsNullOrEmpty(txtX.Text) || string.IsNullOrEmpty(txtY.Text) || string.IsNullOrEmpty(txtWidth.Text) || string.IsNullOrEmpty(txtHeight.Text) )
{
MessageBox.Show("Please fill all the feilds ");
}
else
{
_displayobj.xCoordinate = Convert.ToInt32(txtX.Text);
_displayobj.yCoordinate = Convert.ToInt32(txtY.Text);
_displayobj.rectWidth = Convert.ToInt32(txtWidth.Text);
_displayobj.rectHeight = Convert.ToInt32(txtHeight.Text);
_displayobj.height = panel1.Height - _displayobj.yCoordinate - padding;
_displayobj.width = panel1.Width - _displayobj.xCoordinate - padding;
//if (_displayobj.height < 0 || _displayobj.width < 0)
//{
// _displayobj.rectHeight = (panel1.Height-_displayobj.height-padding);
// _displayobj.rectWidth = (panel1.Width- _displayobj.width-padding);
//}
if ( _displayobj.rectWidth > _displayobj.width || _displayobj.rectHeight > _displayobj.height || _displayobj.xCoordinate>1050 || _displayobj.yCoordinate>1050)
{
_displayobj.rectHeight = Math.Min(_displayobj.rectHeight, _displayobj.height);
_displayobj.rectWidth = Math.Min(_displayobj.rectWidth, _displayobj.width);
//_displayobj.rectHeight = _displayobj.height;
//_displayobj.rectWidth = _displayobj.width;
string message = ("The given size and location are not fit to this area, so we are adjusting rectangle withine drawing area.If you want to draw press 'OK'");
string title = ("Please Confirm Your Action");
MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
DialogResult result = MessageBox.Show(message, title, buttons);
if ( result == DialogResult.OK )
{
Graphics g = panel1.CreateGraphics();
g.DrawRectangle(Pens.Black, _displayobj.xCoordinate, _displayobj.yCoordinate, _displayobj.rectWidth, _displayobj.rectHeight);
txtHeight.Text = Convert.ToString(_displayobj.height);
txtWidth.Text = Convert.ToString(_displayobj.width);
panel1.Invalidate();
}
else if ( result == DialogResult.Cancel )
{
_displayobj._isValid = false;
return _displayobj._isValid;
}
}
}
 
its not working properly if i get  _displayobj.rectHeight negetive its not drawing rectangle, i need to adjust and draw