CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Little help?

  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Little help?

    I started learning C# tonight, and I like to think that I'm alright for my first time, I still can't get my app to work. Basically it's just an app that I'll use to learn, I had originally written it in VB6, and now to learn the ropes I was remaking it in C#. The problem is, I need it to make sure the timer isn't activated if the interval is left blank, or the other textbox is left blank. It works for the interval, if the interval is empty it won't run, if the interval is filled and the other textbox is empty, it starts to run, however. Here's my code.

    (I think it's probably something to do with the { } blocks, because I'm still not sure how to use them 100%)

    Heres my code so far

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                SendKeys.Send(textBox1.Text);
                SendKeys.Send("{enter}");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                if (textBox1.Text.Length.Equals(0))
                {
                    MessageBox.Show("Enter a string to spam!", "Error");
                    timer1.Enabled = false;
                }
                else
                    timer1.Enabled = true;
            
                    if (textBox2.Text.Length.Equals(0))
                    {
                        MessageBox.Show("Enter an interval!", "Error");
                        timer1.Enabled = false;
                    }
                
                    else
                        timer1.Enabled = true;
                
            }
            
    
            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Enabled = false;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void timer1_Tick_1(object sender, EventArgs e)
            {
                SendKeys.Send(textBox1.Text);
                SendKeys.Send("{enter}");
            }
    
            private void button1_Click_1(object sender, EventArgs e)
            {
                if (textBox1.Text.Length.Equals(0))
                {
                    MessageBox.Show("Enter a string to spam!", "Error");
                    timer1.Enabled = false;
                }
                else
                    timer1.Enabled = true;
                
                if (textBox2.Text.Length.Equals(0))
                {
                    MessageBox.Show("Enter an interval!", "Error");
                    timer1.Enabled = false;
                }
            
                else
                
                    timer1.Enabled = true;
                
            }
    
            private void button2_Click_1(object sender, EventArgs e)
            {
                timer1.Enabled = false;
            }   
            
        }
    }

  2. #2
    Join Date
    Jul 2008
    Posts
    29

    Re: Little help?

    Hey, I believe this is your problem. In the following code statements, you need to use the || or && operators instead of using two if statements. || means OR and && AND

    Code:
             private void button1_Click(object sender, EventArgs e)
            {
    
                if (textBox1.Text.Length.Equals(0))
                {
                    MessageBox.Show("Enter a string to spam!", "Error");
                    timer1.Enabled = false;
                }
                else
                    timer1.Enabled = true;
            
                    if (textBox2.Text.Length.Equals(0))
                    {
                        MessageBox.Show("Enter an interval!", "Error");
                        timer1.Enabled = false;
                    }
                
                    else
                        timer1.Enabled = true;
                
            }
    And here is the corrected version:

    Code:
            private void Button1Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Length.Equals(0) || textBox2.Text.Length.Equals(0))
                {
                    MessageBox.Show("You need to fill in both an interval and a string to spam!", "Error");
                    timer1.Enabled = false;
                }
                else
                    timer1.Enabled = true;            
            }
    This should work, but I think it's a bit more sane to write if-statements the other way around, of course this is just my preference.

    Code:
             private void Button1Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0)
                {
                    timer1.Enabled = true;
                }
                else
                {
                    MessageBox.Show("You need to fill in both an interval and a string to spam!", "Error");
                    timer1.Enabled = false;
                }               
            }
    And last but not least, why do you have duplicated code? Button1_Click and Button1_Click_1?
    Last edited by CyberRascal; January 17th, 2009 at 07:54 AM.

  3. #3
    Join Date
    Jan 2009
    Posts
    2

    Re: Little help?

    Thanks for the reply! I just tested this, and I don't get any sort of response, so I guess that didn't work. :\

    As for the duplicate code: I have no clue. xD

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Little help?

    Code:
    MessageBox.Show("Enter a string to spam!", "Error");
    You might want to pick a different app to learn on.

    Not too many folks on this forum are going to be willing to further your study on spamming.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured