Reputation: 77
how can i call a method with system arguments in a mouse click event:
public void DrawStringRectangleF(PaintEventArgs e)
{
// Create string to draw.
String drawString = "Sample Text";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create rectangle for drawing.
float x = 150.0F;
float y = 150.0F;
float width = 200.0F;
float height = 50.0F;
RectangleF drawRect = new RectangleF(x, y, width, height);
// Draw rectangle to screen.
Pen blackPen = new Pen(Color.Black);
e.Graphics.DrawRectangle(blackPen, x, y, width, height);
// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
}
i want to call this method in this event:
private void button1_Click(object sender, EventArgs e)
{
// calling DrawStringRectangleF(PaintEventArgs e) here
}
it's easy to call a method if the parameters are variables just by mentioning the values when calling these methods, but how to call it in such a case?!
Upvotes: 0
Views: 1016
Reputation: 39132
Here's an example using the Paint() event and a List of a custom class to store the information about what should be drawn:
public partial class Form1 : Form
{
public class TextData
{
public string Text;
public string FontName;
public int FontSize;
public RectangleF TextRectF;
public Color TextColor;
}
private List<TextData> Texts = new List<TextData>();
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(Form1_Paint);
TextData td = new TextData();
td.Text = "Example";
td.FontName = "Courier";
td.FontSize = 24;
td.TextRectF = new RectangleF(50.0F, 50.0F, 175.0F, 75.0F);
td.TextColor = Color.Red;
Texts.Add(td);
}
void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (TextData td in Texts)
{
using (Pen drawPen = new Pen(td.TextColor))
{
e.Graphics.DrawRectangle(drawPen, Rectangle.Round(td.TextRectF));
}
using (Font drawFont = new Font(td.FontName, td.FontSize))
{
using (SolidBrush drawBrush = new SolidBrush(td.TextColor))
{
e.Graphics.DrawString(td.Text, drawFont, drawBrush, td.TextRectF);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
TextData td = new TextData();
td.Text = "Sample Text";
td.FontName = "Arial";
td.FontSize = 16;
td.TextRectF = new RectangleF(150.0F, 150.0F, 200.0F, 50.0F);
td.TextColor = Color.Black;
Texts.Add(td);
this.Refresh();
}
}
Upvotes: 1
Reputation: 7804
In order to satisfy the existing method signature you could encapsulate the Graphics
instance that you wish to use to draw the rectangle within an instance of the PaintEventArgs
class like this
using (var controlGraphics = CreateGraphics())
{
var paintEventArgs = new PaintEventArgs(controlGraphics, ClientRectangle);
DrawStringRectangleF(paintEventArgs);
}
The Control.CreateGraphics
method will produce a Graphics
instance that you can use to paint on the underlying control (or form).
Understand that everything painted with that Graphics
object will be erased each time the control is invalidated so you must re-draw the graphic continuously.
You may consider adapting the method signature so that it accepts an instance of Graphics
instead which should make for a cleaner solution.
Upvotes: 3