Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Stivan Radev
Stivan Radev
1,475 Points

C#, Run timer after once it reaches X seconds. [Continues Loop until It meets requirements]

Hello,

So I'm trying to put a timer that runs in a loop and checkes if SOMETHING meets the requirements. If it does, DO AN ACTION IF IT MEETS THE REQUIREMENTS, if it does not, RUN THE TIMER AGAIN AND CHECK IF IT MEETS THE REQUIREMENTS

Here's where Im at right now:

public void isonline()
        {
            var login_cradentials = "pass";
            TextBox text_box = new TextBox();
            text_box.Location = new Point(textBox_resized_width, textBox_resized_height);

            Controls.Add(text_box);
            if (text_box.Text == login_cradentials)
            {
                TextBox text_box2 = new TextBox();
                text_box2.Location = new Point(15, 15);

                Controls.Add(text_box2);
            } else
            {
                TextBox text_box3 = new TextBox();
                text_box3.Location = new Point(15, 15);
                text_box3.Text = "PROBLEM";
                Controls.Add(text_box3);
            }
        }

(((text_box.Text is and empty string))) What I'm trying to do is to check if user puts in the right answer, text_box.Text is == login_credentials, and if it is, show the text_box2 TextBox, else, show text_box3.

HERE IS MY TIMER

private Timer timer1;
        public void InitTimer()
        {
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000; // in miliseconds
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            isonline();
        }

For some reason, it is not working.

If this code is a mess, what do you guys guess me to do? Here's what I want it to do.

  • Have a empty textbox. *User has to put the ''password'' in it.
  • Loop every 1 second to check if ''User has entered the right password''. User enteres wrong ''password'' display *SOMETHING. User enteres right ''password'' display *SOMETHING ELSE

Thanks!

2 Answers

Steven Parker
Steven Parker
229,980 Points

You didn't describe the behavior you're seeing (other than "not working"), but it looks like the text boxes are created within the scope of the function that tests them and are then immediately disposed.

If you need more specific help, you might provide the complete code, or a link to a repo to make it possible to replicate and analyse the situation.

Stivan Radev
Stivan Radev
1,475 Points

**Form1.cs

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 win_opt_200_XP_21_62_8991_sgnileeF
{
    public partial class Form1 : Form
    {
        public static TextBox myText = new TextBox();
        public static Button b = new Button();
        public static int wd = 1400;
        public static int ht = 700;
        public static int textBox_resized_height = (ht - 20) / 2;
        public static int textBox_resized_width = (wd - 100) / 2;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private Timer timer1;
        public void InitTimer()
        {
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000; // in miliseconds
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            isonline();
            timer1.Start();
        }

        public void isonline()
        {
            var login_cradentials = "pass";
            TextBox tt = new TextBox();
            tt.Location = new Point(textBox_resized_width, textBox_resized_height);

            Controls.Add(tt);
            if (tt.Text == login_cradentials)
            {
                TextBox tt2 = new TextBox();
                tt2.Location = new Point(15, 15);
                tt2.Text = "TEST";
                Controls.Add(tt2);
            } else
            {
                TextBox tt2 = new TextBox();
                tt2.Location = new Point(15, 15);
                tt2.Text = "PROBLEM";
                Controls.Add(tt2);
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            isonline();
        }


    }
}

**Form1.Designer.cs

namespace win_opt_200_XP_21_62_8991_sgnileeF
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.BackColor = System.Drawing.Color.MediumAquamarine;
            this.button1.Font = new System.Drawing.Font("8-bit Limit R BRK", 27.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.ForeColor = System.Drawing.Color.White;
            this.button1.Location = new System.Drawing.Point(1273, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(127, 60);
            this.button1.TabIndex = 0;
            this.button1.Text = "EXIT";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.DarkCyan;
            this.ClientSize = new System.Drawing.Size(1400, 700);
            this.Controls.Add(this.button1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "WIN_21_62_8891";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }
}

This should be all the code