textbox gebruiken in program.cs

Status
Niet open voor verdere reacties.

badboyR

Gebruiker
Lid geworden
11 apr 2012
Berichten
28
Ik wil textboxen van form1 gebruiken in program.cs, hoe moet dit? Ik heb geprobeerd private te veranderen in public static maar dan krijg ik deze error (25 keer):
"Error 1 Member 'Car_Adder.Form1.textBox4' cannot be accessed with an instance reference; qualify it with a type name instead"

Weet iemand hoe ik het anders moet doen?

Mijn Form1.Designer.cs:
Code:
namespace Car_Adder
{
    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.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button5 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.AccessibleName = "MainBox";
            this.groupBox1.Controls.Add(this.button5);
            this.groupBox1.Controls.Add(this.button4);
            this.groupBox1.Controls.Add(this.textBox4);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.button3);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Controls.Add(this.textBox3);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.textBox2);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.textBox1);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(564, 319);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Add Cars";
            // 
            // button5
            // 
            this.button5.AccessibleName = "Klaar";
            this.button5.Location = new System.Drawing.Point(237, 284);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 13;
            this.button5.Text = "Klaar";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // button4
            // 
            this.button4.AccessibleName = "DefVehicles";
            this.button4.Location = new System.Drawing.Point(453, 242);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 12;
            this.button4.Text = "Standaard";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(29, 242);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(396, 20);
            this.textBox4.TabIndex = 11;
            // 
            // label5
            // 
            this.label5.AccessibleName = "Vraag4";
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(26, 216);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(133, 13);
            this.label5.TabIndex = 10;
            this.label5.Text = "Voer de Vehicles.ide lijn in:";
            // 
            // button3
            // 
            this.button3.AccessibleName = "DefHandling";
            this.button3.Location = new System.Drawing.Point(453, 118);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 9;
            this.button3.Text = "Standaard";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button2
            // 
            this.button2.AccessibleName = "DefCarcols";
            this.button2.Location = new System.Drawing.Point(453, 177);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 8;
            this.button2.Text = "Standaard";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(29, 180);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(396, 20);
            this.textBox3.TabIndex = 7;
            // 
            // label4
            // 
            this.label4.AccessibleName = "Vraag3";
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(26, 155);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(129, 13);
            this.label4.TabIndex = 6;
            this.label4.Text = "Voer de Carcols.dat lijn in:";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(29, 118);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(396, 20);
            this.textBox2.TabIndex = 5;
            // 
            // label3
            // 
            this.label3.AccessibleName = "Vraag2";
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(26, 93);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(136, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Voer de Handling.dat lijn in:";
            // 
            // button1
            // 
            this.button1.AccessibleName = "ThisPath";
            this.button1.Location = new System.Drawing.Point(453, 52);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "Deze locatie";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.AccessibleName = "Path";
            this.textBox1.Location = new System.Drawing.Point(29, 55);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(396, 20);
            this.textBox1.TabIndex = 2;
            // 
            // label2
            // 
            this.label2.AccessibleName = "Vraag1";
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(26, 30);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(135, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Voer de bestandslocatie in:";
            // 
            // label1
            // 
            this.label1.AccessibleName = "Badboy";
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(463, 294);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(93, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Made by Bad.boy!";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(599, 343);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "IV Car Adder";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion
        
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        public static System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.Label label4;
        public static System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button1;
        public static System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button4;
        public static System.Windows.Forms.TextBox textBox4;
    }
}
 
Laatst bewerkt:
Je zou een variable in Program kunnen aanmaken:
Code:
public static string GlobalInfo;

en dan in form1
Code:
Program.GlobalInfo = "my string value";
 
Dit werkt alleen als je een vaste waarde in textbox1 opvoorhand ingegeven hebt ;).
En wanneer je de Form sluit dat je dan een messagebox krijgt te zien, met uw waarde erin van textbox.

in uw program.cs, moet je overerven van Form1

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

namespace LoginGameAndServer
{
    class Program : Form1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            MessageBox.Show(textboxje);
        }
    }
}
Het is noraal dat uw program.cs precies naar een form verandert, dat komt omdat je van Form1 overerft.

en in uw Form1.cs
Code:
        protected static string textboxje = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            textboxje = userTextbox.Text;
        }

Foto:



Uploaded with ImageShack.us



Uploaded with ImageShack.us



Waarom zou je de waarde van de textbox willen gebruiken in program.cs ? ;)
Het is toch handiger dat je dat gewoon in Form doet ;)



Met vriendelijke groetjes, Jim
 
Laatst bewerkt:
Dit werkt alleen als je een vaste waarde in textbox1 opvoorhand ingegeven hebt ;).
Niet als je de variabele aanpast, wanneer de tekst van de TextBox wordt aangepast.

@TS: Kun je de vraag even op opgelost zetten? :)
 
Sorry, maar nu ik hem probeer te builden krijg ik toch nog een error: "Static class 'Car_Adder.Program' cannot derive from type 'Car_Adder.Form1'. Static classes must derive from object.\"

Weet iemand waar dit aan ligt (code weg gelaten, is niet het probleem):
Program.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Car_Adder
{
    static class Program : Form1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            Globals.Uitvoeren = 1;

            while (Globals.Uitvoeren != 10)
            {
                Check();
            }
        }

        private static void Check()

        private static void GetLines() // Hier moet nog wat toegevoegd worden!

        private static void SetLines()
    }

    public class Globals
}

