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.