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. what is ObjectSender and EventArgs in Windows Form

what is ObjectSender and EventArgs in Windows Form

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
7 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.
  • L Offline
    L Offline
    ldsdbomber
    wrote on last edited by
    #1

    In a simple dialog namespace projectname { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void FormDataChanged(object sender, EventArgs e) { blah......... } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { FormDataChanged(???????????, ?????????); } } } If I want to use the FormDataChanged function when I process the change in a listbox or a checkbox or whatever, what should I supply as the arguments. I suppose I have just dived in to C# and it's a bit different to what I am used to. I am not using either "sender" or "e" in the FormDataChanged function, I just want to know what is/should be in there, is it the item on the form itself that is the object, e.g. in the example above, is it the listbox itself, should it be "this" as the 1st parameter, and where does its EventArgs come from? thanks for any help If I put FormDataChanged(sender, e) that seems like it should be correct, but maybe I just need to find out a bit more about how the sender/args constructs work in more detail

    M L 2 Replies Last reply
    0
    • L ldsdbomber

      In a simple dialog namespace projectname { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void FormDataChanged(object sender, EventArgs e) { blah......... } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { FormDataChanged(???????????, ?????????); } } } If I want to use the FormDataChanged function when I process the change in a listbox or a checkbox or whatever, what should I supply as the arguments. I suppose I have just dived in to C# and it's a bit different to what I am used to. I am not using either "sender" or "e" in the FormDataChanged function, I just want to know what is/should be in there, is it the item on the form itself that is the object, e.g. in the example above, is it the listbox itself, should it be "this" as the 1st parameter, and where does its EventArgs come from? thanks for any help If I put FormDataChanged(sender, e) that seems like it should be correct, but maybe I just need to find out a bit more about how the sender/args constructs work in more detail

      M Offline
      M Offline
      Mirko1980
      wrote on last edited by
      #2

      In an event handler method, sender is the object that is generating the event, and e are event-specific arguments (for example a MouseClick event sends the mouse button that was clicked). If e is of type EventArgs, that means there are no specific argument for an event. If you don't need these informations, you can easily ignore the parameters.

      1 Reply Last reply
      0
      • L ldsdbomber

        In a simple dialog namespace projectname { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void FormDataChanged(object sender, EventArgs e) { blah......... } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { FormDataChanged(???????????, ?????????); } } } If I want to use the FormDataChanged function when I process the change in a listbox or a checkbox or whatever, what should I supply as the arguments. I suppose I have just dived in to C# and it's a bit different to what I am used to. I am not using either "sender" or "e" in the FormDataChanged function, I just want to know what is/should be in there, is it the item on the form itself that is the object, e.g. in the example above, is it the listbox itself, should it be "this" as the 1st parameter, and where does its EventArgs come from? thanks for any help If I put FormDataChanged(sender, e) that seems like it should be correct, but maybe I just need to find out a bit more about how the sender/args constructs work in more detail

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

        ldsdbomber wrote:

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { FormDataChanged(???????????, ?????????); }

        Hi, you should avoid calling event handlers directly; there are some alternatives: 1. some events can be fired by calling a specific method, such as button.PerformClick() 2. you can create a method that contains the bulk of the event handling and call that directly, without needing any special parameters. Example (see Timer_Tick method):

        private void checkbox1_CheckChanged(object sender, EventArgs e) {
        somethingHasChanged(sender as Checkbox);
        }

        private void listbox1_SelectionChanged(object sender, EventArgs e) {
        somethingHasChanged(sender as Listbox);
        }

        private void Timer_Tick(object sender, EventArgs e) {
        somethingHasChanged(null);
        }

        private void somethingHasChanged(Control c) {
        if (c!=null) Console.WriteLine("Something got changed relating to Control "+c.ToString());
        ...
        }

        :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        L 1 Reply Last reply
        0
        • L Luc Pattyn

          ldsdbomber wrote:

          private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { FormDataChanged(???????????, ?????????); }

          Hi, you should avoid calling event handlers directly; there are some alternatives: 1. some events can be fired by calling a specific method, such as button.PerformClick() 2. you can create a method that contains the bulk of the event handling and call that directly, without needing any special parameters. Example (see Timer_Tick method):

          private void checkbox1_CheckChanged(object sender, EventArgs e) {
          somethingHasChanged(sender as Checkbox);
          }

          private void listbox1_SelectionChanged(object sender, EventArgs e) {
          somethingHasChanged(sender as Listbox);
          }

          private void Timer_Tick(object sender, EventArgs e) {
          somethingHasChanged(null);
          }

          private void somethingHasChanged(Control c) {
          if (c!=null) Console.WriteLine("Something got changed relating to Control "+c.ToString());
          ...
          }

          :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          L Offline
          L Offline
          ldsdbomber
          wrote on last edited by
          #4

          Many thanks. Do you have any opinions as to a good reference book that would include this kind of information, i.e do I want a book on C# itself, or is it Windows Forms, or Visual Studio specific?

          L 1 Reply Last reply
          0
          • L ldsdbomber

            Many thanks. Do you have any opinions as to a good reference book that would include this kind of information, i.e do I want a book on C# itself, or is it Windows Forms, or Visual Studio specific?

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

            Sorry, no, I don't generally recommend specific books. Some people prefer a specific author, others a publisher. The books I own I have bought at various points in time, choosing from whatever collection was available at the nearest book store at that time. I often like the Microsoft books, including the Step-by-Step series (which is tutorial, not reference). :)

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Local announcement (Antwerp region): Lange Wapper? Neen!


            L 1 Reply Last reply
            0
            • L Luc Pattyn

              Sorry, no, I don't generally recommend specific books. Some people prefer a specific author, others a publisher. The books I own I have bought at various points in time, choosing from whatever collection was available at the nearest book store at that time. I often like the Microsoft books, including the Step-by-Step series (which is tutorial, not reference). :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              Local announcement (Antwerp region): Lange Wapper? Neen!


              L Offline
              L Offline
              ldsdbomber
              wrote on last edited by
              #6

              Sorry, what I meant was not a specific book, but a specific class of book. i.e. does a general purpose C# book contain details about controls and event handlers, or is that specific to windows forms etc. And for handling and tokenising strings and dialog data, are there specific types of books for that or... I realise this question is a bit wishy washy!

              L 1 Reply Last reply
              0
              • L ldsdbomber

                Sorry, what I meant was not a specific book, but a specific class of book. i.e. does a general purpose C# book contain details about controls and event handlers, or is that specific to windows forms etc. And for handling and tokenising strings and dialog data, are there specific types of books for that or... I realise this question is a bit wishy washy!

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

                What is in a book is what an author chooses to be there (the publisher permitting). Some books are purely language-oriented; others are technology oriented (e.g. database) but require a language for their examples, and may or may not offer an introduction to the language too. Windows Controls have existed for a long time, but they evolve, and their capabilities may be reduced or augmented by some framework (MFC, .NET, ...). My best advice is to go to the book store and make your own choice. That's what I do and recommend. :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                Local announcement (Antwerp region): Lange Wapper? Neen!


                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