12 Replies - 1313 Views - Last Post: 22 September 2009 - 02:07 PM Rate Topic: -----

#1 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

changing char back to an int

Post icon  Posted 22 September 2009 - 12:21 PM

I've recently coded a function where the user would enter an int on a calculator, click an ASCII button and have a char display on screen. I now want to add the fuctionality where, if the ASCII button is pressed again, the char turns back into the original int. I can't quite work out how to do this yet but I had in mind something like the below

		private void Button_ASCII_Click(object sender, EventArgs e)
		{
			bool error = false;
			int input;
			char inputChar;
			
			if (InputTextBox.Text.Length != 0)
			{
				if (!(error = !Int32.TryParse(InputTextBox.Text, out input)))
				{
					InputTextBox.Text = string.Empty;

					if (input >= 33 && input <= 127)
					{
						char c = (char)input;
						string s = c.ToString();
						InputTextBox.Text = s;
					}
					else
						error = true;
				}
			}
			if (error = Char.TryParse(InputTextBox.Text, out inputChar))
			{
				int i = (int) inputChar;
				InputTextBox.Text = i;
			}
		}



Can anyone point me in the right direction? Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: changing char back to an int

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: changing char back to an int

Posted 22 September 2009 - 12:31 PM

which part are you having an issue with? getting the button to perform two different functions? or getting the code to do the conversion?
Was This Post Helpful? 0
  • +
  • -

#3 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: changing char back to an int

Posted 22 September 2009 - 12:34 PM

Getting the button to perform two different functions. Essentially, what I want is, the user will enter an int and the ascii will give the char. If the user clicks the button again, the char changes back to the original int. Does that make sense?
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: changing char back to an int

Posted 22 September 2009 - 12:56 PM

here is a very simple example that should help you out...

protected void button1_Click(object sender, EventArgs e)
{
     if (button1.Text = "Start")
     {
           // Some code to start a process


           button1.Text = "Stop";
     }
     else if (button1.Text = "Stop")
     {
          // some code to stop a process


          button1.Text = "Start";
     }
}




as you can see, the button will perform different tasks depending on the text of the button.
Was This Post Helpful? 1
  • +
  • -

#5 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: changing char back to an int

Posted 22 September 2009 - 01:04 PM

Edit:
Looks like eclipsed4utoo already answered it!

Sounds like what you need to do is create another variable that will determine whether or not you have the ascii character or number. Lets say we have a bool called isSecondary, this will tell us if we have a ascii character or number. Whenever you change the InputTextBox.Text you should set isSecondary equal to true or false, depending on what you consider secondary. Make sure that isSecondary is outside of the Button_Click method. Example

bool isSecondary = false;

private void Button_Click(object sender, EventArgs e)
{
    //If we press the button and the InputTextBox.Text contains an ascii character
    //We set our isSecondary equal to false

    //If we press the button and the InputTextBox.Text contains an ascii character number
    //We set our isSecondary equal to false  
}



Hope this helps

This post has been edited by gbertoli3: 22 September 2009 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: changing char back to an int

Posted 22 September 2009 - 01:24 PM

View Posteclipsed4utoo, on 22 Sep, 2009 - 07:56 PM, said:

here is a very simple example that should help you out...

protected void button1_Click(object sender, EventArgs e)
{
     if (button1.Text = "Start")
     {
           // Some code to start a process


           button1.Text = "Stop";
     }
     else if (button1.Text = "Stop")
     {
          // some code to stop a process


          button1.Text = "Start";
     }
}




as you can see, the button will perform different tasks depending on the text of the button.


That is helpful but my button text is just 'ASCII' whereas the value would go into a text box. Hmmm
Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: changing char back to an int

Posted 22 September 2009 - 01:32 PM

Instead of using the Text property you could use the Tag property. In the Load event of your form set the tag property of your button to 1. Then in the click event you can use the same idea eclipsed used.

if ((int)button1.Tag == 1)
{
	button1.Tag = 2;
	// preform your logic
}
else if ((int)button2.Tag == 2)
{
	button1.Tag = 1;
	// preform your logic
}


