-3

By clear i mean to redraw or paint or color the control back to it's original. This is the working code:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace FTP_ProgressBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            txtHost.TextChanged += anyTextBox_TextChanged;
            txtUploadFile.TextChanged += anyTextBox_TextChanged;
            txtDir.TextChanged += anyTextBox_TextChanged;

            anyTextBox_TextChanged(null, null);

            if ((txtHost.Text == "") || txtUploadFile.Text == "")
            {
                btnUpload.Enabled = false;
            }

            if (txtDir.Text == "")
            {
                checkBox1.Enabled = false;
            }
        }

        private void anyTextBox_TextChanged(object sender, EventArgs e)
        {
            btnUpload.Enabled = txtHost.TextLength > 0 && txtUploadFile.TextLength > 0;
            checkBox1.Enabled = txtDir.TextLength > 0;
            this.Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if(this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
                this.txtUploadFile.Text = this.openFileDialog1.FileName;
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            if(this.ftpProgress1.IsBusy)
            {
                this.ftpProgress1.CancelAsync();
                this.btnUpload.Text = "Upload";
            }
            else
            {
                FtpSettings f = new FtpSettings();
                f.Host = this.txtHost.Text;
                f.Username = this.txtUsername.Text;
                f.Password = this.txtPassword.Text;
                f.TargetFolder = this.txtDir.Text;
                f.SourceFile = this.txtUploadFile.Text;
                f.Passive = this.chkPassive.Checked;
                try
                {
                    f.Port = Int32.Parse(this.txtPort.Text);
                }
                catch { }
                this.toolStripProgressBar1.Visible = true;
                this.ftpProgress1.RunWorkerAsync(f);
                this.btnUpload.Text = "Cancel";
            }
        }

        private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.toolStripStatusLabel1.Text = e.UserState.ToString();   // the message will be something like: 45 Kb / 102.12 Mb
            this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);      
        }

        private void ftpProgress1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Error != null)
                MessageBox.Show(e.Error.ToString(), "FTP error");
            else if(e.Cancelled)
                this.toolStripStatusLabel1.Text = "Upload Cancelled";
            else
                this.toolStripStatusLabel1.Text = "Upload Complete";
            this.btnUpload.Text = "Upload";
            this.toolStripProgressBar1.Visible = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Pen penBorder;
            if (txtHost.TextLength <= 0)
            {
                penBorder = new Pen(Color.Red, 3);
                e.Graphics.DrawRectangle(penBorder, txtHost.Location.X, txtHost.Location.Y, txtHost.Width - 1, txtHost.Height - 1);
            }
            if (txtUploadFile.TextLength <= 0)
            {
                penBorder = new Pen(Color.Red, 3);
                e.Graphics.DrawRectangle(penBorder, txtUploadFile.Location.X, txtUploadFile.Location.Y, txtUploadFile.Width - 1, txtUploadFile.Height - 1);
            }
        }
    }
}

I saw now that without a breakpoint if i minimize form1 when the program is running after typed text in both textBoxes and then resize the form1 it does clear the rectangles.

Strange it seems that it's taking effect only when i minimize and resize back the form1.

In the TextChanged event i tried to add: txtHost.Invalidate(); but it didn't help. The only way that the rectangle get clear is if i minmize and resize back form1.

Or adding this.Invalidate(); did the trick.

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • Ah i found now that it's working if i'm adding this line to the TextChanged event: this.Invalidate(); But is it ok to redraw all the form ? – Daniel Lip Nov 11 '14 at 18:03
  • possible duplicate of [How do i call OnPaint event from textbox textchanged event?](http://stackoverflow.com/questions/26859305/how-do-i-call-onpaint-event-from-textbox-textchanged-event) – Peter Duniho Nov 11 '14 at 18:20

1 Answers1

1

OnPaint() only gets called when the window needs to be updated. This is a basic principle about how Windows works. If you need the window to be updated now then, yes, you need to invalidate the window so that OnPaint() will be called.

But is it ok to redraw all the form?

Sure, but it's not very performant as you are redrawing areas that don't necessarily need redrawing. Invalidate() should have a version that accepts a rectangle argument. Use it to only invalidate the area you want to update.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466