Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my class code

C#
public Label Createlabel()
          {
              Label intervillabel = new Label();
              intervillabel.Text = numericUpDown1.Value.ToString();
              intervillabel.Location = new Point(BeatDetector.Location.X, 0);
              intervillabel.AutoSize = true;
              intervillabel.Visible = true;

              return intervillabel;

          }


say this is my other code, this will add the label to the panel

C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
             if (e.KeyCode == Keys.ControlKey)
        {
            panel1.Controls.Add(Createlabel());

        }
    }



this is the the hard part of the code
beatdetector = picurebox



C#
string detectstring = "";
       System.Media.SoundPlayer player = new System.Media.SoundPlayer();
       private void BeatDetector_Move(object sender, EventArgs e)
       {
        //see if the label is a number or not
               if (BeatDetector.Location.X == Createlabel().Location.X)
               {

                   if (Createlabel().Location == new Point (0,Createlabel().Location.Y))
                   {//MessageBox.Show("NO");

                           //int anInteger;
                           //anInteger = Convert.ToInt32(Createlabel().Text);
                           //anInteger = int.Parse(Createlabel().Text);

                           //move = anInteger;
                       detectstring = Createlabel().Text;
                       string Str = detectstring.Trim();
                           double Num;
                           bool isNum = double.TryParse(Str, out Num);
                           if (isNum)
                           {
                               MessageBox.Show(Num.ToString());
                           }
                       else
                       { //making the label beats
                             MessageBox.Show("Invalid number");
                           if (Createlabel().Text == "KICK2")
                           {
                               SoundPlayer player = new SoundPlayer(Properties.Resources.KICK2);
                               player.Play();

                           }
                       }
                   }



                 }
       }


whats ment to happen is when the picure goes over the label it will see if the label is a text or a number, if it is a number it will convert it int to a number and a message bot will show up saying its conveted into a number and I can use the coverted number for anything, but for some reason nothing happens.

PS: I have a draggable beatdetecor that I drag around the panel
Posted
Comments
Timberbird 6-Jun-12 5:18am    
Er... is this the actual code you use (particulary first code sample)? Is it is, then each call of Createlabel() simply creates a new label control, not even adding it to fotms control. I.e. you just have object (actually, a lot of objects, as you call Createlabel() many times) somewhere in memory and nothing on the form. Instead, you could enumerate all the form's controls, check whether they are labels and if yes - well, then you can start coordinates check
[no name] 6-Jun-12 5:27am    
can you please give me a little example, so I better understand this visually?
Timberbird 6-Jun-12 5:59am    
Something like that (may contain errors, I just wrote it in notepad):

foreach (Control pControl in panel1.Controls)
{
Label pLabel=pControl as Label;
if (null != pLabel)
{
// and here goes your logic:
if (BeatDetector.Location.X == pLabel.Location.X)
{
if (pLabel.Location == new Point (0, pLabel.Location.Y)) // strange check, BTW
{
...
}
}

}
}
Timberbird 6-Jun-12 6:06am    
One more note: I haven't worked with WinForms controls much, but if you need to check whether two controls overlap, aren't there some convenient functions for that in .NET? For example, you could determine controls' bounds and then use Rectangle.Intersect() - http://msdn.microsoft.com/en-us/library/y10fyck0.aspx
[no name] 6-Jun-12 6:42am    
About the strange check, im checking if the the beatdectr is at the same location as the label, then for the 2nd check im checking if the label is on the 0 because I have other labels that are added on the other X

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