[C#] Textbox aus anderer Klasse Füllen

Schnuki

Mitglied
Hallo Leute,

ich habe ein Problem:

ich möchte meine Textbox aus einer anderen klasse füllen. Da ich jedoch in meiner Windows Form mehrere Textboxen habe, ist es hier speziell die TextBox5. wenn bei der Klasse die Schleife durchgelaufen ist, soll er immer die Textbox mit einem bestimmten Satz füllen. Entweder es hat geklappt oder fehlgeschlagen

Kann mir jemand helfen?
Ich schätz mal, Code muss ich nicht posten oder?
 
Hi,

eine Möglichkeit wäre es, wenn du in deinem Form einfach eine öffentliche Methode einfügst, die den String in die Textbox schreibt. Die kannst du dann von außen aufrufen.

Grüße, D.
 
Dario schrieb doch du sollst eine öffentlich Methode schreiben. Demnach hätte ich so etwas erwartet wie
Code:
public void SetText(string newtext)
{
 this.textbox5.text = newtext;
}

und du würdest von aussen nur diese Methode aufrufen.
Vielleicht wäre es doch hilfreich wenn du mal deinen Code postest

Grüsse bb
 
genauso hab ich die Methode auch gemacht, aber dann lässt er mich von außen, von meiner klasse, nicht darauf zugreifen!

Code:
public void tBoxFilled(string name)
        {
            this.textBox5.Text = name;
        }


und der Aufruf kommt in einer anderen klasse:

Code:
ober.tBoxFilled(dllImport.logEntry);

aber ich kann die Funktion gar nicht auswählen!
 
Hi,

sind beide Klassen in einer Assembly und falls nicht: sind die Verweise auf dem neuesten Stand?

Zeig doch mal etwas mehr Code, insbesondere die Deklaration des Formularobjektes in der anderen Klasse.

Evtl. hängt auch Intellisense, aber das kann ich mir eigentlich nicht so richtig vorstellen. Zur Sicherheit aber vielleicht doch einfach mal Visual Studio neu starten?

Grüße, D.
 
also ich hab mal Visual Studio neu gestartet und es ist immer noch das gleiche Problem.

Ich poste einfach mal meine kompletten zwei dateien (klasse und oberfläche). Jedoch sind die klassen ziemlich lang und daher war ein kleines durcheinander nicht mehr zu vermeiden, aber ich hoffe ihr kennt euch aus. die using-direktiven und die deklarationen der variablen hab ich jetzt mal entfernt, damit es net ganz so lang wird:

klasse:

Code:
namespace BSW4MES_TEST
{
    class analyseFiles
    {

        [SerializableAttribute]
        [ComVisibleAttribute(true)]
        public class ArgumentOutOfRangeException : ArgumentException, ISerializable { }

        oberflaeche ober = new oberflaeche();

        public static void getFileInformations(bool isTrue)
        {
            for (int i = 0; i < 100; i++)
            {
                if (oberflaeche.txtFiles[i] != null && oberflaeche.txtFiles[i] != "")
                {
                    string[] artikelnummer = new string[Convert.ToInt16(oberflaeche.txtFilesRows[i])];
                    string[,] artikelnummer_einzeln = new string[Convert.ToInt16(oberflaeche.txtFilesRows[i]), 4];

                    count = oberflaeche.txtFilesRows[i];
                    
                    //Continue to read until you reach end of file
                    StreamReader sr = new StreamReader(oberflaeche.txtFiles[i], Encoding.Default);
                    string line;
                    int x = 0;
                    int c = 0;

                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                        artikelnummer[x] = line;
                        x++;

                        laenge = line.Length;

                        for (int j = 0; j < laenge; j++)
                        {
                            if (line[j].ToString() != ";" && line[j].ToString() != "")
                            {
                                nummer = j;
                            }

                            else if (line[j].ToString() == ";")
                            {
                                strichpunkt++;
                            }

                            if (strichpunkt == 1)
                            {
                                if (j+1 < laenge)
                                {
                                    if (line[j + 1].ToString() != "")
                                    {
                                        nummer2 = nummer + 2;
                                        endziffer[start] = nummer;
                                        startIndex[start] = nummer2;
                                        start++;
                                        strichpunkt = 0;
                                    }

                                    else
                                    {
                                        nummer2 = nummer + 3;
                                        endziffer[start] = nummer;
                                        startIndex[start] = nummer2;
                                        start++;
                                        strichpunkt = 0;
                                    }
                                }

                            }

                            if (start == 4)
                            {
                                break;
                            }

                        }

                        if (start != 4)
                        {
                            if (line[laenge-1].ToString() != ";")
                            {
                                nummer = laenge - 1;
                                nummer2 = nummer + 3;
                                endziffer[start] = nummer;
                                startIndex[start] = nummer2;
                            }
                        }

                        start = 0;

                        int g = 0;

                        for (int z = 0; z < 4; z++)
                        {
                            if (startIndex[z].ToString() == "0" || endziffer[z].ToString() == "0")
                            {
                                break;
                            }

                            else
                            {
                                if (z == 0)
                                {
                                    artikelnummer_einzeln[c, z] = line.Substring(0, endziffer[z]+1);

                                }

                                else
                                {
                                    artikelnummer_einzeln[c, z] = line.Substring(startIndex[g], (endziffer[z] - startIndex[g]) + 1);
                                    g++;
                                }
                            }
                        }

                        for (int y = 0; y < 4; y++)
                        {
                            startIndex[y] = 0;
                            endziffer[y] = 0;
                        }

                        c++;

                    }

                    dllImport.LoadAnalyseFirstEntry(oberflaeche.DLLpath, checkFiles.filePath[i], checkFiles.fileName[i],
                                                     artikelnummer_einzeln[i, 0], artikelnummer_einzeln[i, 1],
                                                     artikelnummer_einzeln[i, 2], artikelnummer_einzeln[i, 3]);

                    if (dllImport.analyse == "1")
                    {
                        string newMeldung = "The analyse of the first entry was successfully!";
                        DialogResult res1 = MessageBox.Show(newMeldung,
                                            Application.ProductName,
                                            MessageBoxButtons.OKCancel,
                                            MessageBoxIcon.Asterisk);
                        
                        dllImport.LoadStartFile(oberflaeche.DLLpath, checkFiles.filePath[i], checkFiles.fileName[i], oberflaeche.txtFilesRows[i]);


                        if (res1 == DialogResult.OK)
                        {
                            for (int q = 0; q < Convert.ToInt16(oberflaeche.txtFilesRows[i]); q++)
                            {
                                dllImport.LoadProcessEntry(oberflaeche.DLLpath, artikelnummer_einzeln[q, 0],
                                                           artikelnummer_einzeln[q, 1], artikelnummer_einzeln[q, 2],
                                                           artikelnummer_einzeln[q, 3]);

                                dllImport.LoadLogEntry(oberflaeche.DLLpath);
                                oberflaeche.tBoxFilled(dllImport.logEntry);
                                
                                
                            }

                            dllImport.LoadStopFile(oberflaeche.DLLpath, checkFiles.filePath[i], checkFiles.fileName[i]);

                            string newMeldung2 = "The " + (i + 1) + ". File is finished. Do you want to continued?";
                            DialogResult res2 = MessageBox.Show(newMeldung2,
                                                Application.ProductName,
                                                MessageBoxButtons.YesNo,
                                                MessageBoxIcon.Question);
                            if (res2 == DialogResult.No)
                            {
                                Application.Exit();
                            }
                        }

                        else if (res1 == DialogResult.Cancel)
                        {
                            Application.Exit();
                        }
                    }

                    else
                    {
                        string meldung = "The First Entry is invalid! Do you want to continue?";
                        DialogResult res = MessageBox.Show(meldung,
                                           Application.ProductName,
                                           MessageBoxButtons.YesNo,
                                           MessageBoxIcon.Asterisk);
                        if (res == DialogResult.No)
                        {
                            Application.Exit();
                        }
                    }

                    for (int r = 0; r < Convert.ToInt16(oberflaeche.txtFilesRows[i]); r++)
                    {
                        for (int o = 0; o < 4; o++)
                        {
                            artikelnummer_einzeln[r, o] = "";
                        }
                    }

                    

                }

            }

            string meldung3 = "All Files were checked!";
            DialogResult res3 = MessageBox.Show(meldung3,
                               Application.ProductName,
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Asterisk); 
        }
    }
}

