0

I'm working on a project which generates a random number by taking a input from a textBox then after the click of a button, it generates the number and puts it in a Label. I want to put the text like this:

public void Button_Click(object sender, RoutedEventArgs e)
{
    string inputstr;
    inputstr = textBox1.Text;
    int inputnum;
    inputnum = Convert.ToInt32(inputstr);
    var me = new MainWindow();
    Random rnd = new Random();
    int outnum = rnd.Next(inputnum);
    string outnum1 = outnum.ToString();
    Label.Text = outnum1;
}

but it gives an error which is:

Severity Code Description Project File Line Suppression State Error CS0117 'Label' does not contain a definition for 'Text' RandomNumber C:\Users\Nameless\source\repos\RandomNumber\RandomNumber\MainWindow.xaml.cs 37 Active

I would be really thankful to anyone who helps.

  • 2
    `Label` is usually the class name. what's the _property_-name of your label? – Franz Gleichmann Jul 20 '21 at 09:13
  • you need ot set the name of the label(for example `label1.Text`) and not The `Label` class itself – styx Jul 20 '21 at 09:19
  • 3
    If the name of the component is `Label`, you should set it's `Content` property. `TextBlock` has a `Text` property, Label doesn't. – Jeroen van Langen Jul 20 '21 at 09:22
  • 1
    Wherever you got your example from, it's for WinForms NOT WPF. [puny winforms](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.label?view=net-5.0#examples) vs. [WPF](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.label?view=net-5.0#examples) – XAMlMAX Jul 20 '21 at 09:27
  • 1
    @XAMlMAX You mean also the RoutedEventArgs and the MainWindow? You're certainly wrong. OP is just stumbling upon the fact that a WPF Label has no Text property. – Clemens Jul 20 '21 at 09:30
  • @Clemens I meant the .Text. The links I posted, link to examples of both Labels and the assignment of text and content properties respectively. – XAMlMAX Jul 20 '21 at 09:31

3 Answers3

0

Several issues/points about your code.

Label.Text

You're trying to assign the Text property to the class itself, that doesn't work. You have to assign the Content property to your instance, for example a label with ID outputLabel. This becomes: outputLabel.Content = "Whatever...";.

Random not being random (enough)

Check this question on StackOverflow: Random number generator only generating one random number

When declaring a Random instance inside your method, it won't be as random as you might want it to be. At least eclare it like following (even better would be from the answer in thlinked question):

private readonly Random rnd = new Random();

public void Button_Click(object sender, RoutedEventArgs e)
{
    int outnum = rnd.Next();
}

Inline declaration

Your whole code could be shortened by declaring and assigning variables in one line of code:

private readonly Random rnd = new Random();
public void Button_Click(object sender, RoutedEventArgs e)
{
    string inputstr = textBox1.Text;
    int inputnum = Convert.ToInt32(inputstr);
    int outnum = rnd.Next(inputnum);
    yourLabel.Content = outnum.ToString();
}

Other points

  • Why type var me = new MainWindow(); when you never use it?
  • You could use var to declare variables
  • You should validate input when converting from string to int
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abbas
  • 14,186
  • 6
  • 41
  • 72
-1

Try this one:

I just change Label to label1

and comment out var me statement

 private void button1_Click(object sender, EventArgs e)
            {
                string inputstr;
                inputstr = textBox1.Text;
                int inputnum;
                inputnum = Convert.ToInt32(inputstr);
                //var me = new MainWindow();
                Random rnd = new Random();
                int outnum = rnd.Next(inputnum);
                string outnum1 = outnum.ToString();
                label1.Text = outnum1;
            }
Yogita Kumar
  • 262
  • 2
  • 7
-2
  • Waarom een ​​nieuw MainWindow maken?
  • Geef de link naar de tag door

<Label x:Name="label1"/>

Error-> label1.Text = "abc";

Right -> label1.Content="abc";
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
Title_
  • 11
  • 3