Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

There are 10 textboxes and 1 button. The user is going to enter 1 number into each textbox and then click the button to find the highest number.

How can I find the highes number among 10 numbers?

This is the code but it didn't work. Is there any different way?

C#
private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")
        {
            MessageBox.Show("Please enter a number...!");
        }
        else
        {
            int[] d = new int[groupBox1.Controls.Count];
            for (int i = 0; i < groupBox1.Controls.Count; i++)
            {
                if (groupBox1.Controls[i] is TextBox)
                {
                    TextBox t = groupBox1.Controls[i];
                    d[i] = int.Parse(t.Text);
                }
            }
            Array.Sort(d);
            Array.Reverse(d);

        }
    }



Regards

Haluk
Posted
Updated 19-Dec-12 8:20am
v5
Comments
joshrduncan2012 19-Dec-12 14:16pm    
Search for maximum, depending on how the numbers are stored?
lewax00 19-Dec-12 14:21pm    
What do you mean by "it didn't work"? Did it throw an exception, did it return the wrong value, something else?
haluk_78 19-Dec-12 14:25pm    
TextBox t = groupBox1.Controls[i];
Cannot implicitly convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.TextBox'. An explicit conversion exists (are you missing a cast?)

I agree with Christian, but I couldn't resist posting a more elegant solution than the others.

C#
int max = (from c in this.Controls.Cast<Control>()
           where c is TextBox && Regex.IsMatch(c.Text.Trim(),"^[0-9]{1,10}$")
           select int.Parse(c.Text.Trim())).Max();
 
Share this answer
 
v3
Comments
Christian Graus 19-Dec-12 15:49pm    
Wow - that IS nice
BC @ CV 19-Dec-12 15:50pm    
Thanks, that means a lot coming from you.
CPallini 19-Dec-12 15:56pm    
:-) 5.
BC @ CV 19-Dec-12 15:59pm    
Grazie signore.
OK, so three people have done your homework for you, so I feel I can weigh in. Saying something 'didn't work', is useless. Especially when it threw an exception, how were we supposed to know that ? An exception means that your code doesn't run. It is telling you something specifically is wrong in your code. Your general idea would work ( although it's not optimal ). So, what you really wanted to know was 'how do I fix this exception' ? And the exception was very clear as to what was wrong. You used 'is' to work out that the control was a textbox, but then you accessed it, as a control. You need to use 'as' to cast it to a textbox, and then access the text in the textbox. Overall, this code was very convoluted, and used some complex topics for someone who doesn't know what an exception is. Instead of piecing code together from snippets you learn here and there, without learning basics, you should buy a book and work through it, so you learn basics first and don't miss major parts of what you need to know to be a programmer
 
Share this answer
 
Comments
Abhishek Pant 19-Dec-12 16:32pm    
+5
Change:
C#
TextBox t = groupBox1.Controls[i];


to:
C#
TextBox t = (TextBox)groupBox1.Controls[i];


or

C#
TextBox t = groupBox1.Controls[i] as TextBox;
 
Share this answer
 
C#
List<sayilar> liste = new List<sayilar>();
           foreach (Control st in panel1.Controls)
           {
               int deger = Convert.ToInt32(st.Text);
               liste.Add(new Sayilar(deger));
           }
           int enBuyuk = (from m in liste select m.Sayi).Max();
           MessageBox.Show(enBuyuk.ToString()); }

   public class Sayilar
   {
       public int Sayi
       {
           get;
           set;
       }

       public Sayilar(int sayi)
       {
           this.Sayi = sayi;
       }
   }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900