Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextBox of user control NavigationBar gives flickering during reducting the height of the form.

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
I have a form with specific minimum size 1100 x 800. My form has labels and textboxes in order to display all customers of a microsoft sql server database table.
I also have a user control class which is a navigation bar for the records.
The following code is my NavigationBar control class.
Code:
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;
using System.Windows.Forms;

namespace AngelTechShop
{
    public partial class NavBar : UserControl
    {
        public NavBar()
        {
            InitializeComponent();
        }

      public partial class RecordTxtBox : TextBox
        {
           
            //constructor
            public RecordTxtBox()
            {
                Font = new Font("Tahoma", 15, System.Drawing.FontStyle.Bold);
                TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
                BackColor = Color.White;
                ForeColor = Color.Blue;
                Anchor = AnchorStyles.Top | AnchorStyles.Left;
                Size = new System.Drawing.Size(563, 33);

            }
            protected internal void ShowPosition(ref CurrencyManager cm)
            {
                this.ReadOnly = true;
                this.MouseUp += TextBoxControl_MouseUp;
                this.Enabled = false;

                this.BackColor = System.Drawing.Color.White;
                this.ForeColor = System.Drawing.Color.Blue;
                this.Text = cm.Position + 1 + " από " + cm.Count.ToString() + " εγγραφές";
                this.TabStop = false;
            }

            private void TextBoxControl_MouseUp(object sender, MouseEventArgs e)
            {
                this.SelectionLength = 0;

            }

            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);

                RecordTxtBox RcrdTxtBx = new RecordTxtBox();


                SolidBrush drawBrush = new SolidBrush(ForeColor);
                // Draw string to screen.
                //e.Graphics.DrawString(Text, Font, drawBrush, 0f, 0f);


