Stijn van Woerkom
Stijn van Woerkom

Reputation: 35

Windows forms button parameters

Background Info:

For school, I need to make a crossword puzzle project in Windows Forms using OOP and the project needs to have a list build in it.

Problem:

For the functions in Form1.cs, I need to pass parameters (Object sender, Eventargs e) coming from the classes for the Form. The Project is OOP based.

Question:

error: There is no argument given that corresponds to the required formal parameter 'sender'

The errors are coming from:

 menuOptions.aboutToolStripMenuItem_Click();
 menuOptions.openPuzzleToolStripMenuItem_Click();
 menuOptions.exitToolStripMenuItem_Click();
 boardCells.Board_CellPainting();
 boardCells.Board_CellValueChanged();
 boardCells.formatCell();

My question is what do I need to do to solve the error?

Form1.cs

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

namespace CrossWordPuzzle
{
    public partial class Form1 : Form
    {
        //Object instances from classes
        Clues clue_window = new Clues();
        Cell boardCells = new Cell();
        Menu menuOptions = new Menu();
        Board board = new Board();

        public Form1()
        {
            InitializeComponent();
            //Board functions
            board.buildWordList();
            board.InitializeBoard();
            //Menu functions
            menuOptions.aboutToolStripMenuItem_Click();
            menuOptions.openPuzzleToolStripMenuItem_Click();
            menuOptions.exitToolStripMenuItem_Click();
            menuOptions.InitializeComponent();
            //Cell functions
            boardCells.Board_CellPainting();
            boardCells.Board_CellValueChanged();
            boardCells.formatCell();
            boardCells.InitializeComponent();
        }

        //Function loads form with window properties
        public void Form1_Load(object sender, EventArgs e)
        {
            board.InitializeBoard();
            clue_window.SetDesktopLocation(this.Location.X + this.Width + 1, this.Location.Y);
            clue_window.StartPosition = FormStartPosition.Manual;

            clue_window.Show();
            clue_window.clueTable.AutoResizeColumns();
        }

        //Function location form window
        public void Form1_LocationChanged(object sender, EventArgs e)
        {
            clue_window.SetDesktopLocation(this.Location.X + this.Width + 1, this.Location.Y);
        }
    }
}

(menuOptions) Menu Class

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

namespace CrossWordPuzzle
{
    class Menu : Form
    {
        //Object instances from classes
        public DataGridView datagridview = new DataGridView();
        public Form1 form = new Form1();
        public string puzzle_file = Application.StartupPath + "\\Puzzles\\Puzzle1.pzl";
        Clues clue_window = new Clues();
        public List<id_cells> idc = new List<id_cells>();
        Board board = new Board();

        //Opens puzzle selector
        public void openPuzzleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Puzzle Files|*.pzl";
            if (ofd.ShowDialog().Equals(DialogResult.OK))
            {
                puzzle_file = ofd.FileName;
                datagridview.Rows.Clear();
                clue_window.clueTable.Rows.Clear();
                idc.Clear();

                board.buildWordList();
                board.InitializeBoard();

            }
        }
        //Exit application function
        public void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //About menu function
        public void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("CrossWordPuzzle made in C# Windows Forms", "CrossWordPuzzle");
            return;
        }
        //Initialize Menu component
        public void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Menu
            // 
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "Menu";
            this.Load += new System.EventHandler(this.Menu_Load);
            this.ResumeLayout(false);

        }
        //Load Menu
        private void Menu_Load(object sender, EventArgs e)
        {

        }
    }
}

(boardCells) Cell class

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

namespace CrossWordPuzzle
{
    class Cell : Form
    {
        //Object instances from classes
        public DataGridView datagridview = new DataGridView();
        public List<id_cells> idc = new List<id_cells>();
        public Form1 form = new Form1();

        //Cell painting
        public void Board_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //Every time a cell gets drawn on the board, it comes with a rectangle that has the number of the word.
            String number = "";
            if (idc.Any(c => (number = c.number) != "" && c.X == e.ColumnIndex && c.Y == e.RowIndex))
            {
                Rectangle r = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.FillRectangle(Brushes.White, r);
                Font f = new Font(e.CellStyle.Font.FontFamily, 7);
                e.Graphics.DrawString(number, f, Brushes.Black, r);
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
            }
        }
        //Function for BoardCell design
        public void Board_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //make letter uppercase
            try
            {
                form.Board[e.ColumnIndex, e.RowIndex].Value = form.Board[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper();
            }
            catch (Exception)
            {

            }
            //Only 1 letter per cell
            try
            {
                if (form.Board[e.ColumnIndex, e.RowIndex].Value.ToString().Length > 1)
                {
                    form.Board[e.ColumnIndex, e.RowIndex].Value = form.Board[e.ColumnIndex, e.RowIndex].Value.ToString().Substring(0, 1);
                }
            }
            catch (Exception)
            {

            }
            //Change color if correct
            try
            {
                if (form.Board[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper().Equals(form.Board[e.ColumnIndex, e.RowIndex].Tag.ToString().ToUpper()))
                {
                    form.Board[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.DarkGreen;
                }
                else
                {
                    form.Board[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {

            }
        }
        //Function for formatting Cell design on Board
        public void formatCell(int row, int col, String letter)
        {
            DataGridViewCell c = form.Board[col, row];
            c.Style.BackColor = Color.White;
            c.ReadOnly = false;
            c.Style.SelectionBackColor = Color.Cyan;
            c.Tag = letter;
        }
        //Initialize Cell component
        public void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Cell
            // 
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "Cell";
            this.Load += new System.EventHandler(this.Cell_Load);
            this.ResumeLayout(false);

        }
        //Load Cell
        private void Cell_Load(object sender, EventArgs e)
        {

        }
    }
    //Class for Cell id's
    public class id_cells
    {
        public int X;
        public int Y;
        public String direction;
        public String number;
        public String word;
        public String clue;

        public id_cells(int x, int y, String d, String n, String w, String c)
        {
            this.X = x;
            this.Y = y;
            this.direction = d;
            this.number = n;
            this.word = w;
            this.clue = c;
        }
    } //end of class
}

Upvotes: 0

Views: 331

Answers (1)

Neil
Neil

Reputation: 11889

You are trying to call menuOptions.aboutToolStripMenuItem_Click(); but the function requires 2 parameters (object sender, EventArgs e);

You need to supply those parameters.

This is done automatically by the framework when you wire up events:

this.Load += new System.EventHandler(this.Cell_Load);

private void Cell_Load(object sender, EventArgs e)
{
}

When the Load event is triggered by the framework when a form is loaded, it will automatically pass in the sender and event args.

If you are trying to call those functions manually (i.e. in YOUR code), then you must supply those parameters.

You can fake it by using:

CellLoad(this, EventArgs.Empty);

What is confusing is why you have decided to call these functions during initialisation.

        menuOptions.aboutToolStripMenuItem_Click();
        menuOptions.openPuzzleToolStripMenuItem_Click();
        menuOptions.exitToolStripMenuItem_Click();

Why do you want to press the exit button during startup?

Upvotes: 3

Related Questions