Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Calling button click Events in classes other than the form class

$
0
0

I have a program that I need the click events for buttons in another class other than than the form class.  I have all my code below (which I just realized I posted earlier in the wrong MSDN forum)

How would I go about calling the events in the other class when the button is clicked?

Thank you.

frmExamScoreFile (the actual form)

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Configuration;usingSystem.Windows;namespace files{publicpartialclass frmExamScoreFile :Form{public frmExamScoreFile(){InitializeComponent();}privatevoidForm1_Load(object sender,EventArgs e){}privatevoid btnNext_Click(object sender,EventArgs e){}privatevoid btnNew_Click(object sender,EventArgs e){}privatevoid btnOpen_Click(object sender,EventArgs e){}privatevoid btnClose_Click(object sender,EventArgs e){}privatevoid btnAdd_Click(object sender,EventArgs e){}}}

Indicies

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;
using System.IO;
using System.Configuration;
using System.Windows;


namespace files
{
   public partial class Indicies : frmExamScoreFile
   {
      protected int TextBoxCount = 2;

      public enum TextBoxIndicies
      {
         NAME,
         SCORE
      }

      public Indicies()
      {
        InitializeComponent();
      }

      public void ClearTextBoxes()
      {
          txtName.Text = "";
          txtScore.Text = "";
      }

      public void SetTextBoxValues(string[] values)
      {
         if (values.Length != TextBoxCount)
         {
            throw (new ArgumentException("There must be " + (TextBoxCount + 1) + " strings in the array"));
         }

         else
         {
            txtName.Text = values[(int)TextBoxIndicies.NAME];
            txtScore.Text = values[(int)TextBoxIndicies.SCORE];
         }
      }

      public string[] GetTextBoxValues()
      {
         string[] values = new string[TextBoxCount];

         values[(int)TextBoxIndicies.NAME] = txtName.Text;
         values[(int)TextBoxIndicies.SCORE] = txtScore.Text;

         return values;
      }
   


   }
}

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace files
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmExamScoreFile());
        }
    }
}

ReadFile

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows;

namespace files
{
   public partial class ReadFile : Indicies
   {
      private StreamReader fileReader;

      public ReadFile()
      {
         InitializeComponent();
      }

      private void btnOpen_Click(object sender, EventArgs e)
      {
         DialogResult result;
         string fileName;

         using (OpenFileDialog fileChooser = new OpenFileDialog())
         {
            result = fileChooser.ShowDialog();
            fileName = fileChooser.FileName;
         }

         if (result == DialogResult.OK)
         {
            ClearTextBoxes();

            if (fileName == string.Empty)
               MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            else
            {
               try
               {
                  FileStream input = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                  fileReader = new StreamReader(input);

                  btnOpen.Enabled = false;
                  btnNext.Enabled = true;
               }

               catch (IOException)
               {
                  MessageBox.Show("Error reading from file", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
            }
         }
      }

      public void btnNext_Click(object sender, EventArgs e)
      {
         try
         {
            string inputRecord = fileReader.ReadLine();
            string[] inputFields;

            if ( inputRecord != null )
            {
               inputFields = inputRecord.Split(',');

               Record record = new Record( Convert.ToString(inputFields[0]), Convert.ToInt32(inputFields[1]) );

               SetTextBoxValues ( inputFields );
            }

            else
            {
               fileReader.Close();
               btnOpen.Enabled = true;
               btnNext.Enabled = false;
               ClearTextBoxes();

               MessageBox.Show("No more records in file");

            }
         }
         catch ( IOException )
         {
            MessageBox.Show("Error Reading from File");
         }

      }
   }
}

CreateFileForm 

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;
using System.IO;
using System.Configuration;
using System.Windows;

namespace files
{
   public partial class CreateFileForm : Indicies
   {

      private StreamWriter fileWriter;

      public CreateFileForm()
      {
         InitializeComponent();
      }

      private void btnNew_Click(object sender, EventArgs e)
      {
         DialogResult result;
         string fileName;

         using (SaveFileDialog fileChooser = new SaveFileDialog())
         {
            fileChooser.CheckFileExists = false;
            result = fileChooser.ShowDialog();
            fileName = fileChooser.FileName;
         }

         if (result == DialogResult.OK)
         {
            if (fileName == string.Empty)
               MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            else
            {
               try
               {
                  FileStream output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);

                  fileWriter = new StreamWriter(output);

                  btnNew.Enabled = false;
                  btnAdd.Enabled = true;

               }
               catch (IOException)
               {
                  MessageBox.Show("Error Opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

               }
            }
         }
      }