                e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow), 0.0F, 0.0F, this.Width, this.Height);
                //e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Blue), e.ClipRectangle, new StringFormat());
                // Measure text

                var textSize = TextRenderer.MeasureText(RcrdTxtBx.Text, RcrdTxtBx.Font);
                if (e.ClipRectangle.Width > textSize.Width)
                {
                    var x = e.ClipRectangle.Width / 2 - textSize.Width;
                    var rect = new Rectangle(x, 0, textSize.Width, textSize.Height);

                    TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
                               TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;

                    TextRenderer.DrawText(e.Graphics, Text, Font, e.ClipRectangle, ForeColor, flags);
                }
            }

            protected override void InitLayout()
            {
                base.InitLayout();
                this.ReadOnly = true;
            }


            protected override void OnGotFocus(System.EventArgs e)
            {
                base.OnGotFocus(e);
                // HACK: Nasty and don't like this....'     : Scrollable though :/
                this.Enabled = false;
                this.Enabled = true;
            }
            protected override void OnEnabledChanged(EventArgs  e)
            {
                if (this.Enabled)
                {
                    this.SetStyle(ControlStyles.UserPaint, false);
                    this.Invalidate();
                }
                else
                    this.SetStyle(ControlStyles.UserPaint, true);
                base.OnEnabledChanged(e);
            }
        }

        public Button FrstBtn, NxtBtn, PrvBtn, LastBtn;
        //public TextBox RcrdTxtBx;
        
        public RecordTxtBox RcrdTxtBx = new RecordTxtBox(); 

        public Panel Pnl;
        
        public void CreateButton(ref Button btn,string name,Action<object, EventArgs> click)
        {
            btn.Name = $"Btn_{name}";
            btn.Click += new EventHandler(click);
        }

        public void CreateNavigationBar(CurrencyManager cm)
        {
           
            Pnl = new Panel();
            Pnl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            Pnl.Size = new System.Drawing.Size(720, 35);
            Pnl.Location = new System.Drawing.Point(0, 0);
            Pnl.BorderStyle = BorderStyle.Fixed3D;
            //CreateButtons();

            FrstBtn = new Button
            {
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                BackColor = System.Drawing.SystemColors.GradientInactiveCaption,
                BackgroundImage = Properties.Resources.Actions_go_first_view_icon,
                BackgroundImageLayout = ImageLayout.Stretch,
                Anchor = AnchorStyles.Top | AnchorStyles.Left,
                Size = new System.Drawing.Size(40, 33),
                Location = new System.Drawing.Point(0, 3)         
            };
            CreateButton(ref FrstBtn,"FrstBtn", (s, e) => FrstBtn_Click(s, e, cm));


            PrvBtn = new Button
            {
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                BackColor = System.Drawing.SystemColors.GradientInactiveCaption ,
                BackgroundImage = Properties.Resources.Oxygen_Icons_org_Oxygen_Actions_go_previous_view,
                BackgroundImageLayout = ImageLayout.Stretch,
                Anchor = AnchorStyles.Top | AnchorStyles.Left,
                Size = new System.Drawing.Size(40, 33),
                Location = new System.Drawing.Point(39, 3)               
            };
            CreateButton(ref PrvBtn,"PrvBtn", (s, e) => PrvBtn_Click(s, e,cm));

            RcrdTxtBx = new RecordTxtBox();
            
            RcrdTxtBx.Location = new System.Drawing.Point(79, 3);
            
            //RcrdTxtBx = new TextBox
            //{
            //    Font = new Font("Tahoma", 15, System.Drawing.FontStyle.Bold),
            //    TextAlign = System.Windows.Forms.HorizontalAlignment.Center,
            //    BackColor = Color.White,
            //    ForeColor = Color.Blue,
            //    Anchor = AnchorStyles.Top | AnchorStyles.Left,
            //    Size = new System.Drawing.Size(563, 33),
            //    Location = new System.Drawing.Point(79, 3)

            //   // BorderStyle=BorderStyle.FixedSingle               
            //};
            NxtBtn = new Button
            {
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                BackColor = System.Drawing.SystemColors.GradientInactiveCaption,
                BackgroundImage = Properties.Resources.Actions_go_next_view_icon,
                BackgroundImageLayout = ImageLayout.Stretch,
                Anchor = AnchorStyles.Top | AnchorStyles.Left,
                Size = new System.Drawing.Size(40, 33),
                Location = new System.Drawing.Point(641, 3)
            };

            CreateButton(ref NxtBtn, "NxtBtn", (s, e) => NxtBtn_Click(s, e,cm));

            LastBtn = new Button
            {
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                BackColor = System.Drawing.SystemColors.GradientInactiveCaption,
                BackgroundImage = Properties.Resources.Oxygen_Icons_org_Oxygen_Actions_go_last_view,
                BackgroundImageLayout = ImageLayout.Stretch,
                Anchor = AnchorStyles.Top | AnchorStyles.Left,
                Size = new System.Drawing.Size(40, 33),
                Location = new System.Drawing.Point(680, 3)
            };
            CreateButton(ref LastBtn,"LastBtn", (s, e) => LastBtn_Click(s, e,cm));

            //this.Size = new Size(Pnl.Size.Width+3, Pnl.Size.Height+2);
            this.Size = new System.Drawing.Size(Pnl.Size.Width, Pnl.Size.Height+3);
            this.Location = new System.Drawing.Point(0, 0);
            this.Location = new System.Drawing.Point(0, 0);

            this.AutoSize = true;
            this.Pnl.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
            this.FrstBtn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.PrvBtn.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.RcrdTxtBx.Anchor= AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
            this.NxtBtn.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 
            this.LastBtn.Anchor= AnchorStyles.Bottom | AnchorStyles.Right;
            this.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
            
            Pnl.Controls.Add(FrstBtn);
            Pnl.Controls.Add(PrvBtn);
            Pnl.Controls.Add(RcrdTxtBx);
            Pnl.Controls.Add(NxtBtn);
            Pnl.Controls.Add(LastBtn);
        }
        

        private void FrstBtn_Click(object sender, EventArgs e, CurrencyManager cm)
        {
            cm.Position = 0;
            RcrdTxtBx.ShowPosition(ref cm);
        }

        private void LastBtn_Click(object sender, EventArgs e, CurrencyManager cm)
        {
            cm.Position = cm.Count - 1;
            RcrdTxtBx.ShowPosition(ref cm);
        }

        private void NavBar_Load(object sender, EventArgs e)
        {
          
        }

        private void PrvBtn_Click(object sender, EventArgs e, CurrencyManager cm)
        {
            cm.Position -= 1;
            RcrdTxtBx.ShowPosition(ref cm); 
        }

        private void NxtBtn_Click(object sender, EventArgs e,  CurrencyManager cm)
        {
            cm.Position += 1;
            this.RcrdTxtBx.ShowPosition(ref cm);   
        }

       
    }
}

Here is my entire code of the form to display all Customers
Code:
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;
using System.Data.SqlClient;
using System.Configuration;

namespace AngelTechShop.Forms
{
    public partial class ViewCustomersForm : Form
    {
        SqlConnection con = new SqlConnection();
        string sql;
        SqlDataAdapter da;
        DataSet ds;
        DataView dv;
        CurrencyManager cm;
       
        public int LocX, LocY;
        public NavBar MyNavBar;
        public int initialFormHeight, initialFormWidth;
       
