same name for all labels?

Status
Not open for further replies.

Pavle

Well-known member
Joined
Oct 4, 2016
Messages
47
Programming Experience
1-3
I am trying to make game that you have 12 random letters and with them you should make longest possible word that exist in ditionary.I want to loop through all labels and every time button is clicked each label separatly stops generating letters,when 12 label stops generating letters timer should stop.How to give same name for all labels?
 
You can't give all of the labels the same name and there isn't a reason you should be able to.
What I would recommend is putting all of the labels into a Panel, then loop the Panel's Items collection and do the label assignments that way. When the loop exits, you stop your Timer.
 

I made it like this but every time I click button it prints System.Windows.Forms.Label.Text:label2, System.Windows.Forms.Label.Text:label3 .......What is wrong???


  • private void button1_Click(object sender, EventArgs e)
  • {
  • Label[] label = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11, label12 };
  • for (int i = 0; i < 12; i++)
  • slova[i] = Convert.ToString(label[i]);
  • if (!button1.Enabled)
  • return;
  • if (button1.Text == "Start")
  • {
  • i = 0;
  • timer1.Enabled = true;
  • button1.Text = "Stop";
  • button1.Enabled = true;
  • }
  • else
  • {
  • label[i].Text = slova[i];
  • i = i + 1;
  • if (i == 12)
  • {
  • textBox1.Enabled = true;
  • button1.Enabled = false;
  • button1.Text = "Start";
  • timer1.Enabled = false;
  • Vreme.Enabled = true;
  • textBox1.Focus();
  • }
  • }
  • }
  • private void timer1_Tick(object sender, EventArgs e)
  • {
  • Label[] label = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11, label12 };
  • long ascii = 0;
  • ascii = rand.Next(65, 90);
  • while (!(ascii != Convert.ToInt32("W") & ascii != Convert.ToInt32("X") & ascii != Convert.ToInt32("Q")))
  • {
  • ascii = rand.Next(65, 90);
  • }
  • label[i].Text = Convert.ToString(ascii);
  • Application.DoEvents();
  • }

 
Firstly, please don't post your code like in future. Just post it as plain text inside the appropriate formatting tags, i.e.

[xcode=c#]your code here[/xcode]

As for the issue, the problem is here:
slova[i] = Convert.ToString(label[i]);
You don't convert a control to a string to get the text it contains. You get its Text property. That code should be:
slova[i] = label[i].Text;
 
I have fixed all problems.
Whether it can be made to computer take a word from database shuffle it and every time button is clicked it show letters on each textbox separatly???
 
Are you asking for how to get something out of a database, or how to take a word (string) and pull out each character (letter) to display somewhere separately, or both?
 
For a database connection, here's a tutorial to get ya started off: https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm

As for the getting each letter of a string (word), what you would do is loop the string and assign the letter to the appropriate label or text:
private void LoopWord()
{
    Label[] label = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11, label12 };
    string WordString = "Example";

    for (int i = 0; i < WordString.Length; i++)
    {
        label[i].Text = WordString[i].ToString();
    }
}
You might want to clear all of the labels before setting the word letters though, here's a full example:
Label[] _labels;

private void Form1_Load(object sender, EventArgs e)
{
    _labels = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11, label12 };
}

private void LoopWord()
{
    string WordString = "Example";

    for (int i = 0; i < WordString.Length; i++)
    {
        _labels[i].Text = WordString[i].ToString();
    }
}

private void ClearLabels() {
    foreach (Label lbl in _labels) {
        lbl.Text = String.Empty;
    }
}
 
How to make this in C# with SQL Server:

Call rs.Open("SELECT DISTINCT Reci FROM " & CStr(duzina), Conn, adOpenKeyset)
rs.Move getRandom(0, rs.RecordCount - 1)
rec = rs.Fields(0).Value


Set rs = Conn.Execute("SELECT DISTINCT Reci FROM " & CStr(Len(r)))
 
How to make this in C# with SQL Server:

Call rs.Open("SELECT DISTINCT Reci FROM " & CStr(duzina), Conn, adOpenKeyset)
rs.Move getRandom(0, rs.RecordCount - 1)
rec = rs.Fields(0).Value


Set rs = Conn.Execute("SELECT DISTINCT Reci FROM " & CStr(Len(r)))
I don't even know what you're trying to do...
 
So you want a query like this:
C#:
Select Top 1 Reci From <SomeTableName> Where Len(Reci) Between 10 And 12;
 
I made to get random word with lenght between 10 and 12.
How to shuffle random word that I get and split it's letters on labels???

C#:
[B][COLOR=#666666][FONT='inherit']string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename= C:\Users\Pavle\Documents\Visual Studio 2015\Projects\Test slagalica\Test slagalica\Slagalica-DB.mdf;Integrated Security=True";[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']            string queryString = "SELECT * FROM table1 WHERE LEN(Reci) >=10 AND LEN(Reci) <=12 ORDER BY NEWID()";[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']            using (SqlConnection connection = new SqlConnection(cs))[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']            {[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                SqlCommand mycommand = new SqlCommand(queryString, connection);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                try[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                {[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                    connection.Open();[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                    string word = (string)mycommand.ExecuteScalar();[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                    label14.Text = word;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                }[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                catch (Exception ex)[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                {[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                    MessageBox.Show(ex.Message);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']                }[/FONT][/COLOR][/B]
[COLOR=#666666][FONT='inherit']            }[/FONT][/COLOR]
 
Status
Not open for further replies.
Back
Top Bottom