Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,681 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,220 people online right now. Registration is fast and FREE... Join Now!




adding labels?

 
Reply to this topicStart new topic

adding labels?, how to let the user add labels

Hypnagogue
post 1 Jun, 2008 - 09:36 AM
Post #1


New D.I.C Head

*
Joined: 1 Jun, 2008
Posts: 5

Hi,

I'm trying to program the game Hangman in C#. Now I'm just a beginner in C#. but I'm halfway there, I'm trying to Add a label in the form after the user clicked on a button.

So far I'm programming with a fixed word ("Apple"), so when I click on the button A (btnA) the result of woord.IndexOf('a')) is 1.

I guessed a letter right and now it has to show on screen, with a label on the right place.


CODE
private void btnA_Click(object sender, EventArgs e)
        {
            btnA.Enabled = false;

            int resultaat = (woord.IndexOf('a'));
            if (resultaat <0) //the result is smaller then 0, the letter is not in the word
            {
                //code for when the letter is wrong
            }
            else
            {
                //here, the letter is right and i want to show it on screen with a label
                label1.Text = "A";
            }
        }

Now I know I drew a label on my form just as a test. but this will be remove the label has te be draw on command.
My question is how can i add these labels?
sorry if my english is a bit blurry...

This post has been edited by Hypnagogue: 1 Jun, 2008 - 09:43 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Jun, 2008 - 10:39 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


From what I gathered of your question you are asking essentially how to add labels to a form dynamically where each label would have the letter from the guess word.

Assuming that is your question, the answer is this example below...

csharp

private void button1_Click(object sender, EventArgs e)
{
String guessword = "test";
int y = 10, x = 10;

Label letter;

// Loop through each letter of our word
// Create a label, assign it a width, the letter they guessed
// Adjust its position on the form and then add it to the form.
for (int i = 0; i < guessword.Length; i++) {
letter = new Label();
letter.Width = 10;
letter.Text = guessword[i].ToString();
letter.Left = x;
letter.Top = y;

this.Controls.Add(letter);
x += 12;
}
}


Now this is an example of how to create each label and add it to the form. After running this, your form will show "t e s t". Of course you can see how this would work for your letter guessing. If you find the letter, based on its position in the word you would adjust the x value accordingly. So lets say the word was "test" and they guessed the "s" since that is in position 3 of the word, you would set x = position * 12 or 36, create the label and add it to the form.

Of course this isn't the only or most efficient way to do what you want to do (guessing letters) but it shows you one way of doing it.

Hope it helps! Enjoy!

"At DIC we be label displaying code ninjas... label here, label there, labels everywhere" decap.gif

This post has been edited by Martyr2: 1 Jun, 2008 - 11:27 AM
User is offlineProfile CardPM

Go to the top of the page

Hypnagogue
post 1 Jun, 2008 - 12:04 PM
Post #3


New D.I.C Head

*
Joined: 1 Jun, 2008
Posts: 5

QUOTE(Martyr2 @ 1 Jun, 2008 - 11:39 AM) *

From what I gathered of your question you are asking essentially how to add labels to a form dynamically where each label would have the letter from the guess word.

Assuming that is your question, the answer is this example below...

csharp

private void button1_Click(object sender, EventArgs e)
{
String guessword = "test";
int y = 10, x = 10;

Label letter;

// Loop through each letter of our word
// Create a label, assign it a width, the letter they guessed
// Adjust its position on the form and then add it to the form.
for (int i = 0; i < guessword.Length; i++) {
letter = new Label();
letter.Width = 10;
letter.Text = guessword[i].ToString();
letter.Left = x;
letter.Top = y;

this.Controls.Add(letter);
x += 12;
}
}


Now this is an example of how to create each label and add it to the form. After running this, your form will show "t e s t". Of course you can see how this would work for your letter guessing. If you find the letter, based on its position in the word you would adjust the x value accordingly. So lets say the word was "test" and they guessed the "s" since that is in position 3 of the word, you would set x = position * 12 or 36, create the label and add it to the form.

Of course this isn't the only or most efficient way to do what you want to do (guessing letters) but it shows you one way of doing it.

Hope it helps! Enjoy!

"At DIC we be label displaying code ninjas... label here, label there, labels everywhere" decap.gif



ok thank you, yes this is what I was searching for.

Now I have another question to see if the input letter is correct or not I use IndexOf() this will result in -1 (if it's incorrect) or a number of the position of the letter in the word. But with indexof() it only recognizes the first letter occurrence in the word. How can I search again without the result of the first indexof()??
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Jun, 2008 - 01:56 PM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


You will notice that the indexOf method has another version which takes a second parameter that is the start of the search. So lets say your word was "mississippi" and they guessed the letter "i" your indexOf will return the first i as index 1 (because index starts at 0). You use the result of this indexOf call, add 1 to it and use that value as the second parameter to a new indexOf call located the place to start the search. So to find the second "i" you would start at index 2 (last index found was 1, we add 1 to get 2).

You can see an example of this on MSDN for the indexOf method. Here is the link for you to take a look at.

String.IndexOf Method - MSDN

Enjoy the example. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Hypnagogue
post 1 Jun, 2008 - 02:14 PM
Post #5


New D.I.C Head

*
Joined: 1 Jun, 2008
Posts: 5

Thanks, I was doing it with a while loop. it also works.

Now, is there a way to place the letter in a specific label?
label1.Text = "a";
is not flexible enough.
I'm searching for something like label(i).Text = "a"
where i is the place where the letter has to be.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Jun, 2008 - 02:17 PM
Post #6


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


You don't have to, when the time comes to show the letter, you would then call the function to add labels to the form. No need to set the labels because there won't be any labels until it is time to show a letter.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

Hypnagogue
post 1 Jun, 2008 - 02:25 PM
Post #7


New D.I.C Head

*
Joined: 1 Jun, 2008
Posts: 5

QUOTE(Martyr2 @ 1 Jun, 2008 - 03:17 PM) *

You don't have to, when the time comes to show the letter, you would then call the function to add labels to the form. No need to set the labels because there won't be any labels until it is time to show a letter.

smile.gif



I don't understand, so I know the position of the character in the string.
you're saying if the user click on button_A for the word "abstract", I have the first label in position but what about the sixth "a"? how do you know where to position it? as the user didn't click on any letter before.

maybe I can name my labels 1, 2, 3, 4,... and code something like [i].Text = "a";

User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Jun, 2008 - 02:32 PM
Post #8


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Your loop will not just add one label, it will add ALL labels for a given letter. So you would loop through "abstract" and find the first "a" and your x value will be equal to 0 (the position) times 12 or 0 pixels added to some arbitrary start point. You create the label and put it on the form... you don't stop the loop there, keep going with the loop until the end of the word. You would do your next indexOf starting from position 0 (where you found the first "a"), add 1, so your next indexOf will start at position 1 and finds the next "a" at position 5 (again because you start at 0). Here "x" will equal 5 * 12 or 60 pixels plus the start point. You create the next label and position it at this new x value. Then if there was more, you continue with the loop.

So in short you find the first "a", create a label, position it with a dynamically changing x value. Continue the loop, find the second "a", create a label, position it with a dynamically changing x value... continue the process this way until you found all the letters and reach the end of the guess word string.

This sounds a bit complicated but it is not. Hopefully it makes sense. smile.gif

This post has been edited by Martyr2: 1 Jun, 2008 - 02:32 PM
User is offlineProfile CardPM

Go to the top of the page

Hypnagogue
post 1 Jun, 2008 - 02:36 PM
Post #9


New D.I.C Head

*
Joined: 1 Jun, 2008
Posts: 5

oh! That's brilliant!! yes I understand it now. I tried avoiding the x and y positioning but I guess it's really needed.

Thanks so much!
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Jun, 2008 - 02:38 PM
Post #10


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Glad it helped you out. I appreciate the thanks votes. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:46AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month