none
Class file witth bool in windowsform. RRS feed

  • Question

  • I have a textbox in my windowsform. Im a beginner so please have some understanding for that.
    If this textbox is empty i want to return a messagebox what tells teh user to fill it out.

    Im in to classes with consoleapplication bu now i think i got lost with windowsapplication. If someone could give me a hand here with the code and a explanation too i would be more than happy.

    My error at the moment are:

    Error CS0103 The name 'cl' does not exist in the current context

    My Form1.cs looks like this

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public void Form1_Load()
            {
                Class1 cl = new Class1();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (!cl.empty())
                {
                    MessageBox.Show("Empty");
                }
            }
        }
    }

    My Class1.cs look like this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Test
    {
        class Class1 : Form1
        {
            public bool empty()
            {
                if (String.IsNullOrWhiteSpace(textBox1.Text))
                {
                    return false;
                }
                return true;
            }
        }
    }

    Form1.Designer.cs look like this

    namespace Test
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private 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>
            private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(77, 52);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(133, 20);
                this.textBox1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(77, 93);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(133, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 261);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            public System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Button button1;
        }
    }



    • Edited by anhesweden Sunday, January 21, 2018 1:02 PM
    Sunday, January 21, 2018 12:59 PM

Answers

  • The below code with Class1 cl makes the object global in scope so that any method in the class can use cl.

    namespace Test
    {
        public partial class Form1 : Form
        {
            private Class1 cl
        
            public Form1()
            {
                InitializeComponent();
            }
    
            public void Form1_Load()
            {
                cl = new Class1();
            }
    
    

    Secondly

    class Class1 : Form1
        {
            public bool empty()
            {
                if (String.IsNullOrWhiteSpace(textBox1.Text))
                {
                    return false;
                }
                return true;
            }
        }
    
    
    It should just be....
    
    public class Class1 
    {
    
        // with code.
    
    }
    
    You should not be inheriting anything from Form1. 
    http://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/

    You need to understand the basics before you can program effectively using Object Oriented Programming.

    • Marked as answer by anhesweden Sunday, January 21, 2018 3:24 PM
    Sunday, January 21, 2018 1:58 PM
  • As shown the class instance of Class1 is not in scope. You should either instantiate the class when needed or at form level as DA924x showed you.

    So here is how it would be otherwise.

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var c1 = new Class1();
                if (c1.Empty(textBox1.Text))
                {
                    MessageBox.Show("No value");
                }
                else
                {
                    MessageBox.Show($"'{textBox1.Text}'");
                }
            }
        }
    }
    

    Class

    namespace WindowsFormsApp1
    {
        class Class1
        {
            public bool Empty(string pValue) => 
                string.IsNullOrWhiteSpace(pValue);       
        }
    }
    
    There is zero reasons for passing a form control to a class, only work with a control in the context of the form.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by anhesweden Sunday, January 21, 2018 3:23 PM
    Sunday, January 21, 2018 2:41 PM
    Moderator

All replies

  • The below code with Class1 cl makes the object global in scope so that any method in the class can use cl.

    namespace Test
    {
        public partial class Form1 : Form
        {
            private Class1 cl
        
            public Form1()
            {
                InitializeComponent();
            }
    
            public void Form1_Load()
            {
                cl = new Class1();
            }
    
    

    Secondly

    class Class1 : Form1
        {
            public bool empty()
            {
                if (String.IsNullOrWhiteSpace(textBox1.Text))
                {
                    return false;
                }
                return true;
            }
        }
    
    
    It should just be....
    
    public class Class1 
    {
    
        // with code.
    
    }
    
    You should not be inheriting anything from Form1. 
    http://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/

    You need to understand the basics before you can program effectively using Object Oriented Programming.

    • Marked as answer by anhesweden Sunday, January 21, 2018 3:24 PM
    Sunday, January 21, 2018 1:58 PM
  • As shown the class instance of Class1 is not in scope. You should either instantiate the class when needed or at form level as DA924x showed you.

    So here is how it would be otherwise.

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var c1 = new Class1();
                if (c1.Empty(textBox1.Text))
                {
                    MessageBox.Show("No value");
                }
                else
                {
                    MessageBox.Show($"'{textBox1.Text}'");
                }
            }
        }
    }
    

    Class

    namespace WindowsFormsApp1
    {
        class Class1
        {
            public bool Empty(string pValue) => 
                string.IsNullOrWhiteSpace(pValue);       
        }
    }
    
    There is zero reasons for passing a form control to a class, only work with a control in the context of the form.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by anhesweden Sunday, January 21, 2018 3:23 PM
    Sunday, January 21, 2018 2:41 PM
    Moderator
  • Thank you! Just the guidance i needed.
    Sunday, January 21, 2018 3:24 PM
  • Thank you for the good answer, and the link. I will read this one.
    Sunday, January 21, 2018 3:25 PM
  • Thank you for the good answer, and the link. I will read this one.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    You need to read this one too, because OO is OO,  and the principles are the same for Java and .NET with them both being OO platforms.

    Sunday, January 21, 2018 3:57 PM
  • Thank you!
    I just wonder one thing.

    why write  "var"
    var c1 = new Class1();
    • Edited by anhesweden Sunday, January 21, 2018 4:24 PM
    Sunday, January 21, 2018 4:22 PM
  • Thank you!
    I just wonder one thing.

    why write  "var"
    var c1 = new Class1();

    var only works in the local scope of a method.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

    Outside of the method at a global scope where all methods in the class can see and access the object.

    private Class1 c1 // global scope

    private void method()

    {

          var cl = new Class1(); // local scope only with only code in the method can see and access cl

    if (help == 'yes')

    {

        var cl = new Class1();  // ci can only be deen in the 'if' scope and no code outside the 'if' can see cl in the method code.

    var does a strong typing of an variable or object without having to type the object, like so.

    private Class1 c1

    or

    Class1 c1 at the local scope/method level

    Sunday, January 21, 2018 7:30 PM