Form1:
Code:
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;

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

        private void button1_Click(object sender, EventArgs e) // Locatie
        {
            string Locatie = Directory.GetCurrentDirectory();
            textBox1.Clear();
            textBox1.Paste(Locatie);
        }

        private void button3_Click(object sender, EventArgs e) // Handling
        {
            textBox2.Paste("Standaard");
            Globals.Uitvoeren = 2;
        }

        private void button2_Click(object sender, EventArgs e) // Carcols
        {
            textBox3.Paste("Standaard");
            Globals.Uitvoeren = 3;
        }

        private void button4_Click(object sender, EventArgs e) // Vehicles
        {
            textBox4.Paste("Standaard");
            Globals.Uitvoeren = 4;
        }

        private void button5_Click(object sender, EventArgs e) // Klaar
        {
            Globals.Uitvoeren = 5;
        }

        protected static string LocatieUser = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            LocatieUser = textBox2.Text;
        }

        protected static string HandlingUser = null;
        private void Form1_Load1(object sender, EventArgs e)
        {
            HandlingUser = textBox2.Text;
        }

        protected static string CarcolsUser = null;
        private void Form1_Load2(object sender, EventArgs e)
        {
            CarcolsUser = textBox3.Text;
        }

        protected static string VehiclesUser = null;
        private void Form1_Load3(object sender, EventArgs e)
        {
            VehiclesUser = textBox4.Text;
        }

        protected static string AutoUser = null;
        private void Form1_Load4(object sender, EventArgs e)
        {
            AutoUser = textBox5.Text;
        }
    }
}
 
de code:
Code:
    static class Program : Form1

word
Code:
    class Program : Form1

In mijn voorbeeld die ik je gegeven hebt staat het er ookzo op ;)
 
Er zijn natuurlijk genoeg manieren om dit voor elkaar te krijgen, alleen zie ik niet waarom je program 'inherit' van het form.
Maar voornamelijk, waarom wil je deze code en methods allemaal in de Program.cs hebben als ik vragen mag? Is het niet fijner om dit in een eigen class te zetten en alleen een referentie naar die class ?

Zo zou je het kunnen doen met een simpel event.

Form:
Code:
using System;
using System.Windows.Forms;

namespace Example1
{
    public partial class MainView : Form
    {
        public event EventHandler<InputEventArgs> InputChanged;

        public MainView()
        {
            InitializeComponent();
        }

        private void txtInput_TextChanged(object sender, EventArgs e)
        {
            OnInputChanged(txtInput.Text);
        }

        private void OnInputChanged(string input)
        {
            var handler = InputChanged;
            if (handler != null)
                handler(this, new InputEventArgs(input));
        }
    }
}

Program:
Code:
using System;
using System.Windows.Forms;

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

            var mainView = new MainView();
            mainView.InputChanged += new EventHandler<InputEventArgs>(mainView_InputChanged);

            Application.Run(mainView);
        }

        static void mainView_InputChanged(object sender, InputEventArgs e)
        {
            //use event args the way you want
            Console.WriteLine(e.Input);
        }
    }
}

Event Args:
Code:
using System;

namespace Example1
{
    public class InputEventArgs : EventArgs
    {
        private readonly string _input;

        public InputEventArgs(string input)
        {
            _input = input;
        }

        public string Input
        {
            get { return _input; }
        }
    }
}

In dit voorbeeld zie je dan bij elke wijziging in de textbox een reactie kan doen. Maar dit kun je ook doen wanneer iemand op een knop op je form klikt etc
rlw8pj.jpg



---


Een andere manier zou zijn om een class in de form constructor mee te geven.

Form:
Code:
using System;
using System.Windows.Forms;

namespace Example1
{
    public partial class MainView : Form
    {
        private readonly ProgramData _programData;

        public MainView(ProgramData programData)
        {
            InitializeComponent();
            _programData = programData;
        }

        private void txtInput_TextChanged(object sender, EventArgs e)
        {
            _programData.Input = txtInput.Text;
        }
    }
}

Program:
Code:
using System;
using System.Windows.Forms;

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

ProgramData:
Code:
namespace Example1
{
    public class ProgramData
    {
        public string Input { get; set; }
    }
}
 
Nu ik static heb weg gelaten werkt het. Bedankt voor alle antwoorden.

Ik ben een beginner met C# (zoals je waarschijnlijk al door had) en weet nog niet hoe alles werkt. Voor het volgende programma zal ik het anders doen. Ik heb het zo gedaan omdat mijn programma de hele tijd "loopt" en alles wordt zo'n beetje gedaan vanuit program.cs.

Weet iemand trouwens een goede tutorial over wanneer je public static, protect, readonly etc. moet gebruiken (het liefst in het Nederlands)?
 
Dat is geen enkel probleem ;), je moet van iets beginnen he ;)

private: is enkel voor binnen uw klasse bereikbaar.
public: is in elke klasse bereikbaar, wanneer je eerst een object aanmaakt.
protected: is private en public gemengt, enkel wordt protected gebruikt voor in overgeerfde klasses.
static: dan moet je geen object maken.

hier vind je meer informatie: http://msdn.microsoft.com/en-us/library/6tcf2h8w(v=vs.71).aspx
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan