locked
Hello World RRS feed

  • Question

  • I am attempting to check whether or not my buttons work by getting them to produce the message "Hello World", I have a form with a number on buttons on it, two of which are named btn_Close and btn_Convert, my code is this:

     

     

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Windows.Forms;
    
    
    
    namespace Attempt1
    
    {
    
        public partial class form : Form
    
        {
    
            public form()
    
            {
    
                InitializeComponent();
    
            }
    
    
    
    
    
    
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
    
    
            }
    
    
    
            private void btn_Convert_Click(object sender, EventArgs e)
    
            {
    
                MessageBox.Show("hello world");
    
            }
    
    
    
            private void btn_Close_Click(object sender, EventArgs e)
    
            {
    
                MessageBox.Show("hello world");
    
            }
    
        }
    
    }
    

     

    Currently only when I push on the btn_Close button do I get a msgbox saying Hello World. Clicking on btn_convert appears to do nothing.
    Could someone please explain why? I have made sure that the button names match the names in the code.
    Thanks,
    Seb

    Wednesday, May 4, 2011 4:54 PM

Answers

  • Probably, you have linked the button and the event handler. Right click on your button thats not working and select its properties.

    In the properties, there is a lightning bolt symbol, which is for Events. In that check if the click event is given some sort of value. If it is, then we got some other issue to think of. If not, then it means that you havent tied the event to the event handler.

    Alternatively, look at your source code for the button that isnt working. Compare it with the button that is working. You will notice that there is something like:

    Click = "someEventHandlerName"

    • Marked as answer by Sebster10 Wednesday, May 4, 2011 5:28 PM
    Wednesday, May 4, 2011 5:14 PM

