Newbie question about events

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1080 Views - Last Post: 27 March 2012 - 08:34 AM Rate Topic: -----

#1 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-March 12

Newbie question about events

Posted 15 March 2012 - 07:09 PM

Sorry for these simple questions. I added the following event handler to my file, but it doesn't fire. Is there someplace else it should be? The only events that work are the ones that were created by double clicking on the control in the designer. But there is no way to click on a node in the treeview during design time.

        void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string msgString;
            msgString = e.Node.Text;
            MessageBox.Show(msgString, "Selected Node");
        }



I am new to Visual Studio 10 and really, really new to c#.

Is This A Good Question/Topic? 0
  • +

Replies To: Newbie question about events

#2 MasterOfBinary  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 33
  • Joined: 02-March 12

Re: Newbie question about events

Posted 15 March 2012 - 07:22 PM

Put this code after you make the TreeView:

treeView1.OnNodeMouseClick += new NodeMouseClickEventHandler(treeView1_NodeMouseClick);

Was This Post Helpful? 0
  • +
  • -

#3 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-March 12

Re: Newbie question about events

Posted 15 March 2012 - 07:44 PM

Thanks MasterOfBinary. I tried this but it is generating errors. I tried it in the frmParent.Designer.cs where the rest of the toolstrips, imageLists, etc. Should I put it in the frmParent.cs in the constructor? I'll try that. But it would be great if you can lead me a little more!
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Newbie question about events

Posted 15 March 2012 - 07:51 PM

These tutorials should help.


Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 0
  • +
  • -

#5 MasterOfBinary  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 33
  • Joined: 02-March 12

Re: Newbie question about events

Posted 15 March 2012 - 07:54 PM

Actually Visual Studio can do the stuff for you, just double-click NodeMouseClicked in the event list. You don't have to have nodes added already.
Was This Post Helpful? 1
  • +
  • -

#6 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-March 12

Re: Newbie question about events

Posted 15 March 2012 - 08:04 PM

OK. So I see how things are structured, but the program doesn't like your code. This is a very picky environment!
See if I understand this. The Designer is where the code goes to create the items that appear on the form. The code in the other cs is for the subroutines etc. I don't think I'm using the proper words to describe this. In my case I have frmParent.Designer.cs and frmParent.cs.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Newbie question about events

Posted 15 March 2012 - 08:09 PM

Before trying to build a program from scratch, you probably should work your way through any of the numerous "Learn C# in month" type self-teaching books. Not only will you learn the basics of C#, but also the concepts behind OOP and walking through their lessons will also walk you through Visual Studio so you can learn the IDE as well.

There are a few books in these resources that are all about Visual Studio, not just programming. VS has a lot of power in it and if you don't take the time to learn that you are just making things hard for yourself.


For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler


See FAQ # 23. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



This post has been edited by tlhIn`toq: 15 March 2012 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 MasterOfBinary  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 33
  • Joined: 02-March 12

Re: Newbie question about events

Posted 15 March 2012 - 08:21 PM

View PostScottinTexas, on 15 March 2012 - 08:04 PM, said:

OK. So I see how things are structured, but the program doesn't like your code. This is a very picky environment!
See if I understand this. The Designer is where the code goes to create the items that appear on the form. The code in the other cs is for the subroutines etc. I don't think I'm using the proper words to describe this. In my case I have frmParent.Designer.cs and frmParent.cs.

Sorry my code was wrong. It should be:
treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);

But read tlhIn`toq's post, and if you're still wanting to do this use the list of events like I said so Visual Studio can do the dirty work for you. No need to do things yourself when someone else can do it for you. :)
Was This Post Helpful? 1
  • +
  • -

#9 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-March 12

Re: Newbie question about events

Posted 15 March 2012 - 08:29 PM

Got it! I figured out what I did wrong with your code and fixed my frmParent.Designer.cs. All is moving forward in the quest to learn C#!
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Newbie question about events

Posted 15 March 2012 - 08:37 PM

Notice in the designer.cs code how Visual Studio put in a comment saying...

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()



At this point in your self-education you should probably follow the warning messages. As MasterOfBinary mentioned, just let VS do it for you.

Attached Image
Was This Post Helpful? 2
  • +
  • -

#11 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-March 12

Re: Newbie question about events

Posted 25 March 2012 - 07:18 AM

It's been a busy few days since I posted this. I wanted to get back and say thanks for the input. Getting used to finding things in the environment is half the battle.

I have a VBA project that I started writing 6 years ago that does most of what I am trying to implement in this program. I just want it to be better, cleaner, and what I can only describe as "tighter." The relational database now has a few thousand records and I use it everyday at work. This is a private program. No one else will use it and no one else has access to the data. They use index cards and other people to do their typing for them. They organize their data by sorting the cards in "Effective Date" order. I just have my program do that for me. I have reports that are automatically generated, etc.

I appreciate what tlhIn`toq says about finding and using the "Learn C# in 21 Days" books (I happen to have one for C++ and a book on "Using Visual C++ Version 4"). I also bought Wrox Visual C# book a few weeks ago and read that. Unfortunately they are all repeats of the OOP I studied in school when we moved from Fortran IV to Pascal. The examples are console applications and don't touch on real life situations and the Wrox book does not discuss data access except by using the wizard. Sometimes that is not enough. So far Hastings and Barnes and Nobel doesn't have a book that is anything more than what I got from Wrox. Still looking though.

Since I have not worked as a programmer in over 12 years I have not kept up on new technologies. And even then I was working on proprietary web sites for Dow Chemical which used ASP. The VBA application is and has been a work in progress from the beginning. It started as a simple form to accept my typed data and has grown. Therefore it was not designed properly and has become a hodge podge of routines that were written on an as needed basis.

So all of that was to first say thanks to those of you who provide answers to my questions. It is forums like this that make it possible me to learn new programming languages. Usually the questions I have are based on running into a brick wall and not being able to find the answer anywhere. Microsoft's MSDN web site is OK, but sometimes they just don't have the answer because it hasn't been written yet. Especially for Visual Studio 2010.

This post has been edited by tlhIn`toq: 25 March 2012 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Newbie question about events

Posted 25 March 2012 - 07:29 AM

Quote

This is a private program. No one else will use it and no one else has access to the data. They use index cards and other people to do their typing for them. They organize their data by sorting the cards in "Effective Date" order. I just have my program do that for me. I have reports that are automatically generated, etc.


Sounds like you should plan as if your program won't always be private. Once your co-workers see its power they might be begging for copies of it. It might turn out to be your first product for sale.

So I have to ask out of personal curiosity: Did you buy paper books from the Guttenberg era, or did you buy eBooks for a reader like the iPad? I love having all my coding books on my iPad. So much easier to keep and carry a few dozen huge tomes.
Was This Post Helpful? 0
  • +
  • -

#13 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-March 12

Re: Newbie question about events

Posted 27 March 2012 - 05:34 AM

Quote

So I have to ask out of personal curiosity: Did you buy paper books from the Guttenberg era, or did you buy eBooks for a reader like the iPad? I love having all my coding books on my iPad. So much easier to keep and carry a few dozen huge tomes.


I am an updated modern kind of guy. I have a Kindle my daughter bought for me a couple of years ago. I got the Wrox C# book on that. But I have a book shelf that is bending under the weight of several references on all sorts of programming. But most of them are now obsolete. One day I'll get the nerve to throw them away and replace them with eBooks.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,710
  • Joined: 08-June 10

Re: Newbie question about events

Posted 27 March 2012 - 05:57 AM

Don't throw them away, shelve ornaments from a previous era are pretty nice! You just tend to accumulate them faster in a fast-moving field like programming. But if you need to get rid of them, donate them to a library, or at least to a half-priced bookstore or something. Someone'll get some use out of them.
Was This Post Helpful? 0
  • +
  • -

#15 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • View blog
  • Posts: 547
  • Joined: 07-July 10

Re: Newbie question about events

Posted 27 March 2012 - 07:29 AM

The books can still be useful to someone. When I went to school in 07, my mom gave me her old VB3 book. (She never learned to program but still had the book). It was able to help me learn some of the core concepts of OOP even though the actual code in it was outdated.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2