instantiate from several files, Error CS0103 The name 'richTextBox1' does not exist in the current context [closed]

1

I am not a computer professional, but with many hours of flight. I come from the good builder borland c ++, but it becomes obsolete and I heard better to go to c #. I do not give him the point, it seems like a roll. The fact is that I want to manage MySQL from c #. I take an example. After changing several things for the suggestions, it seems to start. It occurs to me to add a RichText to see a trace, but there is no way.

The example has several files:

1st is the Program.cs that I understand that you do not have to touch anything, there is an Application.Run (new Form1 ()); that leads to the creation and gets into Form1.

2nd there is Form1.Design.cs

    namespace ConnectCsharpToMysql
{
//    partial class Form1//<< VERSION ORIGINAL
      public partial class Form1
    {
,,,,,,,,,,,,,,,,,,,,, mas codigo,,,,,,,,
        private System.Windows.Forms.Button buttonDesco;
//        private System.Windows.Forms.RichTextBox richTextBox1;//<< VERSION ORIGINAL
        public  System.Windows.Forms.RichTextBox richTextBox1;
    }
}

3rd the Form1.cs, at the moment, the Richtext works, that is, after starting the components, of course.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ConnectCsharpToMysql
{
    public partial class Form1 : Form
    {
        private DBConnect dbConnect;

        public Form1()
        {
//            richTextBox1.AppendText("entra InitializeComponent\r\n");//System.NullReferenceException: 'Referencia a objeto no establecida como instancia de un objeto.'

            InitializeComponent();
            richTextBox1.AppendText("sale InitializeComponent\r\n");

            dbConnect = new DBConnect();
            richTextBox1.AppendText("sale new DBConnect()\r\n");
        }

        //Insert button clicked 
,,,,,,,,,,,,,,,,,,

4th DBConectc.cs and now the mess. I want to use rich to draw, but there is no way.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.IO;
    //Add MySql Library
    //using MySql.Data.MySqlClient;//<<<<
    using System.Data.SqlClient;

    namespace ConnectCsharpToMysql
    {
        class DBConnect
        {
            private SqlConnection connection;
            private string server;
            private string database;
            private string uid;
            private string password;

    //        private Form Form1 = new Form();

            //Constructor
            public DBConnect()
            {
                Initialize();
// ERRORES DEL RICH >>>>>>>>>>>>>>>>
//            richTextBox1.AppendText("sale Initialize\r\n");//Error CS0103  El nombre 'richTextBox1' no existe en el contexto actual
//            Form1.richTextBox1.AppendText("sale Initialize\r\n");//Error CS0122  'Form1.richTextBox1' no es accesible debido a su nivel de protección
//            Form1.richTextBox1.AppendText("sale Initialize\r\n");//despues de cambiar a public el iniciatecomponet de form1//Error CS0120  Se requiere una referencia de objeto para el campo, método o propiedad 'Form1.richTextBox1' no estáticos
            }

            //Initialize values
            private void Initialize()
            {
                server = "localhost";
                database = "test";
    ,,,,,,,,,,,

I gave it a thousand laps, I found no example of intanciar from several files, already probe with doing it all public, and with prefixing the object,

SOMEONE KNOWS THE MAGIC WORDS ????

ADDED NOTHING , THERE'S NO WAY I have stripped the problem, in new project, form with two buttons and RichText. no problem in using the Rich.

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 test3{

    public partial class Form1 : Form    {

        public class_Test3A Test3A = new class_Test3A();

        public Form1()        {
            InitializeComponent();
            richTextBox1.AppendText("sale InitializeComponent\r\n");
        }

        private void button1_Click(object sender, EventArgs e)        {
            richTextBox1.AppendText("pasa boton 1\r\n");
            RichOut("test RichOut(), desde boton 1");
        }

        private void button2_Click(object sender, EventArgs e)        {
            richTextBox1.AppendText("pasa boton 2\r\n");
            Test3A.Molino();
        }

        public void RichOut(string text)        {//puente, parece que no estatico
            richTextBox1.AppendText(text + "\r\n");
        }
    }
}

The problem now comes, in new file, with a class that tries to write in the rich, direct or through the bridge.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test3
{
    public class class_Test3A    {

        public class_Test3A()   {  }//constructor

        public void Molino()    {
            Form1.richTextBox1.AppendText("test richTextBox1, desde Molino");//Error CS0120  Se requiere una referencia de objeto para el campo, método o propiedad 'Form1.richTextBox1' no estáticos
            Form1.RichOut("test RichOut(), desde Molino");//Error CS0120  Se requiere una referencia de objeto para el campo, método o propiedad 'Form1.richTextBox1' no estáticos
        }
    }
}

The problem does not come from being private, I put everything in public, up to a line in Form1.Desing.cs I do not think it's because I do not prefix the methods with their object. Layered programming is fine, I hope some day , but even so, it will need a service system, to "see" the data, for maintenance and debug. I study, I downloaded several PDFs, and I found out about something. The problem is that they all explain c #, in console mode, but not the peculiarities of windows and visual setudio. And besides, I am a self-taught, I have mastered the c ++ without studying it in a methodical and rigorous way, I have looked for examples, and I made a copy and paste of what interested me. But I did not find anyone with the need to act with forms from other objects.

    
asked by Jacinto Pereira 24.08.2018 в 06:11
source

1 answer

1

Your main problem is to think that the form is something else, and not a class (an object) .. and it really is the same thing ... what happens is that it has a visual component, but it is not something that really impacts on what you want to do

The examples are usually console because they are to explain the language. The visual part, is studied in other places maybe, but it does not stop being something similar its handling.

What's more, all the visual components are classes that can be instantiated and created in your form (and practically, that's what the constructor does in windows forms).

Now, as a mini example of what you want to do, you do not need anything more than what you have, let's see an example of how to fill a textbox from a class, in two ways:

Let's create a form, and put a textbox and two buttons.

Also, let's create the following class:

class TestClass
{

    public string DevolverTexto()
    {
        return "esto solo devuelve un texto";
    }

    public void EscribirTexto(TextBox t)
    {
        t.Text = "aca la clase escribio directamente!!!";
    }
}

When you generate that class, it will show an error about TextBox , because you need to tell it that it is .. for that, you have to add the following line above:

using System.Windows.Forms;

Ready the class, no longer has errors.

We double click on the button1 that we created before, (that will automatically generate the click event) and add this code:

TestClass t = new TestClass();
textBox1.Text = t.DevolverTexto();

We do the same as before on the button2, and add this code:

TestClass t = new TestClass();
t.EscribirTexto(textBox1);

There will be two different ways to pass a text to a textbox from a class. In general, the first version is always used, but I wanted to show you that there are other ways. You could also send the form, search the textbox and write there. But it is only complicate much more, something that becomes much simpler.

    
answered by 26.08.2018 / 20:06
source