Advertisement

05.09.2008 at 02:57PM PDT, ID: 23390928
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

C# windows label display with button

Tags: C#, windows
Good afternoon expert,

Studying C#. Looking at example 2 at
http://download.iticentral.com/DevCentral/pdf/intro_Csharp.pdf

Used this example to create console application which displays the sentence
'The apple varieties ar Macintosh, Granny, and Cortland.' Inane to be sure
but hey, you gotta start somewhere.

I hate console applications so I morphed the console app into a Windows
appl using a button and a label. I kept the public class containing an instance
variable and a contruct with the same name as the class in a separate
file and the two files which do this are callled AppleMain.cs and Apple.cs
below.

Trying to go the next step create a windows app where a button displays the varieties.
I can get it to work as a single file, Apple_Win_Btn.cs, below. Attempted to
create a separate cs file in which a public class called Apple is created and objects
are created like Apple mac = new Apple(Macintosh") and then those objects are referred to
in onClick method like label1.Text = ("The apple varietys are " + Apple.Apple.Mac.Text);
and a couple of other ways but I can't get it to work. This gives me an error
No overload for method 'Apple' takes '1' arguments   and
Apple.Apple' does not contain a definition for 'Apple'

How can I separate Apple class and refer to it in the form method?

Thanks.

Allen in Dallas

+++++++++++++AppleMain.cs+++++++++++++++
// using apple console application with Windows message box

using System;
using System.Windows.Forms;

namespace Apple
{
      class AppleMain
      {
            // main entry point for application
            static void Main( string[] args )
            {
                  //
                  // TODO: Add code to start application here
                  //

                  Apple mac = new Apple("Macintosh");
                  Apple gra = new Apple("Granny Smith");
                  Apple cor = new Apple("Cortland");
                  mac.outputVariety();
                  gra.outputVariety();
                  cor.outputVariety();

//      MessageBox.Show( "Apple varietys are " + mac.outputVariety() + "," + gra.outputVariety() + ","       + cor.outputVariety(), MessageBoxButtons.OK, MessageBoxIcon.Information );
MessageBox.Show( "The apple varietys are " + mac.outputVariety() + ", " + gra.outputVariety() + ", " + cor.outputVariety(), "Display apple varietys", MessageBoxButtons.OK, MessageBoxIcon.Information );

            } // end Main

      } // end class AppleMain

} // end namespace Apple


+++++++++++++Apple.cs+++++++++++++++
using System;

namespace Apple
{
      public class Apple
      {
            private string variety = "";
            public Apple(string appleVariety)
            {
                  this.variety = appleVariety;
            }
            public string outputVariety()
            {
            return this.variety;
                  //System.Console.WriteLine(variety);
                        
            }
      }
}


++++++++++++++++++Apple_Win_Btn.cs+++++++++++++++++
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Apple
{
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button button1;
            public System.Windows.Forms.Label label1;
            private System.ComponentModel.Container components = null;
            public Form1()
            {
                  // Required for Windows Form Designer support
                  InitializeComponent();
            }
            /// Clean up any resources being used.
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }
            #region Windows Form Designer generated code
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            private void InitializeComponent()
            {
                  this.button1 = new System.Windows.Forms.Button();
                  this.label1 = new System.Windows.Forms.Label();
                  this.SuspendLayout();
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(80, 16);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(168, 23);
                  this.button1.TabIndex = 0;
                  this.button1.Text = "Display apple varieties";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // label1
                  //
                  this.label1.Location = new System.Drawing.Point(32, 64);
                  this.label1.Name = "label1";
                  this.label1.Size = new System.Drawing.Size(272, 24);
                  this.label1.TabIndex = 2;
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(328, 110);
                  this.Controls.Add(this.label1);
                  this.Controls.Add(this.button1);
                  this.Name = "Form1";
                  this.Text = "Display apple varieties";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }
            public string varietiesMac;      
            public string varietiesGra;
            public string varietiesCor;
            
            private void button1_Click(object sender, System.EventArgs e)
            {
                  varietiesMac = "Macintosh";
                  varietiesGra = "Granny";
                  varietiesCor = "Cortland";
                                               //label1.Text = ("The apple varietys are Macintosh, Granny and Cortland");
            label1.Text = ("The apple varietys are " + varietiesMac + ", " +       varietiesGra + ", " + varietiesCor + ".");
            }
      }
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: 9apit
Solution Provided By: josgood
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
05.09.2008 at 03:17PM PDT, ID: 21536721

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:22PM PDT, ID: 21536748

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:43PM PDT, ID: 21536850

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:46PM PDT, ID: 21536859

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:50PM PDT, ID: 21536880

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.09.2008 at 03:17PM PDT, ID: 21536721

Rank: Master

button1_Click needs to be a member of Form1.  
Accepted Solution
 
05.09.2008 at 03:22PM PDT, ID: 21536748

Rank: Master

I built it with the files you specified, and placed button1_Click in Form1.  This version works fine for me...

      private void button1_Click(object sender, System.EventArgs e)
      {
         Apple mac = new Apple("Macintosh");
         Apple gra = new Apple("Granny Smith");
         Apple cor = new Apple("Cortland");
         MessageBox.Show("The apple varietis are " + mac.outputVariety() + ", " +  gra.outputVariety() + ", " + cor.outputVariety() + ".");
      }
 
05.09.2008 at 03:43PM PDT, ID: 21536850

Rank: Guru

From your description it sounds like you tried to create another class named Apple?
 
05.09.2008 at 03:46PM PDT, ID: 21536859

Rank: Master

Yes.  It is a copy of 9apit's code, except for a change of namespace, since I was inserting it into a demo form I already had.

using System;
using System.Collections.Generic;
using System.Text;

namespace QuickCSharpForm {
   class Apple {

      private string variety = "";
      public Apple(string appleVariety) {
         this.variety = appleVariety;
      }
      public string outputVariety() {
         return this.variety;
      }
   }
}
 
05.09.2008 at 03:50PM PDT, ID: 21536880

Rank: Guru

My question was for 9apit :))

The code he posted does actually work, and that is what he said in his post. He didn't post the non-working code...
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628