I have a drawn grid on a WinForms form. When I resize the form and redraw the grid, the grid lines do not exceed the size of the original design form. I have tried both screens. GetWorkingArea(this) and this.ClientSize for the height and width. The simple maths generates the correct values. Original: numofXlines 29 numofYlines 18 end of line dims xPos 1400 yPos 850. After resizing: numofXlines 51 numofYlines 27 ends of line dims xPos 2500 yPos 1400. The form resizes as I drag the corner or the t max button, but the redraw of the grid stops both horizontally and vertically at the boundaries of the design time form. I appreciate that is a nonproblem problem, the world works fine until I interfere, but this has me somewhat stumped.
c# .net8/9 vs2025 windows 11 Project is WinForms only
private void DrawGrid(Boolean Resize)
{
int NumOfYLines;
int NumOfXLines;
int GridHeight;
int GridWidth;
Pen ThisPen = new Pen(Color.LightSlateGray, 1);
if (!Resize)
{
GridHeight = this.Height;
GridWidth = this.Width;
}
else
{
GridHeight = this.ClientSize.Height;
GridWidth = this.ClientSize.Width;
// ThisPen = new Pen(Color.DarkRed, 1);
}
NumOfYLines = GridHeight / GridSize;
NumOfXLines = GridWidth / GridSize;
// Generate coordinates in loops
Array[,] GridCoordinates = new Array[NumOfXLines, NumOfYLines];
for (int x = 0; x < NumOfXLines; ++x)
{
int xPos = x * GridSize;
for (int y = 0; y < NumOfYLines; ++y)
{
int yPos = y * GridSize;
TheseGraphics.DrawLine(ThisPen, 0, yPos, GridWidth, yPos);
TheseGraphics.DrawLine(ThisPen, xPos, 0, xPos, NumOfXLines * GridSize);
GridCoordinates[x, y] = new int[xPos, yPos];
Misc.writeLog(
"xPos ::" + xPos.ToString() + " :: " +
"numofXlines :: " + NumOfXLines.ToString() + " :: " +
"yPos ::" + yPos.ToString() + " :: " +
"numofYlines :: " + NumOfYLines.ToString(), true
);
}
}
}
I have stepped through and can find no problem. The pen draws the line, but only that part is contained within the design time for shows. It develops no exceptions.
Daniel WrightPosted Feb 24, 2025, 12:14 PM
It seems like the issue you are facing with the grid not resizing beyond the original form boundaries when you redraw it after resizing the form can be attributed to how the grid drawing logic is being handled. Let's break down the possible reasons for this behavior and suggest some approaches to resolve it:
1. Grid Drawing Logic:
- The code snippet provided shows the drawing of grid lines using a Pen object. However, the coordinates for drawing these lines may not be getting recalculated correctly after the form is resized. This could lead to the grid lines being restricted within the original form boundaries.
2. Drawing Method Call:
- Ensure that the `DrawGrid` method is being called at the appropriate time after the form has been resized. If it's not called or not triggered correctly, the grid won't be redrawn with the updated dimensions.
3. Drawing Outside Form Bounds:
- When drawing the grid lines, make sure to consider the absolute coordinates relative to the form, not just the grid size. The starting point (0, 0) should be the top-left corner of the form, and the lines should extend beyond the current form size based on the calculated grid dimensions.
4. Redrawing Mechanism:
- Check if the grid is being redrawn in response to the actual form size change event. Ensure that the logic to redraw the grid considers the new form dimensions accurately.
To address these points, you can try the following:
- Recalculate the positions of the grid lines based on the updated form size (after resizing).
- Ensure that the grid lines are drawn using absolute form coordinates, allowing them to extend beyond the original form boundaries.
- Validate that the method responsible for redrawing the grid is triggered correctly after the form is resized.
By incorporating these adjustments into your drawing logic, you should be able to overcome the issue of the grid not extending beyond the initial form size when redrawn. If you encounter any specific challenges or need further assistance with the implementation, feel free to provide additional details or code snippets for a more tailored solution.
Matthew HessPosted Feb 25, 2025, 5:46 PM
Glad you got the main questions answered! Windows Custom Draw can be fun but sometimes a bit tricky.
For example, I wonder if your code above causes flickering as you resize the form. It depends on when you are calling DrawGrid. One thing I did notice: objects like System.Drawing.Pen implement IDisposable. This is because they wrap unmanaged GDI objects that need to be disposed. I believe your code above is leaking one GDI object per call. You can fix this easily by changing one line of code to:
using Pen ThisPen = new Pen(Color.LightSlateGray, 1);
The use of the "using" statement will cause the Pen to be disposed of once it goes out of scope.
Also, you should check out C# String Interpolation for those log messages. It can be written like this:
Misc.writeLog($"xPos :: {xPos} :: numofXlines :: { NumOfXLines} :: yPos :: {yPos} :: numofYlines :: {NumOfYLines}", true);
Which is not only easier to read, but much more efficient because you elimintate 4 calls to ToString() and five string allocations. :)
Have fun!
-Matthew
@CSharpArtisan
Peter ReillyPosted Feb 24, 2025, 2:27 PM
Thank Daniel.
Your answer caused me to check what I was drawing on.I define TheseGraphics against the original Form. It was necessary for me to redefine TheseGraphics against the resized Form. The form was resizing OK but the graphics surface defined by TheseGraphics was not. As I said, a nonproblem problem of my own making.
Thank you for the nudge in the right direction.