Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

i have three textboxes in my asp page with three colors.I want to assign textbox1 bgcolor to 2nd textbox and textbox2's color to 3rd textbox.... likewise i want to repeat a loop among the textboxes. Can any one help me in doing this task!



I want to change the bgcolor of each textbox with the color of previous textbox bgcolor and this should be happend for each 5 seconds
Posted
Updated 21-Jun-13 3:14am
v3
Comments
Mahesh Bailwal 21-Jun-13 8:28am    
Question is not clear to me. What do you want to accomplish from this. Why you want to change bgColor?
jemaritn80 21-Jun-13 8:29am    
I'm not sure what you're asking. Do you want to swap the colors periodically? Or have it look more like "chaser" mode on christmas lights? Is this ASP.Net, WPF, or Windows Forms? If ASP.Net does it need to be client side or server side?
keerthi cse 21-Jun-13 8:38am    
asp .net at client side
Mahesh Bailwal 21-Jun-13 8:58am    
you can use JavaScript for that. Below is sample.
var txt1bgColor= document.getElementById("<%=TextBox1.ClientID%>").style.backgroundColor;

document.getElementById("<%=TextBox2.ClientID%>").style.backgroundColor = txt1bgColor;
[no name] 21-Jun-13 8:50am    
You are not going to do this in C#, try javascript.

Hi,

As ThePhantomUpvoter said, you should try to do this with JavaScript, not with C#.
Try this:
ASP
<form runat="server">
    <asp:TextBox runat="server" BackColor="Red" ID="textbox1" />
    <asp:TextBox runat="server" BackColor="Green" ID="textbox2" />
    <asp:textbox runat="server" BackColor="Blue" ID="textbox3" />
</form>
<script type="text/javascript">
    var txtbox1 = document.getElementById("<%=textbox1.ClientID%>");
    var txtbox2 = document.getElementById("<%=textbox2.ClientID%>");
    var txtbox3 = document.getElementById("<%=textbox3.ClientID%>");
    setInterval(function () {
        prevColorTxt1 = txtbox1.style.backgroundColor;
        prevColorTxt2 = txtbox2.style.backgroundColor;
        prevColorTxt3 = txtbox3.style.backgroundColor;
        txtbox2.style.backgroundColor = prevColorTxt1;
        txtbox3.style.backgroundColor = prevColorTxt2;
        txtbox1.style.backgroundColor = prevColorTxt3;
    }, 5000, null);
</script>
More about the setInterval function:
https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval[^]

Hope this helps.
 
Share this answer
 
v2
C#
static int flag = 0;
    static int counter = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (flag == 0)
        {
            TextBox1.BackColor = System.Drawing.Color.Red;
            TextBox2.BackColor = System.Drawing.Color.Green;
            TextBox3.BackColor = System.Drawing.Color.Blue;

            flag = 1;
            counter = 1;
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (counter == 1)
        {
            TextBox1.BackColor = System.Drawing.Color.Blue;
            TextBox2.BackColor = System.Drawing.Color.Red;
            TextBox3.BackColor = System.Drawing.Color.Green;
            counter = 2;
        }
        else if (counter == 2)
        {
            TextBox1.BackColor = System.Drawing.Color.Green;
            TextBox2.BackColor = System.Drawing.Color.Blue;
            TextBox3.BackColor = System.Drawing.Color.Red;
            counter = 3;
        }
        else if (counter == 3)
        {
            TextBox1.BackColor = System.Drawing.Color.Red;
            TextBox2.BackColor = System.Drawing.Color.Green;
            TextBox3.BackColor = System.Drawing.Color.Blue;
            counter = 0;
            flag = 0;
        }
    }
 
Share this answer
 
Hi! I use Winform to do this task

private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();

}

void timer1_Tick(object sender, EventArgs e)
{
Color temp1 = textBox1.BackColor;
textBox1.BackColor = textBox3.BackColor;

Color temp2 = textBox2.BackColor;
textBox2.BackColor = temp1;

Color temp3 = textBox3.BackColor;
textBox3.BackColor = temp2;
}
 
Share this answer
 
v2
Comments
[no name] 21-Jun-13 10:03am    
For a web page?
keerthi cse 24-Jun-13 5:20am    
Thank you all

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