-2

This is based off a console program that I'm trying to make a GUI for it.

Please take a look in firstNumber_Click and Arithmetic firstRandomNumber()

Any chance I could get an explanation on why it's not returning the random number to the text box?

Arithmetic

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAI_GUI
{
    class Arithmetic
    {
        public int firstNumber;
        public int secondNumber;

        public Arithmetic(int _firstNumber, int _secondnumber)
        {
            firstNumber = _firstNumber;
            secondNumber = _secondnumber;
        }

        public Arithmetic()
        {
        }

        public int firstRandomNumber()
        {
            Random rnd = new Random();
            firstNumber = rnd.Next(0, 9);
            return firstNumber;
        }

        public int secondRandomNumber()
        {
            Random rnd = new Random();
            secondNumber = rnd.Next(0, 9);
            return secondNumber;
        }

        public int FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
            {
                firstNumber = value;
            }
        }

        public int SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }
    }
}

Form4

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 CAI_GUI
{
    public partial class Form4 : Form
    {
        Arithmetic A1 = new Arithmetic();

        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            A1.secondRandomNumber();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void firstNumber_Click(object sender, EventArgs e)
        {
            int first = Convert.ToInt32(num1.Text);

            num1.Text = A1.firstRandomNumber().ToString();
        }
    }
}
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
Rayne Blackham
  • 119
  • 1
  • 8
  • You're never assigning anything to the textbox text. `TextChanged` is an event called when the value of the text box changes, not an event *to* change the text. – Luaan Jul 18 '14 at 12:23
  • 1
    There's a few issues here, which one are you trying to deal with? what error are you getting? – Christian Phillips Jul 18 '14 at 12:23
  • I get an input error; I'm trying to return the random number to num1.text; could you perhaps show me how to rectify this in code please? – Rayne Blackham Jul 18 '14 at 12:25
  • I'm wondering what the syntax for the textbox_changed would be; I've got the generate button working now (thanks to LIUFA) but I would like it to automatically appear when the form launches. – Rayne Blackham Jul 18 '14 at 12:33

1 Answers1

1

I think it does not work because of line int first = Convert.ToInt32(num1.Text); as it fails to convert, try this.

private void firstNumber_Click(object sender, EventArgs e)
    {
        num1.Text = A1.firstRandomNumber().ToString();
    }
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • Un-freakin-believeable... that actually worked. If I was it to place it in the text box without having to click to generate; I assume I place that line within the TextBox_Changed? – Rayne Blackham Jul 18 '14 at 12:28
  • How would I accomplish this without a button? What if I want to have the form upon launch place those random numbers within the num1.Text; without clicking anything. – Rayne Blackham Jul 18 '14 at 12:53
  • That worked perfectly, realised this after I posted; my only remaining problem now is; the random numbers generated are identical, first and second random numbers are sequentially the same... – Rayne Blackham Jul 18 '14 at 13:29
  • @RayneBlackham http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – Matas Vaitkevicius Jul 18 '14 at 13:32