Click here to Skip to main content
13,090,299 members (76,161 online)
Rate this:
Please Sign up or sign in to vote.
Hi, I have a string with a sentence. The string is displayed in a richtextbox. The user types a letter in a textbox and the program attempts to find that letter in the string. I want to make it that when the user presses a button a messagebox appears and displays the letter along with its numeric position in the string.

For example, if my sentence has 10 letters and the letter that the user types is number 5 how do I display that letter's number in the messagebox?

Posted 12-Jul-12 1:58am
Updated 12-Jul-12 3:49am
Wes Aday 12-Jul-12 9:20am
Your question is not clear. You want to know how to write MessageBox.Show("5")?
MR. AngelMendez 12-Jul-12 9:36am
I want to get a letter's line number and display it in the messagebox
Wes Aday 12-Jul-12 10:06am
I do not think that is what you want either. What I *think* you want is: MessageBox.Show(yourstring.IndexOf(textbox.txt).ToString());
MR. AngelMendez 12-Jul-12 10:26am
I followed the code that you put but I'm puzzled, what can I do with this?
Wes Aday 12-Jul-12 10:38am
I am not sure what you are asking me here if you are asking me at all since you did not reply to me. What do you mean "what can I do with this?" It is the answer to your question. I cannot see your project, your code, your hard drive or read your mind. I would assume that you would write this into your code where you want to display the index of the letter in your string since that is what you asked to be able to do.
MR. AngelMendez 12-Jul-12 13:52pm
Sorry for the trouble, here is my full source code of what I'm working on with comments of what I'm doing.

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 WindowsFormsApplication1
public partial class Form1 : Form
public Form1()

//the word that the person has to guess
string word;

// the letter that the person types to see if the there is a match
string guess;

// lines to show how many characters are missing
string space = "____";

private void Form1_Load(object sender, EventArgs e)


private void button1_Click(object sender, EventArgs e)

//adds a space for each char from the textbox1.
word = textBox1.Text;

foreach (char c in word)

// the listview is pretty much the game board, it displays the spaces and letters when correct

private void button2_Click(object sender, EventArgs e)

// puts any text that the person types into the guess string

guess = textBox2.Text;

// this checks to see if guess has any char comparisons from the word string
foreach(char c2 in word)
if (guess.Contains(c2))
// I want to see if I can get the letter's line number to see if I can get the listview to
// correctly diplay the letter in the right order.

MR. AngelMendez 12-Jul-12 13:55pm
its pretty much a hangman game. this is just a test but if I can get it right I want to make the game allow users to create their own hangman levels and send the game file to their friends so they can play it.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Make a handler to the KeyPress event of the textbox. Then check out 'e.KeyChar'.
MR. AngelMendez 12-Jul-12 8:08am
I'm sorry I was talking about the letters not the keychar. I updated my question so no one else will get confused. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 12 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100