Resolved PictureBox image getting cleared

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
95
Programming Experience
10+
I have a PictureBox that I drag from and drop onto another PictureBox. If I hit the TAB key, the Image in the PictureBox that was dropped on disappears. Any idea what Event I need to trap to prevent this? The picture below shows what happens after I have dropped the "Greater Than" Picture onto the SteelBlue PictureBox and then hit the TAB key. (Ctrl-Alt by the way also cleared the image).

I just found out some additional information. This ONLY happens right after I drop an image from the PictureBox to the SlateBlue PictureBox when the SlateBlue PictureBox has no image in it. If I drop another image on the SlateBlue PictureBox after it already has one, the TAB key does not clear that image. Really strange behavior.

1608744487027.png


I also just tried setting an initial image in the SlateBlue PictureBox and the TAB key still removed the image that was dropped...

1608747311015.png
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,025
Location
Chesapeake, VA
Programming Experience
10+
Without seeing your code and how you are handling various events, we would just be speculating.
 

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
95
Programming Experience
10+
Without seeing your code and how you are handling various events, we would just be speculating.
Sky,
Here it is:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLQueryBuilder
{
    public partial class frmCriteria : Form
    {
        public string sSQLQuery = "WHERE ";
        private string sSelectedCommand1 = "";
        private string sBaseQuery = "";

        public frmCriteria()
        {
            InitializeComponent();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            System.Drawing.Drawing2D.LinearGradientBrush gradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(this.ClientRectangle, Color.Black, Color.SteelBlue, 90);

            e.Graphics.FillRectangle(gradientBrush, this.ClientRectangle);
        }

        private void frmCriteria_Load(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            this.UpdateStyles();

            this.Text = "Crieria Builder for " + frmSQLQueryBuilder.sQueryField;
            this.sBaseQuery = "WHERE " + frmSQLQueryBuilder.sQueryField;

            this.pbLessThan.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbLessThan_MouseDown);
            this.pbLessThanEqual.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbLessThanEqual_MouseDown);
            this.pbEqual.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbEqual_MouseDown);
            this.pbGreaterThanEqual.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbGreaterThanEqual_MouseDown);
            this.pbGreaterThan.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbGreaterThan_MouseDown);
            this.pbAnd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbAnd_MouseDown);
            this.pbBetween.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbBetween_MouseDown);
            this.pbLike.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbLike_MouseDown);

            this.pbCommand1.AllowDrop = true;
            this.pbCommand2.AllowDrop = true;
            this.pbCommand1.DragDrop += new System.Windows.Forms.DragEventHandler(this.pbCommand1_DragDrop);
            this.pbCommand1.DragEnter += new System.Windows.Forms.DragEventHandler(this.pbCommand1_DragEnter);

            lblField.Text = frmSQLQueryBuilder.sQueryField;
            switch (frmSQLQueryBuilder.sQueryField)
            {
                case "Date":
                    txtLesserNum.Visible = false;
                    txtGreaterNum.Visible = false;
                    dtStartDateTime.Visible = true;
                    dtEndDateTime.Visible = false;
                    pbLike.Enabled = false;
                    pbLike.Image = SQLQueryBuilder.Properties.Resources.LikeGrey64;
                    break;
                default:
                    txtLesserNum.Visible = true;
                    txtGreaterNum.Visible = false;
                    dtStartDateTime.Visible = false;
                    dtEndDateTime.Visible = false;
                    break;
            }
            pbAnd.Enabled = false;
            pbAnd.Image = SQLQueryBuilder.Properties.Resources.PlusGrey64;
            lblSQLQueryString.Text = sBaseQuery;
        }

        private void pbLessThan_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Less Than";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbLessThanEqual_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Less Than or Equal";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbEqual_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Equal";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbGreaterThanEqual_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Greater Than or Equal";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbGreaterThan_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Greater Than";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbAnd_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "And";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbBetween_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Between";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbLike_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbSelected = (PictureBox)sender;
            sSelectedCommand1 = "Like";
            pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
        }

        private void pbCommand1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void pbCommand2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void pbCommand1_DragDrop(object sender, DragEventArgs e)
        {
            PictureBox picbox = (PictureBox)sender;
            Graphics g = picbox.CreateGraphics();
            g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(0, 0));
            lblCommand1.Text = sSelectedCommand1;

            switch (frmSQLQueryBuilder.sQueryField)
            {
                case "Date":
                    txtGreaterNum.Visible = false;
                    pbAnd.Enabled = false;
                    pbAnd.Image = SQLQueryBuilder.Properties.Resources.PlusGrey64;
                    pbCommand2.Visible = false;
                    lblCommand2.Visible = false;
                    dtEndDateTime.Visible = false;
                    switch(sSelectedCommand1)
                    {
                        case "Less Than":
                            lblSQLQueryString.Text = sBaseQuery + " < '" + dtStartDateTime.Text + "' ";
                            break;
                        case "Less Than or Equal":
                            lblSQLQueryString.Text = sBaseQuery + " <= '" + dtStartDateTime.Text + "' ";
                            break;
                        case "Equal":
                            lblSQLQueryString.Text = sBaseQuery + " = '" + dtStartDateTime.Text + "' ";
                            break;
                        case "Greater Than or Equal":
                            lblSQLQueryString.Text = sBaseQuery + " >= '" + dtStartDateTime.Text + "' ";
                            break;
                        case "Greater Than":
                            lblSQLQueryString.Text = sBaseQuery + " > '" + dtStartDateTime.Text + "' ";
                            break;
                        case "Between":
                            pbCommand2.Visible = true;
                            lblCommand2.Visible = true;
                            dtEndDateTime.Visible = true;
                            lblCommand2.Text = "And";
                            pbCommand2.Image = SQLQueryBuilder.Properties.Resources.Plus64;
                            lblSQLQueryString.Text = sBaseQuery + " BETWEEN '" + dtStartDateTime.Text + "' AND '" + dtEndDateTime.Text + "' ";
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    pbCommand2.Visible = false;
                    txtGreaterNum.Visible = false;
                    dtEndDateTime.Visible = false;
                    lblCommand2.Visible = false;
                    pbAnd.Enabled = false;
                    pbAnd.Image = SQLQueryBuilder.Properties.Resources.PlusGrey64;
                    switch(sSelectedCommand1)
                    {
                        case "Less Than":
                            lblSQLQueryString.Text = sBaseQuery + " < '" + txtLesserNum.Text + "' ";
                            break;
                        case "Less Than or Equal":
                            lblSQLQueryString.Text = sBaseQuery + " <= '" + txtLesserNum.Text + "' ";
                            break;
                        case "Equal":
                            lblSQLQueryString.Text = sBaseQuery + " = '" + txtLesserNum.Text + "' ";
                            break;
                        case "Greater Than or Equal":
                            lblSQLQueryString.Text = sBaseQuery + " >= '" + txtLesserNum.Text + "' ";
                            break;
                        case "Greater Than":
                            lblSQLQueryString.Text = sBaseQuery + " > '" + txtLesserNum.Text + "' ";
                            break;
                        case "Between":
                            pbCommand2.Visible = true;
                            lblCommand2.Visible = true;
                            lblCommand2.Text = "And";
                            txtGreaterNum.Visible = true;
                            pbCommand2.Image = SQLQueryBuilder.Properties.Resources.Plus64;
                            lblSQLQueryString.Text = sBaseQuery + " BETWEEN '" + txtLesserNum.Text + "' AND '" + txtGreaterNum.Text + "' ";
                            break;
                        case "Like":
                            lblSQLQueryString.Text = sBaseQuery + " LIKE '%" + txtLesserNum.Text + "%' ";
                            break;
                    }
                    break;
            }
        }

        private void ttControl_Draw(object sender, DrawToolTipEventArgs e)
        {
            Font tooltipFont = new Font("Microsoft Sans Serif", 10.8f, FontStyle.Bold);
            e.DrawBackground();
            e.DrawBorder();
            e.Graphics.DrawString(e.ToolTipText, tooltipFont, Brushes.Black, new PointF(4, 2));
        }

        private void ttControl_Popup(object sender, PopupEventArgs e)
        {
            Size newSize;
            newSize = TextRenderer.MeasureText(ttControl.GetToolTip(e.AssociatedControl), new Font("Microsoft Sans Serif", 10.8f, FontStyle.Bold), new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPadding);
            newSize = new Size(newSize.Width, newSize.Height + 6);
            e.ToolTipSize = newSize;
        }

        private void txtLesserNum_TextChanged(object sender, EventArgs e)
        {
            switch (sSelectedCommand1)
            {
                case "Less Than":
                    lblSQLQueryString.Text = sBaseQuery + " < '" + txtLesserNum.Text + "' ";
                    break;
                case "Less Than or Equal":
                    lblSQLQueryString.Text = sBaseQuery + " <= '" + txtLesserNum.Text + "' ";
                    break;
                case "Equal":
                    lblSQLQueryString.Text = sBaseQuery + " = '" + txtLesserNum.Text + "' ";
                    break;
                case "Greater Than or Equal":
                    lblSQLQueryString.Text = sBaseQuery + " >= '" + txtLesserNum.Text + "' ";
                    break;
                case "Greater Than":
                    lblSQLQueryString.Text = sBaseQuery + " > '" + txtLesserNum.Text + "' ";
                    break;
                case "Between":
                    lblSQLQueryString.Text = sBaseQuery + " BETWEEN '" + txtLesserNum.Text + "' ";
                    break;
                case "Like":
                    lblSQLQueryString.Text = sBaseQuery + " LIKE '%" + txtLesserNum.Text + "%' ";
                    break;
            }
        }

        private void txtGreaterNum_TextChanged(object sender, EventArgs e)
        {
            lblSQLQueryString.Text = sBaseQuery + " BETWEEN '" + txtLesserNum.Text + "' AND '" + txtGreaterNum.Text + "' ";
        }
    }
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,265
Location
Norway
Programming Experience
10+
You have to either set the Image or use Paint event to draw. Using CreateGraphics is not lasting, it is wiped when control redraws for any reason.
 

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
95
Programming Experience
10+
You have to either set the Image or use Paint event to draw. Using CreateGraphics is not lasting, it is wiped when control redraws for any reason.
John,
Setting the PictureBox image worked like a charm!

C#:
PictureBox picbox = (PictureBox)sender;
picbox.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
 
Top Bottom