Was This Post Helpful? 0
  • +
  • -

#8 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: changing char back to an int

Posted 22 September 2009 - 01:34 PM

Thanks but Im a bit confused about it all. I am only using the one button. Mabye tomorrow :(
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: changing char back to an int

Posted 22 September 2009 - 01:45 PM

View Postirishgirl, on 22 Sep, 2009 - 02:34 PM, said:

Thanks but Im a bit confused about it all. I am only using the one button. Mabye tomorrow :(


Every control has a Tag property. It holds an object so you can assign anything to it. Look at this simple example program.

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 ButtonTester
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			button1.Tag = 1;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if ((int)button1.Tag == 1)
			{
				MessageBox.Show("This was the first click.");
				button1.Tag = 2;
			}
			else if ((int)button1.Tag == 2)
			{
				MessageBox.Show("This was the second click.");
				button1.Tag = 1;
			}
		}
	}
}



In the load event I set the Tag property of button1 to 1, meaning that when it is first clicked by the user in your case it would convert an int to ASCII. Now in the click event for button1 I have and if-else-if in the if I cast the Tag to an int and if it is 1 I show a message box saying this is the first click, on your form you would convert to ASCII, and I set the Tag property to 2 meaning that the next time the button is clicked it will do the second option because in the else-if part I check to see if the Tag property, cast to an int, is 2. If that is true the button was clicked a second time and I write out a message box saying that the button was clicked a second time you would convert from ASCII back to int and I set the Tag back to 1 so when the button is clicked next it will go back to the first one.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#10 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: changing char back to an int

Posted 22 September 2009 - 02:02 PM

Thanks, I coded the below with the help of above but got a NullReferenceException unhandled for

			if ((int)Button_ASCII.Tag == 1)




Code in full
	 private void Button_ASCII_Click(object sender, EventArgs e)
		{
			int input;
			char charInput;

			if ((int)Button_ASCII.Tag == 1)
			{
				input = System.Int32.Parse(InputTextBox.Text);
				InputTextBox.Text = string.Empty;

				if (input >= 33 && input <= 127)
				{
					char c = (char)input;
					string s = c.ToString();
					InputTextBox.Text = s;
				}
				Button_ASCII.Tag = 2;
			}

			else if ((int)Button_ASCII.Tag == 2)
			{
				charInput = Char.Parse(InputTextBox.Text);
				int i = (int)charInput;
				InputTextBox.Text = i.ToString();
				Button_ASCII.Tag = 1;
			}

		}



Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: changing char back to an int

Posted 22 September 2009 - 02:03 PM

View Postirishgirl, on 22 Sep, 2009 - 04:24 PM, said:

View Posteclipsed4utoo, on 22 Sep, 2009 - 07:56 PM, said:

here is a very simple example that should help you out...

protected void button1_Click(object sender, EventArgs e)
{
     if (button1.Text = "Start")
     {
           // Some code to start a process


           button1.Text = "Stop";
     }
     else if (button1.Text = "Stop")
     {
          // some code to stop a process


          button1.Text = "Start";
     }
}




as you can see, the button will perform different tasks depending on the text of the button.


That is helpful but my button text is just 'ASCII' whereas the value would go into a text box. Hmmm


With your button having the text of "ASCII", users will believe that they will get an ascii code when clicking that button. Which would be half true. Changing the text let's your users know to expect different behavior.
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: changing char back to an int

Posted 22 September 2009 - 02:06 PM

View Postirishgirl, on 22 Sep, 2009 - 03:02 PM, said:

Thanks, I coded the below with the help of above but got a NullReferenceException unhandled for

			if ((int)Button_ASCII.Tag == 1)




Code in full


Did you set the Tag to 1 in the Load event of your form? That is important otherwise the Tag will be null.
Was This Post Helpful? 0
  • +
  • -

#13 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: changing char back to an int

Posted 22 September 2009 - 02:07 PM

Sorted now, thank you all!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1