      public void btnAdd_Click(object sender, EventArgs e)
      {
         string[] values = GetTextBoxValues();

         Record record = new Record();

         if (values[(int)TextBoxIndicies.NAME] != string.Empty)
         {
            try
            {
               int Name = Int32.Parse(values[(int)TextBoxIndicies.NAME]);

               if (Name > 0)
               {
                  record.Name = Convert.ToString(Name);
                  record.Score = Convert.ToInt32(values[(int)TextBoxIndicies.SCORE]);

                  fileWriter.WriteLine(record.Name + "," + record.Score);
               }

               else
               {
                  MessageBox.Show("Invalid Account Number");
               }

            }
            catch (IOException)
            {
               MessageBox.Show("Error Writing to File");
            }

            catch ( FormatException)
            {
               MessageBox.Show("Invalid Format");
            }
         }

         ClearTextBoxes();
      }
   }
}

Form1.Designer

namespace files
{
    partial class frmExamScoreFile
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        public System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #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>
        public void InitializeComponent()
        {
         this.lblName = new System.Windows.Forms.Label();
         this.txtName = new System.Windows.Forms.TextBox();
         this.lblScore = new System.Windows.Forms.Label();
         this.txtScore = new System.Windows.Forms.TextBox();
         this.btnNew = new System.Windows.Forms.Button();
         this.btnOpen = new System.Windows.Forms.Button();
         this.btnClose = new System.Windows.Forms.Button();
         this.btnAdd = new System.Windows.Forms.Button();
         this.btnNext = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // lblName
         // 
         this.lblName.AutoSize = true;
         this.lblName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.lblName.Location = new System.Drawing.Point(77, 41);
         this.lblName.Name = "lblName";
         this.lblName.Size = new System.Drawing.Size(51, 20);
         this.lblName.TabIndex = 0;
         this.lblName.Text = "Name";
         // 
         // txtName
         // 
         this.txtName.Location = new System.Drawing.Point(134, 41);
         this.txtName.Name = "txtName";
         this.txtName.Size = new System.Drawing.Size(179, 20);
         this.txtName.TabIndex = 1;
         // 
         // lblScore
         // 
         this.lblScore.AutoSize = true;
         this.lblScore.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
         this.lblScore.Location = new System.Drawing.Point(77, 73);
         this.lblScore.Name = "lblScore";
         this.lblScore.Size = new System.Drawing.Size(51, 20);
         this.lblScore.TabIndex = 2;
         this.lblScore.Text = "Score";
         // 
         // txtScore
         // 
         this.txtScore.Location = new System.Drawing.Point(134, 72);
         this.txtScore.Name = "txtScore";
         this.txtScore.Size = new System.Drawing.Size(100, 20);
         this.txtScore.TabIndex = 3;
         // 
         // btnNew
         // 
         this.btnNew.Location = new System.Drawing.Point(39, 126);
         this.btnNew.Name = "btnNew";
         this.btnNew.Size = new System.Drawing.Size(75, 51);
         this.btnNew.TabIndex = 4;
         this.btnNew.Text = "New File";
         this.btnNew.UseVisualStyleBackColor = true;
         // 
         // btnOpen
         // 
         this.btnOpen.Location = new System.Drawing.Point(176, 126);
         this.btnOpen.Name = "btnOpen";
         this.btnOpen.Size = new System.Drawing.Size(75, 51);
         this.btnOpen.TabIndex = 5;
         this.btnOpen.Text = "Open File";
         this.btnOpen.UseVisualStyleBackColor = true;
         // 
         // btnClose
         // 
         this.btnClose.Location = new System.Drawing.Point(313, 126);
         this.btnClose.Name = "btnClose";
         this.btnClose.Size = new System.Drawing.Size(75, 51);
         this.btnClose.TabIndex = 6;
         this.btnClose.Text = "Close";
         this.btnClose.UseVisualStyleBackColor = true;
         // 
         // btnAdd
         // 
         this.btnAdd.Location = new System.Drawing.Point(101, 207);
         this.btnAdd.Name = "btnAdd";
         this.btnAdd.Size = new System.Drawing.Size(75, 51);
         this.btnAdd.TabIndex = 7;
         this.btnAdd.Text = "Add Record";
         this.btnAdd.UseVisualStyleBackColor = true;
         // 
         // btnNext
         // 
         this.btnNext.Location = new System.Drawing.Point(250, 207);
         this.btnNext.Name = "btnNext";
         this.btnNext.Size = new System.Drawing.Size(75, 51);
         this.btnNext.TabIndex = 8;
         this.btnNext.Text = "Next Record";
         this.btnNext.UseVisualStyleBackColor = true;
         this.btnNext.Click += new System.EventHandler(this.btnNext_Click);*/


         /*ReadFile readFile = new ReadFile();
         this.btnNext.Click += new System.EventHandler(readFile.btnNext_Click);*/

         // 
         // frmExamScoreFile
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(427, 285);
         this.Controls.Add(this.btnNext);
         this.Controls.Add(this.btnAdd);
         this.Controls.Add(this.btnClose);
         this.Controls.Add(this.btnOpen);
         this.Controls.Add(this.btnNew);
         this.Controls.Add(this.txtScore);
         this.Controls.Add(this.lblScore);
         this.Controls.Add(this.txtName);
         this.Controls.Add(this.lblName);
         this.Name = "frmExamScoreFile";
         this.Text = "Exam Score File";
         this.ResumeLayout(false);
         this.PerformLayout();

        }

        #endregion

        public System.Windows.Forms.Label lblName;
        public System.Windows.Forms.TextBox txtName;
        public System.Windows.Forms.Label lblScore;
        public System.Windows.Forms.TextBox txtScore;
        public System.Windows.Forms.Button btnNew;
        public System.Windows.Forms.Button btnOpen;
        public System.Windows.Forms.Button btnClose;
        public System.Windows.Forms.Button btnAdd;
        public System.Windows.Forms.Button btnNext;
    }
}

Viewing all articles
Browse latest Browse all 12583

Trending Articles