All replies

  • It seems the even Button Click is nor called at all.

    But a break point on the row (private void btn:Convert_Click(...);) and check if the code stops there when you click on the button. I am 100% that is does not - that means that the event is not rised (called).

    Wht you have to do (I assume you are a beginner), delete button in the designer form and delete that event.

    Then start all over again! It has to work!

    Why the button Click event is not called: There is something wrong with the code; you had to rename something (or button name, or even t name, or..). All the code does VS it self instead of you, and when you rename something, it will later not work. So you have to be careful with the name. You can check the form Designer class, to see the names if they are ok or not.

     

    You can try this code bellow, if you want to learn how to create your own controls (button in this case) and execite its event:

      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          Button btnConvet = new Button();
          btnConvet.Name = "buttonConvert";
          btnConvet.Location = new Point(20, 20);
          btnConvet.Text = "Convert button";
          btnConvet.Size = new Size(120, 25);
          btnConvet.Click += new EventHandler(btnConvet_Click);
          this.Controls.Add(btnConvet);
        }
    
        private void btnConvet_Click(object sender, EventArgs e)
        {
          MessageBox.Show("This is the new button clicked", "Attention");
        }
      }

     

     


    Mitja
    Wednesday, May 4, 2011 4:57 PM
  • In the designer: Check that the click event is connected to that function. Just select the button and look at the properties window. Select to see the events....

    Or just double click the button in the designer and it will create a function for you that handles the button click.

    With kind regards,

    Konrad

    Wednesday, May 4, 2011 5:03 PM
  • Hello Sebster1 from World,

    Your event is not wired correctly. My guess is that you coded it by hand. To be sure, in the designer, double-click the button and it should auto-generate the event for you and take you into the code view. In the process, it will automatically add the handler for you in the designer file which wires the control to the event.

    Adam


    Ctrl+Z
    Wednesday, May 4, 2011 5:05 PM
  • Probably, you have linked the button and the event handler. Right click on your button thats not working and select its properties.

    In the properties, there is a lightning bolt symbol, which is for Events. In that check if the click event is given some sort of value. If it is, then we got some other issue to think of. If not, then it means that you havent tied the event to the event handler.

    Alternatively, look at your source code for the button that isnt working. Compare it with the button that is working. You will notice that there is something like:

    Click = "someEventHandlerName"

    • Marked as answer by Sebster10 Wednesday, May 4, 2011 5:28 PM
    Wednesday, May 4, 2011 5:14 PM
  • Thank you for the fast replies :)

    Yes, I am a beginner, I tried as you guys suggested and double clicked the btn_Convert button.

    And it came up with this;

     

            private void btn_Convert_Click_1(object sender, EventArgs e)
    
            {
    
    
    
            }
    

     

     

    Of course I added            

    MessageBox.Show("hello world");


    and it worked! This has rather confused me, however as my button is named exactly "btn_Convert", where did the "_1" come from?
    Wednesday, May 4, 2011 5:17 PM
  • It came from the place storks bring babies from. :)

    Ok seriously, the IDE is smart enoughtto know that the procedure/event already existed with that name...BUT you were missing the handler code in the designer file, SO it appended the _1 which you'll see in the designer file if you click the Show All Files icon at the top of the solution explorer and double-click the designer file that appears for that form.

    Adam


    Ctrl+Z
    Wednesday, May 4, 2011 5:20 PM
  • The button had no event handler for the click event. So when you double clicked the button, Visual Studio created a new event handler.

    But a function with the default name already existed, so it looked for the _1 one. If that existed, too, a _2 one would have been created.

    Just have a look at the properties next so you see the events available and that it is connected. Maybe that way you learn something new.

    With kind regards,

    Konrad

    Wednesday, May 4, 2011 5:22 PM
  • Whenever you double click on a button, it tries to tie the button with an event handler in the code behind (since a button is generally used for doing something by clicking it).

    Now, it appears that at certain point of time, you double clicked the button and then either deleted the linker in the source code or from the properties window. That just removes the link, but doesnt remove the code behind that was generated. Or the other case would be that, by seeing the first button event handler, you - yourself wrote the event handler in the code - behind, but didnt take care of the linking part.

    So, when you double clicked the button sometime back, it tried to create a button click event. But since there was an event with the exact same name, it appended 1. If you double click the button, it would take you to this code. In case you are interested, try to remove the link (while letting the code behind stay) and again double click on the button.

    You will find that, it generates one more event handler with 2 as the postfix.

    Hope it helps.

    Wednesday, May 4, 2011 5:28 PM
  • Ah! Thank you very much.

    I did not realise that the lightning symbol was the event box, I was search through properties looking for some value named events. I got it working now thank you :)

    Wednesday, May 4, 2011 5:30 PM
  • Glad to know :)
    Wednesday, May 4, 2011 5:36 PM
  • I have another problem and will most likely be having more, should I make a new thread for each problem or keep it all contained in one?

    I am attempting to display a calculated integer through a msgbox with some text; currently this displays "hello world" in a text box with 60 as the title of the msgbox.

     

      private void btn_Convert_Click(object sender, EventArgs e)
      {
       int x = 2 * 30;
    
       MessageBox.Show("hello world", x.ToString());
    
       
      }
    

     

    Any advice on how to make it simply appear as "Hello world 60"

     

    Thanks

     


    • Edited by Sebster10 Wednesday, May 4, 2011 6:04 PM 360 was meant to be 60
    Wednesday, May 4, 2011 5:52 PM
  • When you type in a statement, the visual studio intellisense is your best friend.

    Once you type "(" - you should be able to see the intellisense stating the parameter that the message box takes, the type of it and a general description of what it does. In case you are unable to see this, then just hit CONTROL + SPACE.

    You will realise that the first argument that the message box takes is a string- the string that is going to be displayed in the message box, while the second one is the title of the messagebox and so on (as you go on adding COMMAS).

    So, in your present case: you message is - "hello world" and your title is - "60"

    Since you want "Hello World 60" to appear, you need to make that whole thing as a message string by doing:

    MessageBox.Show ( "Hello World " + x.ToString() );

    Wednesday, May 4, 2011 5:59 PM
  • Hi,

    I would create new threads for each problem when it occurs. And try to find a good description each time for the subject.

    You can concatenate strings with a simple +. So something like this works:

    MessageBox.Show("hello world " + x.ToString(), "Some Title");

    Or you can use the String.Format function:

    MessageBox.Show(String.Format("Hello World {0}", x), "Some Title");

    With kind regards,

    Konrad

    Wednesday, May 4, 2011 5:59 PM
  • Thank you I got it working  :)

    I will try using visual studio intellisense.

    Wednesday, May 4, 2011 6:08 PM