Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there, thanks for the answers. What if i use a storyboard for the border color change and play the story board onTextChanged? Is this a bad way of doing it?


Hi there, i was wondering if someone can assist with tips on the following:

1. I added a listbox with five listbox items (Name of a Color)
2. I added a textbox and bound it to the listbox selected item which displays the Name of the color in the textbox.

The above works fine.

What i want to know is this:

Using data or normal triggers: If the listbox selection changes, and the textbox text updates to the new selected value, i want the border of the textbox to simply flash red once to draw the user's attention to the text box that changed.


Thank you in advance!

Posted
Updated 30-Dec-10 10:58am
v3

What you would want to do is hook the textbox's TextChanged event. Because you've databound that textbox, the listbox has no knowledge of the textbox, so that's where you would have to start.

So, in that event, you would just draw a box around the textbox with the color that is in the textbox.

You don't need to use a UserControl.

Something like this:

C#
private void textBox2_TextChanged(object sender, EventArgs e)
{
    Thread t = new Thread(new ThreadStart(FlashBorder));
    t.Start();
}

private void FlashBorder()
{
    Color color = Color.FromName(textBox2.Text);

    using (Graphics g = this.CreateGraphics())
    {
        Pen pen = new Pen(color);
        pen.Width = 2;

        Rectangle rect = new Rectangle(textBox2.Left - 1, textBox2.Top - 1, 
                                       textBox2.Width + 2, textBox2.Height + 2);

        Rectangle invalidateRect = new Rectangle(rect.Left - 1, rect.Top - 1,
                                                 rect.Width + 2, rect.Height + 2);

        for (int i = 0; i <= 5; i++)
        {
            //draw the box
            g.DrawRectangle(pen, rect);

            //leave it there for a little while
            Thread.Sleep(300);

            //clear the box
            this.Invalidate(invalidateRect);

            //wait awhile before continuing
            Thread.Sleep(300);
        }
    }

    //clear the box
    this.Invalidate();
}
 
Share this answer
 
Comments
fjdiewornncalwe 30-Dec-10 12:53pm    
Starting a new thread to do the FlashBorder may be overkill, but it is a very good solution... (5)
Adriaan 30-Dec-10 17:06pm    
Hi there, thanks for the answers. What if i use a storyboard for the border color change and play the story board onTextChanged? Is this a bad way of doing it?
William Winner 30-Dec-10 17:09pm    
A storyboard should work fine for your needs...though I've never used one.
Hi,

One way to do this is to create your own usercontrol to extend the standard textbox, replace the .Net TextBox and put yours instead. In the set of the Text property you can do what ever you fancy.

 public partial class MyTextBox : UserControl
 {
     public MyTextBox()
     {
         this.Resize += new EventHandler(MyTextBox_Resize);
         textBox.Multiline = true;
         textBox.BorderStyle = BorderStyle.Fixed3D;
         this.Controls.Add(textBox);
         timer.Tick += new EventHandler(t_Tick);
         timer.Interval = 500;
     }

     TextBox textBox = new TextBox();
     Timer timer = new Timer();

     public string Text
     {
         get { return textBox.Text; }
         set
         {
             textBox.Text = value;
             Graphics graphics = this.CreateGraphics();
             ControlPaint.DrawBorder(graphics, this.ClientRectangle, Color.Transparent, ButtonBorderStyle.None);
             timer.Start();
         }
     }

     void t_Tick(object sender, EventArgs e)
     {
         Graphics graphics = this.CreateGraphics();
         ControlPaint.DrawBorder(graphics, this.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
         timer.Stop();
     }

     private void MyTextBox_Resize(object sender, EventArgs e)
     {
         textBox.Size = new Size(this.Width - 2, this.Height - 2);
         textBox.Location = new Point(1, 1);
     }
}


Is that answering your question?

Valery.
 
Share this answer
 
Hi there, thanks for the answers. What if i use a storyboard for the border color change and play the story board onTextChanged? Is this a bad way of doing it?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900