0

This is called file.cs in Visual Studio web application with C#. I want to call the function ReadOnlySetting() in file.designer.cs. I tried calling this after the "initialize component" like this ReadOnlySetting(); I am getting the error "must return type"

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=LENOVO-        PC;Integrated Security=True");
            con.Open();
            SqlCommand sc = new SqlCommand("insert intp Emp values('" +     textBox2.Text + "'," + textBox1.Text + ",'" + textBox3.Text + "','" + textBox4.Text + "'," + textBox5.Text + ");", con);
            object o = sc.ExecuteNonQuery();
            MessageBox.Show(o+ " :Record has been inserted");
            con.Close();
        }

        //sender controls what the action is for
        //EventArgs is the argument 
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit(); 
        }

        **private void ReadOnlySetting()**
        {
            this.textBox2.ReadOnly = true;
        }

        public static void main(string[] args)
        {
            Application.Run(new Form1());
        }

    }
}

This is my file.designer.cs

namespace WindowsFormsApplication7
{
    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.components = new System.ComponentModel.Container();
            this.dataSet1 = new WindowsFormsApplication7.DataSet1();
            this.dataSet1BindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet1BindingSource)).BeginInit();
            this.SuspendLayout();
            this.textBox2.ReadOnly = true;
            // 
            // dataSet1
            // 
            this.dataSet1.DataSetName = "DataSet1";
            this.dataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // 
            // dataSet1BindingSource
            // 
            this.dataSet1BindingSource.DataSource = this.dataSet1;
            this.dataSet1BindingSource.Position = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(45, 75);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 3;
            this.label1.Text = "Name";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(86, 72);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 4;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(45, 156);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(25, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Sex";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(86, 153);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 20);
            this.textBox3.TabIndex = 4;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(45, 112);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(26, 13);
            this.label3.TabIndex = 3;
            this.label3.Text = "Age";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(86, 109);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 4;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(45, 195);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(45, 13);
            this.label4.TabIndex = 3;
            this.label4.Text = "Address";
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(86, 192);
            this.textBox4.Name = "textBox4";
            this.textBox4.ReadOnly = true;
            this.textBox4.Size = new System.Drawing.Size(100, 20);
            this.textBox4.TabIndex = 4;
            // 
            // textBox5
            // 
            this.textBox5.Location = new System.Drawing.Point(86, 228);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(100, 20);
            this.textBox5.TabIndex = 6;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(45, 231);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(58, 13);
            this.label5.TabIndex = 5;
            this.label5.Text = "Phone No.";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(163, 278);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 7;
            this.button1.Text = "Close";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(35, 278);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 8;
            this.button2.Text = "Submit";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(420, 380);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox5);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet1BindingSource)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }



        #endregion

        private System.Windows.Forms.BindingSource dataSet1BindingSource;
        private DataSet1 dataSet1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.TextBox textBox5;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}

I don't where I can include my method or how to call it.

maddddie123
  • 185
  • 1
  • 1
  • 10
  • I get this error (Method 'System.Windows.Forms.Form.ReadOnlySetting' not found.) when I just write ReadOnlySetting(); – maddddie123 Jan 21 '16 at 05:52
  • Why are manually editing the designer files? Those files are not intended to be edited by hand. – Joshua Shearer Jan 21 '16 at 05:55
  • I want to call it in the designer files. Where else can I call my function then? I don' t want to call it in the main function – maddddie123 Jan 21 '16 at 05:57
  • I answered this already today on [34912168](http://stackoverflow.com/questions/34912168) – Jens Meinecke Jan 21 '16 at 05:58
  • I realize that's what you *want* to do, but it's not recommended. ([See this question for more information](https://stackoverflow.com/questions/16161994/manual-editing-of-designer-cs-file)). That said, what are you trying to accomplish? – Joshua Shearer Jan 21 '16 at 05:59
  • Do you mean I can write this line after the initialize component? ReadOnlySetting(); I am getting the error "must return type" – maddddie123 Jan 21 '16 at 06:01
  • If you just want to prevent users from editing the contents of the control, set the value of the `ReadOnly` property from the designer itself. Click the control in the design-mode - I believe the shortcut is `F4` to open the properties window. – Joshua Shearer Jan 21 '16 at 06:02
  • I don't want to do that because I have some other methods to call later too – maddddie123 Jan 21 '16 at 06:03
  • What are you trying to accomplish? As stated previously, there are significant implications with editing the `.designer` files, so any methods of this nature that you wish to call will probably need be in event handlers. Knowing what you're trying to do helps us determine how you might be able to do it. Without knowing more information, it's hard to say much aside from "you can't". – Joshua Shearer Jan 21 '16 at 06:10

1 Answers1

0

Don't put it in the designer. It will just be erased whenever you make a change to your form's design. Call it straight after InitializeComponent.

    public Form1()
    {
        InitializeComponent();
        ReadOnlySetting();
    }
Steve Harris
  • 5,014
  • 1
  • 10
  • 25