C# Char input help needed

Need help for when someone enters a single letter and how to use that

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 12333 Views - Last Post: 19 November 2009 - 11:00 AM Rate Topic: -----

#1 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

C# Char input help needed

Posted 18 November 2009 - 04:38 PM

Ok so i'm not the best programmer and i just recently started wit c#... i just have hopefully a simple question.... i have a copy center like project going and i'm trying to get a user to input a b or a c depending on if they want color or black and white copies... i want it to recognize upper and lower case letters. when i get the letters i want to do calculations using if structures depending on which letter was inputted by the user. I'm stuck trying to use char and get single characters so any help would be good... i don't have much useful code done so anything you can give me will help...
thanks in advance,
Steven

Is This A Good Question/Topic? 0
  • +

Replies To: C# Char input help needed

#2 indrora   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: C# Char input help needed

Posted 18 November 2009 - 04:55 PM

What have you tried already?
Was This Post Helpful? 0
  • +
  • -

#3 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 05:03 PM

actually not that much... i'm pretty new to c# so i've been going over a book i have and samples i have but not really sure where to start... i have a text box for input and im looking for someone to enter a upper or lower case b or c depending on if they want color... i need it to take that value and be able to go to seperate if statements to do different calculations based on what they chose... any chance you have any starter ideas or a sample of something u can make....
Was This Post Helpful? 0
  • +
  • -

#4 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 05:28 PM

namespace Stevens_Copy_Centre
{
	public partial class CopyCentre : Form
	{
		public CopyCentre()
		{
			InitializeComponent();
		}

		private void calculateToolStripMenuItem_Click(object sender, EventArgs e)
		{
			int NumberOfCopies;
			double TotalCost;

			try
			{
				NumberOfCopies = int.Parse(CopiesTextBox.Text);
			}
			catch
			{
				MessageBox.Show("Input Is Non-numeric or Blank", "Input Error");
				CopiesTextBox.Focus();
				return;
			}
		}

		private void clearToolStripMenuItem_Click(object sender, EventArgs e)
		{
			CopiesTextBox.Clear();
			BlackOrColorTextBox.Clear();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Programmer: Steven Smiley", "About my Project");
		}
	}
}



it might be hard to follow... i have all the the textboxes and stuff for the letters i just don't know how to get the letters from the input and use them in the code to make calculations...
Was This Post Helpful? 0
  • +
  • -

#5 indrora   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: C# Char input help needed

Posted 18 November 2009 - 06:19 PM

BlackOrColorTextBox.Text to get a string out of the textbox. However, there are already controls to help you with this:

NumericUpDown allows for up/down values of -infinity to infinity.
CheckBox (just say "Print In Color?")
Etc... Etc... Etc..
Was This Post Helpful? 0
  • +
  • -

#6 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 06:54 PM

thanks for the tips but i would like to do it this way because it's for a project.... so is it possible to give me help with doing it this way if at all possible... just looking to get the values of c or b in upper and lowercase and use them in a if structure to do calculations... let me know what you can do....
Was This Post Helpful? 0
  • +
  • -

#7 knichols   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 21
  • Joined: 09-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 07:20 PM

If you want to just sit there looking for key presses I guess you're looking for more of a console type app.
Heres some console code to do it.

			while (true)
			{
				Console.WriteLine("Enter Letter");

				char InputChar = Console.ReadKey(true).KeyChar;

				switch (InputChar)
				{
					case 'A':
					case 'a':
						Console.WriteLine("Do the Task For Letter A");
						break;

					case 'B':
					case 'b':
						Console.WriteLine("Do the Task For Letter B");
						break;
				}
			}



Was This Post Helpful? 0
  • +
  • -

#8 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 07:26 PM

thanks for that code... quick question for ya... if i use that code ur talking about here where it says the case for each letter like upper and lower case a, you have console.writeline to output something could i put an if structure in there???
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: C# Char input help needed

Posted 18 November 2009 - 07:34 PM