        public ViewCustomersForm()
        {
            InitializeComponent();
            
            
            //MyNavBar = new NavBar();
            //MyNavBar.CreateNavigationBar(ref cm);

            //MyNavBar.Dock = DockStyle.Bottom;
            //MyNavBar.Pnl.Dock = DockStyle.Bottom;
        
            //this.Controls.Add(MyNavBar);
            //this.Controls.Add(MyNavBar.Pnl);
            //initialFormHeight = this.Height;
            //initialFormWidth = this.Width;

            con.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
            if (con.State == ConnectionState.Closed)
               con.Open();
        }
        public void FromUControl()
        {
            Point LocalMousePosition;
            LocalMousePosition = this.PointToClient(Cursor.Position);
            //textBox1.Text = ("X=" + LocalMousePosition.X + "," + "Y= " + LocalMousePosition.Y);
        }
        private void RecordNumberTxtBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Point LocalMousePosition;
            LocalMousePosition = this.PointToClient(Cursor.Position);
            //textBox1.Text = ("X=" + LocalMousePosition.X + "," + "Y= " + LocalMousePosition.Y);
        }
       
        private void Navigate()
        {
            FormTextBoxes MyTextBoxes = new FormTextBoxes();
            MyTextBoxes.DataBindings.Clear();
            
            CustomerIDTxt.DataBindings.Add("Text", dv, "CustID");
            AfmTxt.DataBindings.Add("Text", dv, "CustAFM");
            DoyTxt.DataBindings.Add("Text", dv, "CustDOY");
            CustomerNameTxt.DataBindings.Add("Text", dv, "CustName");
            CustomerAddressTxt.DataBindings.Add("Text", dv, "CustAddress");
            CustomerProfessionTxt.DataBindings.Add("Text", dv, "CustProfession");
            CustomerCityTxt.DataBindings.Add("Text", dv, "CustCity");
            CustomerHomePhoneTxt.DataBindings.Add("Text", dv, "CustHomePhone");
            CustomerPrefectureTxt.DataBindings.Add("Text", dv, "CustPrefecture");
            CustomerMobilePhoneTxt.DataBindings.Add("Text", dv, "CustMobilePhone");
            CustomerPostCodeTxt.DataBindings.Add("Text", dv, "CustPostCode");
            CustomerEmailTxt.DataBindings.Add("Text", dv, "CustEmail");
            CustomerFloorTxt.DataBindings.Add("Text", dv, "CustFloor");
        }
        private void CustomersViewForm_Load(object sender, EventArgs e)
        {

            LoadTheme();

            initialFormWidth = this.Width;
            initialFormHeight = this.Height;

            
            //////souper na mi ta svisw apla einai gia xeiropoihth mpara kai oxi usercontrol
            //MyNavigationBar.Dock = DockStyle.Bottom;
            //int pW, pH;
            //pW = MyNavigationBar.Width;
            //pH = MyNavigationBar.Height;


            //FirstBtn1.Dock = DockStyle.Left;
            //FirstBtn1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            //PreviousBtn1.Dock = DockStyle.Left;
            //PreviousBtn1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;

            //NextBtn1.Dock = DockStyle.Right;
            //NextBtn1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            //LastBtn1.Dock = DockStyle.Right;
            //LastBtn1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            //RecordNumberTxtBox1.Dock = DockStyle.Left;
            //RecordNumberTxtBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;

            //NewNxtBLocX = NextBtn1.Location.X;

            //int ActualTxtWidth = TxtWidth - RecordNumberTxtBox1.Location.X;
            //LocX = RecordNumberTxtBox1.Location.X;
            //LocY = TxtLocY;


            //sql = "Select * from Customers";
            //da = new SqlDataAdapter(sql, con);
            //ds = new DataSet();
            //da.Fill(ds, "Customers");
            //dv = new DataView(ds.Tables["Customers"]);
            //cm = (CurrencyManager)this.BindingContext[dv];
            //MyNavBar.SetValues(ref cm, ref dv);
            //Navigate();
            //ShowPosition();
        }
        public void ResizeNavBar()
        {


            if (this.Width >= MyNavBar.Width) //&& (this.Height == initialFormHeight))
            {
                MyNavBar.Pnl.Size = new Size(MyNavBar.Pnl.Size.Width - 9, MyNavBar.Size.Height - 1);

                MyNavBar.Pnl.Refresh();

                MyNavBar.LastBtn.Location = new Point(MyNavBar.Pnl.Width - (MyNavBar.LastBtn.Width + 4), MyNavBar.LastBtn.Location.Y);

                MyNavBar.NxtBtn.Location =
                    new Point(MyNavBar.LastBtn.Location.X - (MyNavBar.NxtBtn.Width - 1), MyNavBar.NxtBtn.Location.Y);
                int StartPoint = (MyNavBar.PrvBtn.Location.X + MyNavBar.PrvBtn.Width)-2;
                int TargetPoint = MyNavBar.NxtBtn.Location.X-2;
                int NewWidth = TargetPoint - StartPoint;

                
                MyNavBar.RcrdTxtBx.Size = new Size(NewWidth, MyNavBar.Size.Height);


                MyNavBar.RcrdTxtBx.Location = new Point(MyNavBar.RcrdTxtBx.Location.X , MyNavBar.RcrdTxtBx.Location.Y);

               
            }
            else if (this.Width < initialFormWidth)//MyNavBar.Width) //&& (this.Height<MyNavBar.Height))
            {
                MyNavBar.Pnl.Size = new Size(this.Size.Width - 9, MyNavBar.Size.Height - 1);

                MyNavBar.Pnl.Refresh();

                MyNavBar.LastBtn.Location = new Point(MyNavBar.Pnl.Width - (MyNavBar.LastBtn.Width + 4), MyNavBar.LastBtn.Location.Y);

                MyNavBar.NxtBtn.Location =
                    new Point(MyNavBar.LastBtn.Location.X - (MyNavBar.NxtBtn.Width - 1), MyNavBar.NxtBtn.Location.Y);
                
                int WStartPoint =( MyNavBar.PrvBtn.Location.X + MyNavBar.PrvBtn.Width) - 2;
                int WTargetPoint = MyNavBar.NxtBtn.Location.X-2;
                int HStartPoint=this.Height-((MyNavBar.PrvBtn.Location.Y + MyNavBar.PrvBtn.Height) - 2);
                int HTargetPoint = MyNavBar.NxtBtn.Location.Y - 2;

                int NewWidth = WTargetPoint - WStartPoint;
                int NewHeight = HTargetPoint - HStartPoint;
                
                MyNavBar.RcrdTxtBx.Size = new Size(NewWidth, MyNavBar.Size.Height);


                MyNavBar.RcrdTxtBx.Location = new Point(MyNavBar.RcrdTxtBx.Location.X , MyNavBar.RcrdTxtBx.Location.Y);

                if (this.Height < MyNavBar.Height)
                    MyNavBar.SetBounds(MyNavBar.Location.X, HStartPoint, MyNavBar.Width, MyNavBar.Height);
            }
        }
        private void CustomersViewForm_Shown(object sender, EventArgs e)
        {
            sql = "Select * from Customers;";
            da = new SqlDataAdapter(sql, con);
            ds = new DataSet();
            da.Fill(ds, "Customers");
            dv = new DataView(ds.Tables["Customers"]);
            cm = (CurrencyManager)this.BindingContext[dv];

            MyNavBar = new NavBar();
            MyNavBar.CreateNavigationBar(cm);

            MyNavBar.Dock = DockStyle.Bottom;
            MyNavBar.Pnl.Dock = DockStyle.Bottom;

            this.Controls.Add(MyNavBar);
            this.Controls.Add(MyNavBar.Pnl);
            initialFormHeight = this.Height;
            initialFormWidth = this.Width;

            
            Navigate();
            MyNavBar.RcrdTxtBx.ShowPosition(ref cm);
            
            //MyNavBar.cm = (CurrencyManager)this.BindingContext[dv];
            ////MyNavBar.SetValues(ref cm, ref dv);
            //Navigate();
            //MyNavBar.ShowPosition();
        }

        
        private void CustomersViewForm_SizeChanged(object sender, EventArgs e)
        {
            initialFormWidth = this.Width;
            initialFormHeight = this.Height;
            //if (this.WindowState is FormWindowState.Normal)
            //ResizeNavBar();
        }

       
        private void LoadTheme()
        {
            foreach (Control btns in this.Controls)
            {
                if (btns.GetType() == typeof(Button))
                {
                    Button btn = (Button)btns;
                    if (btn.BackgroundImage == null)
                    {
                        btn.BackColor = ThemeColor.PrimaryColor;
                        btn.ForeColor = Color.White;
                        //btn.Font = new Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular);
                        btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
                        lblTitle.ForeColor = ThemeColor.PrimaryColor;
                    }
                }
            }
        }
    }
}
As everything works fine, it's time to mention my problem now. During runtime when the user reducts the height of the form fastly to the minimum form height, I got a flickering to the textbox of navigation bar which is RcrdTxtBx in the NavBar class I added above until the user let the key mouse button up. What can I do to avoid that? Any help and explanation will be much appreciated. Thank you so much in advanced.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top