oberfläche:

Code:
namespace BSW4MES_TEST
{
    public partial class oberflaeche : Form
    {

        analyseFiles anafil;

        public oberflaeche()
        {
            InitializeComponent();

            button2.Enabled = false;

            //textBox1
            textBox1.Multiline = true;
            textBox1.ScrollBars = ScrollBars.Vertical;
            textBox1.AcceptsReturn = true;
            textBox1.AcceptsTab = true;
            textBox1.WordWrap = true;

            //textBox2
            textBox2.Multiline = true;
            textBox2.ScrollBars = ScrollBars.Vertical;
            textBox2.AcceptsReturn = true;
            textBox2.AcceptsTab = true;
            textBox2.WordWrap = true;

            anafil = new analyseFiles();
        }

        public void tBoxFilled(string name)
        {
            this.textBox5.Text = name;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int i = 0;
            int x = 0;

            //create an open file dialog
            OpenFileDialog dlgOpen1 = new OpenFileDialog();
            dlgOpen1.Filter = "Text Files (*.txt)|*.txt";
            dlgOpen1.Title = "Select the Text Files";
            dlgOpen1.Multiselect = true;

            //display the dialog and return results
            if (dlgOpen1.ShowDialog() == DialogResult.OK)
            {
                foreach (string l in dlgOpen1.FileNames)
                {
                    if(i == 100)
                    {
                        MessageBox.Show("Es sind zu viele Text-Files! Bitte fügen Sie max. nur 100 hinzu!!");
                        break;
                    }

                    txtFiles[i] = l;
                    i++;
                    textBox1.Text += l + "\r\n";
                }
            }

            for (int j = 0; j < 100; j++)
            {
                if (txtFiles[j] != null && txtFiles[j] != "")
                {
                    StreamReader sr = new StreamReader(txtFiles[j], System.Text.Encoding.GetEncoding("ISO-8859-1"));
                    string zeile;
                    while ((zeile = sr.ReadLine()) != null)
                    {
                        x++;
                    }
                    textBox2.Text += x + "\r\n";
                    txtFilesRows[j] = x.ToString();
                }
                x = 0;
            }

            checkFiles.splitFiles(txtFiles);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strMeldung = "Do you really want to close?";

            DialogResult result = MessageBox.Show(strMeldung,
                                  Application.ProductName,
                                  MessageBoxButtons.OKCancel,
                                  MessageBoxIcon.Question,
                                  MessageBoxDefaultButton.Button2);

            if (result == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtFiles[0] == null)
            {
                string meldung7 = "You have not selected any text-file!";
                DialogResult res7 = MessageBox.Show(meldung7,
                                    Application.ProductName,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
            }

            else
            {
                DLLpath = comboBox1.Text;
                analyseFiles.getFileInformations(true);
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (Suchen.dateien[0] == null)
            {
                string meldung4 = "You have not selected a correct config-file!";
                DialogResult res4 = MessageBox.Show(meldung4,
                                    Application.ProductName,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
            }
            else if (pfad == "")
            {
                string meldung6 = "You have not selected a correct config-file!";
                DialogResult res6 = MessageBox.Show(meldung6,
                                    Application.ProductName,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
            }

            else
            {
                DLLpath = comboBox1.Text;

                dllImport.LoadName(DLLpath);
                textBox3.Text = dllImport.name2;

                dllImport.LoadDescription(DLLpath);
                textBox4.Text = dllImport.description2;

                button2.Enabled = true;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Text Files (*.txt)|*.txt";
            openDialog.Title = "Select the Text Files";

            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                StreamReader reader = new StreamReader(File.OpenRead(openDialog.FileName));
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    pfad = line;
                }

                Suchen.getCustomFiles(pfad, ".dll");

                if (Suchen.dateien[0] == null && Suchen.dateien[0] != "")
                {
                    string meldung5 = "No PlugIns were found!!\nMaybe a wrong config-file was selected!";
                    DialogResult res5 = MessageBox.Show(meldung5,
                                        Application.ProductName,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                }

                for (int d = 0; d < 100; d++)
                {
                    if (Suchen.dateien[d] != null && Suchen.dateien[d] != "")
                    {
                        comboBox1.Items.Add(Suchen.dateien[d]);
                    }
                }

                if (Suchen.dateien[0] != null && Suchen.dateien[0] != "")
                {
                    comboBox1.SelectedItem = Suchen.dateien[0];
                }

                reader.Close();
            }
        }
    }
}
 

Neue Beiträge

Zurück