Yes, you could also use this:

if (InputChar == 'a' || InputChar == 'A')
{
	// Do stuff
}

if (InputChar == 'b' || InputChar == 'B')
{
	// Do stuff
}


Was This Post Helpful? 0
  • +
  • -

#10 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 07:36 PM

View PostSixOfEleven, on 18 Nov, 2009 - 06:34 PM, said:

Yes, you could also use this:

if (InputChar == 'a' || InputChar == 'A')
{
	// Do stuff
}

if (InputChar == 'b' || InputChar == 'B')
{
	// Do stuff
}



ok so where i get confused is where am i getting the info from.... do i have to declare anything or with your code what should my text box be called.... sorry for all the questions i just really don't get this part...
Was This Post Helpful? 0
  • +
  • -

#11 knichols   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 21
  • Joined: 09-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 07:51 PM

What i was implying was that you use console, so you wouldn't have a textbox or any other windows controls.

It would just be typing stuff in like when you are using the command prompt.

If you use windows forms and want to capture single key strokes, you would have to make sure the control you're capturing them with always has focus.

The inputchar from below is straight from what you type in the keyboard.



Perhaps sixofeleven knows a better way to capture the key strokes from a form without using the console.
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: C# Char input help needed

Posted 18 November 2009 - 07:53 PM

Sorry, I was going from what the last poster said with switch statement. Instead of the switch you could use the if statements. Do you have to use a text box to get the user's input, what I'm asking is this a class assignment? If it is not a class assignment you would be better to use a Combo Box or List Box and add the different selections to those. If you have to use a Text Box I will call the Text Box, CopyTypeTextBox. What you can do is in the properties for CopyTypeTextBox is set the MaximumLenght property to be 1 so that the user can only enter 1 letter into the text box. Then I guess you wouldn't have to worry about using the character type you could just use the string type. In the click event where you calculate the total you could do something like the following:

		private void calculateToolStripMenuItem_Click(object sender, EventArgs e)
		{
			int NumberOfCopies;
			double TotalCost;

			try
			{
				NumberOfCopies = int.Parse(CopiesTextBox.Text);
			}
			catch
			{
				MessageBox.Show("Input Is Non-numeric or Blank", "Input Error");
				CopiesTextBox.Focus();
				return;
			}
			if (CopyTypeTextBox.Text == "a" || CopyTypeTextBox.Text == "A")
			{
				 // you have an a or A in the text box do what you need to do
			}
			else if (CopyTypeTextBox.Text == "b" || CopyTypeTextBox.Text == "B")
			{
				 // you have an b or B in the text box do what you need to
			}
			else if (CopyTypeTextBox.Text == "c" || CopyTypeTextBox.Text == "C")
			{
				 // you have a c or C
			}
			else
			{
				 MessageBox.Show("Invalid copy type.", "Input Error:);
			}
		}


Was This Post Helpful? 0
  • +
  • -

#13 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 07:54 PM

ya maybe well thanks for your help... for the project we need to capture keystrokes from a text box and based on what keystroke it is do different calculations... if u know anything more that would be fantastic...
Was This Post Helpful? 0
  • +
  • -

#14 SixOfEleven   User is offline

  • Planeswalker
  • member icon

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

Re: C# Char input help needed

Posted 18 November 2009 - 07:56 PM

Read my reply above. That will help you out I believe.
Was This Post Helpful? 0
  • +
  • -

#15 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: C# Char input help needed

Posted 18 November 2009 - 08:02 PM

thanks sooo much for that new code... its workin so far i have a little more to do... just one more quick question can u do an if inside of an if structure....

View Poststsmiley, on 18 Nov, 2009 - 07:01 PM, said:

thanks sooo much for that new code... its workin so far i have a little more to do... just one more quick question can u do an if inside of an if structure....

if i could i need to take those letters and say it was a b for black i need to give a 5 cents a page for up to 100 and everything after 100 its 3 cents.... something like that....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2