I am making a list box with two input numbers, Run button, Clear button and Increment/Decrement button.

What i'm trying to get it to do is the user inputs a number into textbox1 and a number into textbox2, then clicks run. The numbers then display in ascending or descending order based on the Increment/Decrement button.

Could you guys help me out on coding it correctly? Thanks!!

Recommended Answers

All 9 Replies

btw, I am new poster here in case you couldnt tell with my postcount. lol =)

How you want to have numbers listed? Horizontal ot vertical?

like this, I am having most trouble with the descending code though when the Increment/Decrement button is pressed

This is your 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 Dec9Exercise
{
    public partial class Form1 : Form
    {
        string direction;

        public Form1()
        {
            InitializeComponent();
            direction = Direction.Increment.ToString();
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            int[] array = Calculation();
            if (array.Length > 0)
                PopulatingValues(array);
        }

        private int[] Calculation()
        {
            int[] array = null;
            try
            {
                int value1 = Convert.ToInt32(textBox1.Text);
                int value2 = Convert.ToInt32(textBox2.Text);
                int counter = 0;
                array = new int[value2 - 1];

                if (direction == Direction.Increment.ToString())
                {
                    for (int i = value1; i <= value2; i++)
                        array[counter++] = i;
                }
                else
                {
                    for (int i = value2; i >= value1; i--)
                        array[counter++] = i;
                }
            }
            catch
            {
                MessageBox.Show("Some error has occured during calculation...");
            }
            return array;
        }

        private void PopulatingValues(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
                this.listBox1.Items.Add(array[i]);
        }

        private void buttonChange_Click(object sender, EventArgs e)
        {
            if (buttonChange.Text == Direction.Increment.ToString())
            {
                direction = Direction.Decrement.ToString();
                buttonChange.Text = Direction.Decrement.ToString();
            }
            else
            {
                direction = Direction.Increment.ToString();
                buttonChange.Text = Direction.Increment.ToString();
            }
        }

        private enum Direction
        {
            Increment,
            Decrement
        }
    }
}

I hope you like it. It works fine.
DONT FORGET: Rename the controls (textBoxes, buttons, and listBox) like you have it in your project! Then its got to work.

I hope you like it,

Mitja

Hey thanks. I'm not adept in C# by any means and some of the code you have written is a bit above me at the moment. Could you take a look at my code here and see if you can decypher it and maybe make some changes to the way i have it?
Thanks~

I am unsure of how to even post my code like you posted yours, but here it is.

private void btnRun_Click(object sender, System.EventArgs e)
        {

            int intStart,
                intEnd;

            string strMsg1 = "Goodbye",
                   strIncrement = btnIncrement.Text;

            intStart = Convert.ToInt32( txtStart.Text );
            intEnd   = Convert.ToInt32( txtEnd.Text );



            if ( txtStart.Text == "" ||
                 txtEnd.Text   == "" )
            {
                    MessageBox.Show(
                    "Please enter a starting and ending value.",
                    "Missing Information", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation );
            }





            while (intStart <= intEnd)
            {
                lstOutput.Items.Add(intStart);
                ++intStart;


                        if (intStart > intEnd &&
                             strIncrement == "Increment")
                            ++intStart;
                        else
                            if (intStart < intEnd &&
                                 strIncrement == "Decrement")
                                --intStart;



                while ( intStart <= intEnd )
                {
                        lstOutput.Items.Add( intStart );
                        intStart += 1;


                        if ( intStart > intEnd )
                             lstOutput.Items.Add( strMsg1 );

                        }
                    }
                }

        private void btnClear_Click(object sender, EventArgs e)
        {
             lstOutput.Items.Clear();


             txtStart.Text = "";
             txtEnd.Text = "";
        }

        private void btnIncrement_Click(object sender, EventArgs e)
        {
            if ( btnIncrement.Text == "Increment" )
                 btnIncrement.Text = "Decrement";
            else
                 btnIncrement.Text = "Increment";
        }
        }

I have re-made your code. I didnt test it, but it should work. Here you have it:

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;
using System.Text.RegularExpressions;

namespace Dec9Exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRun_Click(object sender, System.EventArgs e)
        {

            int intStart,
            intEnd;

            string strMsg1 = "Goodbye",
            strIncrement = btnIncrement.Text;

            if (txtStart.Text == "" ||
            txtEnd.Text == "")
            {
                MessageBox.Show(
                "Please enter a starting and ending value.",
                "Missing Information", MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation);
            }
            else
            {
                intStart = Convert.ToInt32(txtStart.Text);
                intEnd = Convert.ToInt32(txtEnd.Text);

                if (btnIncrement.Text == "Increment")
                {
                    while (intStart <= intEnd)
                    {
                        lstOutput.Items.Add(intStart);
                        intStart++;
                    }
                }
                else
                {
                    int value = intEnd;
                    while (intStart <= intEnd)
                    {
                        lstOutput.Items.Add(value);
                        intStart++;
                        value--;
                    }
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstOutput.Items.Clear();
            txtStart.Text = "";
            txtEnd.Text = "";
        }

        private void btnIncrement_Click(object sender, EventArgs e)
        {
            if (btnIncrement.Text == "Increment")
                btnIncrement.Text = "Decrement";
            else
                btnIncrement.Text = "Increment";
        }
    }
}

Thanks! that worked like a charm! I appreciate all of your help! hope to be able to help myself soon

Glad to help and that you are happy.

bye,
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.