1

I am trying to create a elipse in a textbox on double click. But it doesnt seem to happen.

panel.MouseClick += create_terms;

private void create_terms(object sender, EventArgs arg)
{
    if (Phys_terms_check.Checked == true)
    {
         MouseEventArgs e = (MouseEventArgs)arg;
            Graphics g = CreateGraphics();
            SolidBrush p = new SolidBrush(Color.Red);
            Pen erase = new Pen(Color.White);

            Panel panel = (Panel)sender;
            g.FillEllipse(p, e.X+panel.Left,e.Y+panel.Top,10,10);
    }
}

The e.x and e.y seem to be giving relative coordinates from the sender. How to get point relative to the form.

Amarendra Reddy
  • 243
  • 2
  • 17
  • 1
    __NEVER__ use `Graphics g = CreateGraphics();` Instead add the point to a List and code the Paint event to draw ellipses at all those points using its e.Graphics event. After adding a point to the list call Invalidate on the control you want to draw on. Also: `TextBoxes` are not really happy if you want to draw on them; you can see that from the fact that __they don't even have a Paint event!!__ Better use a Panel or if you want text on it as well a Label. (Btw: Labels are greatly underrated) – TaW May 12 '16 at 10:52
  • Can u elaborate on how to call the paint event. I need to call this each time user doubleclicks on the panel. – Amarendra Reddy May 12 '16 at 11:05
  • If you want to use a Panel (or Label) simply double-click the Paint event in the Panel's Propertybox-Events-Pane! For [hooking up events](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=2|0.3896#33276161) read this. For drawing lists of ellipses [see this post](http://stackoverflow.com/questions/36924016/how-to-draw-multiple-ellipse-in-the-same-panel/36924235#36924235) - One more remark: the `Graphics g = CreateGraphics();` created a Graphics object you can use only to draw __non-persistent graphics__ onto the __Form__ – TaW May 12 '16 at 11:30

2 Answers2

0

add sender's top and left coordinates.

g.FillEllipse(p, e.X + textbox.Left, e.Y + textbox.Top, 10, 10);

but, this won't show, because textbox paint event fill fire and repaint textbox.

Nino
  • 6,931
  • 2
  • 27
  • 42
0

First of all: TextBoxes are old legacy and rather special Controls that do not support all things normal controls let you do.

Among the things that won't work are

  • Setting a BackgroundImage
  • Owner-drawing them

The latter includes any drawing in its Paint/OnPaint events.

You can code and hook-up the Paint event, but it won't get called.

You still can draw onto a TextBox using CreateGraphics, if you do it right, but as always with this function the result is non-persistent and will go away as soon as the system refreshes the TextBox itself, which is super-fast: as soon as you move your cursor over it the circle you draw may disappear..

The code to do it would have to look similar to this:

Graphics g = yourTextBox.CreateGraphics();
g.FillEllipse(Brushes.Red, yourTextBox.Width - 22, 2, 11, 11);

But as I said this will not persist, so it has little or no value.

If you want to draw onto something with a visible Text property you can use a Label:

private void yourLabel_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillEllipse(Brushes.Red, yourLabel.Width - 22, 2, 11, 11);
}

enter image description hereenter image description here

The result looks the same, but only the dot in the Label will persist, e.g. a Minimize-Maximize of the form.. In fact the dot in the TextBox didn't even survive calling my screenshot program, so I had use resort to pressing the Print-Key !

For drawing circles upon mouseclicks onto normal controls see this post!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111