Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to ceate event_click for button with C# ? [modified]

how to ceate event_click for button with C# ? [modified]

Scheduled Pinned Locked Moved C#
helpquestioncsharptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xingselex
    wrote on last edited by
    #1

    my code is here . what is the misstake ? it error when click on that button and message that "the object reference not set to an instand of an object". please give me some help void Button_Click(object sender, EventArgs e) { Button clickedButton = sender as Button ; if (clickedButton.Name == "Program") { MessageBox.Show("lkjd;lfjal;sdkjf;laksdjfl;jasd"); } else { MessageBox.Show("dddddddddddddddd"); } }

    modified on Thursday, May 21, 2009 3:27 PM

    L L 2 Replies Last reply
    0
    • X xingselex

      my code is here . what is the misstake ? it error when click on that button and message that "the object reference not set to an instand of an object". please give me some help void Button_Click(object sender, EventArgs e) { Button clickedButton = sender as Button ; if (clickedButton.Name == "Program") { MessageBox.Show("lkjd;lfjal;sdkjf;laksdjfl;jasd"); } else { MessageBox.Show("dddddddddddddddd"); } }

      modified on Thursday, May 21, 2009 3:27 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, if that handler gets connected to an event of something other than a Button, then Button clickedButton = sender as Button ; will set clickedButton to null. You can take care of that by changing your code to:

      ...
      if (clickedButton!=null && clickedButton.Name == "Program")
      ...

      or by making absolutely sure only Buttons are being wired up to this handler. FWIW: the fact that the handler's name starts with "Button" is fine for readability but does not change the way the code works. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      1 Reply Last reply
      0
      • X xingselex

        my code is here . what is the misstake ? it error when click on that button and message that "the object reference not set to an instand of an object". please give me some help void Button_Click(object sender, EventArgs e) { Button clickedButton = sender as Button ; if (clickedButton.Name == "Program") { MessageBox.Show("lkjd;lfjal;sdkjf;laksdjfl;jasd"); } else { MessageBox.Show("dddddddddddddddd"); } }

        modified on Thursday, May 21, 2009 3:27 PM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Are you sure the Button you are clicking on is a Button (and not some other control).

        ___________________________________________ .\\axxx (That's an 'M')

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups