All the good time. The question is: I use Graphics to draw multiple outlines (PNG format) on the PictureBox. Every time I click on RadioBtn, the image is redrawn according to the picture whose RadioBtn was clicked. The problem is that the redrawn outlines flicker as long as you hold the LMB, then disappear. I probably wrote some trifle not there, but I can't find it. Sobsno:

try { Image fon=Image.FromFile($"styles/{selectedRadioButton_Text}.png"); pbx_StylePic.BackgroundImage = fon; Graphics gr = pbx_StylePic.CreateGraphics(); for (int i = 0; i < OrderData.Styles.Count; i++) { if (OrderData.Styles[i] != "") { Image im = Image.FromFile($"styles/shirt/{OrderData.Styles[i]}.png"); gr.DrawImage(im, new Point(0, 0)); } } } 

Tell me where confused? Thank!

  • Yes WF. The event occurs when you click on the radio button. In principle, the solution has already been found: Refresh () pixer - Sergey
  • Event Click (LMB) - Sergey
  • moved comments in response. Transfer the clarifications from the comments to the question. - rdorn
  • one
    "trifle" is that you need to paint in the Paint event (and use Graphics from its arguments), and not some left-handed events, then there will be no such problems - MSDN.WhiteKnight
  • @VadimTagil is not always possible, although I partially agree with you, but only partially, because sometimes it is easier and more correct to override the inherited OnPant, or, as it is, draw as needed. It all depends ... I will not remember the patterns, and without them the theme is holivar. - rdorn

2 answers 2

When you click on the radio button, several events occur, you just need to use the correct one. Use the CheckedChanged event instead of a Click .

 private void radioButton1_CheckedChanged(Object sender, EventArgs e) { var rb = (RadioButton)sender; if (rb.Checked) { //ваш код } } 

It will work strictly once when the button is turned on. When you click on the included button, there will be no rerun, when you remove the selection from the button, too.


In general, according to the mind, you do not have to draw by hand. I don’t know what exactly you are drawing there, and you didn’t write it, but I would do it differently:

  • pictures put in resources
  • radio buttons choose the desired image from the collection (by index or name does not matter)
  • The control that displays the image takes it by that name / index at the time of the redraw in the Paint event or the inherited overridden OnPaint method if the control is not standard, but inherited.

Thus, it is not necessary to catch errors associated with redrawing the window after minimizing to tray, resizing, etc.

  • comment about the minus will be? - rdorn
  • If a button is dynamically created, how to properly assign an event? So: RB.CheckedChanged + = new EventHandler (onRBstyleClick)? - Sergey
  • one
    @ Sergey right after creation, you should create it before adding, it is at this moment that you attach the handler - rdorn
  • Error writes: Cannot convert type "System.Windows.Forms.MouseEventHandler" to "System.EventHandler" - Sergey
  • one
    @ Sergey set the correct type to the handler, debugger to help - rdorn
  try { Graphics gr = pbx_StylePic.CreateGraphics(); pbx_StylePic.Refresh(); foreach (string s in OrderData.Styles) { if (s != "") { Image im = Image.FromFile($"styles/shirt/{s}.png"); gr.DrawImage(im, new Point(0, 0)); } } }