i m new to c#
i was designing a calculator
i need to accept a string of integers from textbox
save it in a variable
clear the textbox
and then accept a new string of integers
plz kindly help

Recommended Answers

All 12 Replies

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 daniweb
{
  public partial class frmText : Form
  {
    private List<string> _values;

    public frmText()
    {
      InitializeComponent();
      _values = new List<string>();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      _values.Add(textBox1.Text);
      textBox1.Clear();
      MessageBox.Show(GetLastString());
    }

    private string GetLastString()
    {
      return (_values.Count == 0 ? string.Empty : _values[_values.Count - 1]);
    }

  }
}

You can use two different variables.
For example:
Lets have a text box, and an "Ok" button . Whenever the "Ok" button is pressed the entered data is stored into the variable and then use
[\icode]textbox1.Clear()[\icode] assuming that the name of the text box is textbox1. This command will empty the text box and after doing the same thing with another variable, I would recommend you to restart the application to avoid confusion.[code=csharp][\icode]textbox1.Clear()[\icode]
assuming that the name of the text box is textbox1.
This command will empty the text box and after doing the same thing with another variable, I would recommend you to restart the application to avoid confusion.

commented: Restart an application to clear a variable? Please +0

sknake sir
plz czn u tell what has been done here ?

sarganaa,

STOP SENDING ME PRIVATE MESSAGES WITH THE SAME TEXT AS YOUR POSTS. Daniweb sends an email when you update the thread, I don't need a private message + new private message alert email + thread update email every time you post on a thread.

I posted the source code for something you might want... the _values contains a list of strings. Each time you press button1 it adds the value to the end of the collection and GetLastString() will retrieve that value for you. Try it out.

Good luck

themaster sir
thnx for the reply
i tried that thing
but using a calc
it is illogical to press the +,-,*,/ buttons twice
firstly to initiate and then to save the operands

This stores a text box to a global string s, then clears the textbox.
(Assuming you have a button named button1 and a textbox named textBox1)

string inputstring;
private void button1_Click(object sender, EventArgs e)
{
  inputstring = textBox1.Text;
  textBox1.Clear();
}

If you want the button to work for more than one input, you can use an array:

Array inputarray();
int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
  inputarray[counter] = textBox1.Text;
  textBox1.Clear();
  counter++;
}

To use the information, you can use inputarray[i] as a string (replace i with a number, 0 will be the first entry because of the second line of code)
If you want to convert it to a number for calculations, you can use Convert.ToInt32(inputarray[i]);

danbe sir
the code provided there is quite complex
i can't understand that thing
n not thinking to copy from anyone
sknake sir
that code is showing error of '{'
and "Error Type or namespace definition, or end-of-file expected "
inspite i have checked that '{' or '}' or ';' isn't missing in the code...

Don't try to design a calculator if you find some simple code "quite complex" perhaps you should start nitting? Bye!

themaster sir
thnx for the reply
i tried that thing
but using a calc
it is illogical to press the +,-,*,/ buttons twice
firstly to initiate and then to save the operands

No. Like I meant that after writing the first number in the text box enter the operation to enter the number in the variable then put textbox1.Clear();
and then enter the value of the second variable. Since I dont have the compiler right now I cannot give you the code or I surely would have given you the new project.

Since I dont have the compiler right now I cannot give you the code or I surely would have given you the new project.

STOP smoking funny sigarettes!

danbe sir
scott sir
i succeeded in designing one myself
it works as the original one in the windows
but not willing to make it scientific

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.