Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

03.05.2014, 09:13

C# Fenster wird unendlich mal ausgeführt

Hallo
Ich weis ich hab diese Frage schon mal gepostet, aber ich dachte mir das es vielleicht nich alle sehen wenn ich die Frage einfach unten hinschreibe.
Hier mal zuerst der Code:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace Hexconverter
{
    class Program
    {
        static void Main(string[] args)
        {
//int i = 1;
            {
                string path = Console.ReadLine();
                if (File.Exists(path)==true)
                {


                    // Open the file to read from.
                    string readText = File.ReadAllText(path);
                    char[] values = readText.ToCharArray();
                    foreach (char letter in values)
                    {

                        // Get the integral value of the character.
                        int value = Convert.ToInt32(letter);
                        // Convert the decimal value to a hexadecimal value in string form.
                        string hexOutput = String.Format("{0:X}", value);
                        //Console.WriteLine(i++);
                        MyForm form = new MyForm();
                        form.lblText1.Text = path;
                        form.Show();
                        Console.Write("{1} ", letter, hexOutput, readText);


                    }
                }
                else
                {
                    Environment.Exit(0);
                }



                Console.WriteLine("");
                
                
                   string path1 = Console.ReadLine();
                   if (File.Exists(path1) == true)
                   {
                       {
                           // Open the file to read from.
                           string readText1 = File.ReadAllText(path1);
                           char[] values1 = readText1.ToCharArray();
                           foreach (char letter1 in values1)
                           {
                               // Get the integral value of the character.
                               int value1 = Convert.ToInt32(letter1);
                               // Convert the decimal value to a hexadecimal value in string form.
                               string hexOutput = String.Format("{0:X}", value1);
                               //Console.WriteLine(i++);
                               {
                                   MyForm form = new MyForm();
                                   form.lblText2.Text = path1;
                                   form.Show();
                                   Console.Write("{1} ", letter1, hexOutput, readText1);

                               }
                           }
                       }
                       
                   }
                   else
                   {
                       Environment.Exit(0);
               
                   }
                    
                

                string MyString = path;
                Console.WriteLine(String.Compare(MyString, path1));
                if (MyString == path1)
                {
                    Console.WriteLine("Gleich!");
                }
                else
                {
                    {
                        Console.WriteLine("Nicht Gleich!");
                    }
                }
                Console.ReadKey();





            }
        }
    }
}


Mein Problem: Die WinForm wird unendlich mal ausgeführt. Wieso?

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

2

03.05.2014, 09:32

naja, schau dir doch mal genau an, wann dein Fenster geöffnet wird. Oder noch besser: Schau mit dem Debugger, wie oft er in Zeile 33 landet :).

3

03.05.2014, 09:48

naja, schau dir doch mal genau an, wann dein Fenster geöffnet wird. Oder noch besser: Schau mit dem Debugger, wie oft er in Zeile 33 landet :).

So besser:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace Hexconverter
{
    class Program
    {
        static void Main(string[] args)
        {
//int i = 1;
            {
                string path = Console.ReadLine();
                if (File.Exists(path) == true)
                {


                    // Open the file to read from.
                    string readText = File.ReadAllText(path);
                    char[] values = readText.ToCharArray();
                    foreach (char letter in values)
                    {

                        // Get the integral value of the character.
                        int value = Convert.ToInt32(letter);
                        // Convert the decimal value to a hexadecimal value in string form.
                        string hexOutput = String.Format("{0:X}", value);
                        //Console.WriteLine(i++);
                        Console.Write("{1} ", letter, hexOutput, readText);




                    }

                }
                else
                {
                    Environment.Exit(0);
                }



                Console.WriteLine("");


                string path1 = Console.ReadLine();
                if (File.Exists(path1) == true)
                {
                    {
                        // Open the file to read from.
                        string readText1 = File.ReadAllText(path1);
                        char[] values1 = readText1.ToCharArray();
                        foreach (char letter1 in values1)
                        {
                            // Get the integral value of the character.
                            int value1 = Convert.ToInt32(letter1);
                            // Convert the decimal value to a hexadecimal value in string form.
                            string hexOutput = String.Format("{0:X}", value1);
                            //Console.WriteLine(i++);
                            {
                                Console.Write("{1} ", letter1, hexOutput, readText1);

                            }
                        }
                    }

                }
                else
                {
                    Environment.Exit(0);

                }
                {
                    MyForm form = new MyForm();
                    form.lblText1.Text = path;
                    form.lblText2.Text = path1;
                    form.Show();
                }

                string MyString = path;
                Console.WriteLine(String.Compare(MyString, path1));
                if (MyString == path1)
                {
                    Console.WriteLine("Gleich!");
                }
                else
                {
                    {
                        Console.WriteLine("Nicht Gleich!");
                    }
                }
                Console.ReadKey();





            }
        }
    }
}


?? er öffnet es jetz nur einmal aber in der KOnsolenanwendung steht nur noch 48 und die WinForm stürzt ab... Wie mach ich das überhaupt das er den gewünschten Text (Hexkonvertierten Text in den Label schreibt?

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »layle« (03.05.2014, 10:07)


TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

4

03.05.2014, 10:08

Ich glaube du verstehst deinen Programmcode nicht. Was würdest du denn von folgendem Code erwarten, was er tut?

C#-Quelltext

1
2
3
4
5
6
7
string values = "This is a long text"

foreach (char letter in values)
{
    MyForm form = new MyForm();
    form.Show();
}


Jetzt ist der Aufruf weg, der vorhin noch stand :P

Naja, was ist denn die Ausnahme? Visual Studio sagt dir genau, was nicht stimmt!

5

03.05.2014, 10:11

Ich glaube du verstehst deinen Programmcode nicht. Was würdest du denn von folgendem Code erwarten, was er tut?

C#-Quelltext

1
2
3
4
5
6
7
string values = "This is a long text"

foreach (char letter in values)
{
    MyForm form = new MyForm();
    form.Show();
}


Jetzt ist der Aufruf weg, der vorhin noch stand :P

Naja, was ist denn die Ausnahme? Visual Studio sagt dir genau, was nicht stimmt!


Visual Studio sagt mir nichts rein gar nichts....
Hast du den korrigierten Teil gesehen?

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

6

03.05.2014, 10:28

Ich habe deinen Programmcode kurz mal angeschaut. Bei mir hängt bei der Anzeige des Fensters, was nicht verwunderlich ist:
Du öffnest ein Fenster mit Show(), blockierst aber den aufrufenden Thread mit einem Console.ReadKey(). Dadurch kann das Fenster gar nicht reagieren. Entweder, du erstellst und zeigst das Fenster aus einem anderem Thread heraus, oder gibst dem Hauptthread Zeit. Ich würde dir ja zu letzterem raten. Einfach an Stelle von Console.ReadKey() folgendes machen:

C#-Quelltext

1
2
3
4
5
6
7
8
while (true)
{
    if (Console.KeyAvailable)
    {
        break;
    }
    Application.DoEvents();
}


Da man merkt, dass du quasi keine Erfahrung mit Windows-Programmierung hast, solltest du dir dringend Literatur zulegen! Schlage alle hier aufgeführten Befehle nach und verstehe, was sie tun.

7

03.05.2014, 10:38

Ich habe deinen Programmcode kurz mal angeschaut. Bei mir hängt bei der Anzeige des Fensters, was nicht verwunderlich ist:
Du öffnest ein Fenster mit Show(), blockierst aber den aufrufenden Thread mit einem Console.ReadKey(). Dadurch kann das Fenster gar nicht reagieren. Entweder, du erstellst und zeigst das Fenster aus einem anderem Thread heraus, oder gibst dem Hauptthread Zeit. Ich würde dir ja zu letzterem raten. Einfach an Stelle von Console.ReadKey() folgendes machen:

C#-Quelltext

1
2
3
4
5
6
7
8
while (true)
{
    if (Console.KeyAvailable)
    {
        break;
    }
    Application.DoEvents();
}


Da man merkt, dass du quasi keine Erfahrung mit Windows-Programmierung hast, solltest du dir dringend Literatur zulegen! Schlage alle hier aufgeführten Befehle nach und verstehe, was sie tun.


Danke jetzt klappts! Jetzt bleibt trotzdem noch ein Problem :dash:
Er zeigt mir im Label den Namen des Pfades statt des Inhaltes...
Stimmtdas so überhaupt?

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace Hexconverter
{
    class Program
    {
        static void Main(string[] args)
        {
//int i = 1;
            {
                
                string path = Console.ReadLine();
                if (File.Exists(path) == true)
                {


                    // Open the file to read from.
                    string readText = File.ReadAllText(path);
                    char[] values = readText.ToCharArray();
                    foreach (char letter in values)
                    {

                        // Get the integral value of the character.
                        int value = Convert.ToInt32(letter);
                        // Convert the decimal value to a hexadecimal value in string form.
                        string hexOutput = String.Format("{0:X}", value);
                        //Console.WriteLine(i++);
                        Console.Write("{1} ", letter, hexOutput, readText);




                    }

                }
                else
                {
                    Environment.Exit(0);
                }



                Console.WriteLine("");


                string path1 = Console.ReadLine();
                if (File.Exists(path1) == true)
                {
                    {
                        // Open the file to read from.
                        string readText1 = File.ReadAllText(path1);
                        char[] values1 = readText1.ToCharArray();
                        foreach (char letter1 in values1)
                        {
                            // Get the integral value of the character.
                            int value1 = Convert.ToInt32(letter1);
                            // Convert the decimal value to a hexadecimal value in string form.
                            string hexOutput = String.Format("{0:X}", value1);
                            //Console.WriteLine(i++);
                            {
                                Console.Write("{1} ", letter1, hexOutput, readText1);

                            }
                        }
                    }

                }
                else
                {
                    Environment.Exit(0);

                }
                {
                    
                    MyForm form = new MyForm();
                    form.lblText1.Text = path; //Hier müsste glaub ich readText
                    form.lblText2.Text = path1; //und hier readText1 stehen? Leider kommt hier die Meldung:Fehler   1   Der Name "readText(1)" ist im aktuellen Kontext nicht vorhanden.
                    form.Show();
                }

                string MyString = path;
                Console.WriteLine(String.Compare(MyString, path1));
                if (MyString == path1)
                {
                    Console.WriteLine("Gleich!");
                }
                else
                {
                    {
                        Console.WriteLine("Nicht Gleich!");
                    }
                }
                while (true)
                {
                    if (Console.KeyAvailable)
                    {
                        break;
                    }
                    Application.DoEvents();
                }





            }
        }
    }
}


So funktioniert das auch nicht:

C#-Quelltext

1
2
3
4
5
6
7
8
{
                    string readText1 = path1;
                    string readText = path;
                    MyForm form = new MyForm();
                    form.lblText1.Text = path;
                    form.lblText2.Text = path1;
                    form.Show();
                }

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

8

03.05.2014, 10:44

Naja du gibst nur die Pfade rein, wie soll er denn den Inhalt darstellen? Dann musst du dir die Inhalte von File.ReadAllText() + dessen Konvertierung merken und dem Formular übergeben. Ich war mal so frei und hab deinen Code aufgeräumt, der ist ziemlich schlampig:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace HexConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = Console.ReadLine();
            if (!ReadFileAndPrintHexaDecimalContent(path)) return;
                
            Console.WriteLine();
                
            var path1 = Console.ReadLine();
            if (!ReadFileAndPrintHexaDecimalContent(path1)) return;

            ShowWindowWithFileNames(path, path1);

            CompareFiles(path, path1);
            WaitForQuit();
        }

        private static bool ReadFileAndPrintHexaDecimalContent(string path)
        {
            if (!File.Exists(path)) return false;

            var readText = File.ReadAllText(path);
            var values = readText.ToCharArray();
            foreach (var letter in values)
            {
                var value = Convert.ToInt32(letter);
                var hexOutput = String.Format("{0:X}", value);
                Console.Write("{1} ", letter, hexOutput, readText);
            }

            return true;
        }

        private static void ShowWindowWithFileNames(string path1, string path2)
        {
            MyForm form = new MyForm();
            form.lblText1.Text = path;
            form.lblText2.Text = path1;
            form.Show();
        }

        private static void CompareFiles(string filePath1, string filePath2)
        {
            Console.WriteLine(String.Compare(filePath1, filePath2));
            if (filePath1 == filePath2)
            {
                Console.WriteLine("Gleich!");
            }
            else
            {
                Console.WriteLine("Nicht Gleich!");
            }
        }

        private static void WaitForQuit()
        {
            while (true)
            {
                if (Console.KeyAvailable) break;
                Application.DoEvents();
            }   
        }
    }
}

9

04.05.2014, 08:47

Naja du gibst nur die Pfade rein, wie soll er denn den Inhalt darstellen? Dann musst du dir die Inhalte von File.ReadAllText() + dessen Konvertierung merken und dem Formular übergeben. Ich war mal so frei und hab deinen Code aufgeräumt, der ist ziemlich schlampig:

C#-Quelltext

1
2
3
4
5
6
7
        private static void ShowWindowWithFileNames(string path1, string path2)
        {
            MyForm form = new MyForm();
            form.lblText1.Text = path;
            form.lblText2.Text = path1;
            form.Show();
        }

private static void ShowWindowWithFileNames(string path1, string path2) { MyForm form = new MyForm(); form.lblText1.Text = path; form.lblText2.Text = path1; form.Show(); }


Guten Morgen Zusammen
Ich will ja nicht unhöfflich sein aber kann es sein dass sich bei dir dort ein Fehler eingeschlichen hat? Müsste es nicht so aussehen?

C#-Quelltext

1
2
3
4
5
6
7
private static void ShowWindowWithFileNames(string path, string path1) 
        {
            MyForm form = new MyForm();
            form.lblText1.Text = path; // oder hier eifach path1
            form.lblText2.Text = path1; //und hier path2
            form.Show();
        }


und bei diesem Teil komm ich nicht ganz draus:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
 private static void CompareFiles(string filePath1, string filePath2) //Muss ich hier filePath1 und filePath2 durch den Befehl für den Pfad eingeben? Oder vielleich nur den Pfad?
        {
            Console.WriteLine(String.Compare(filePath1, filePath2)); //Hier dassselbe
            if (filePath1 == filePath2) //Hier dasselbe
            {
                Console.WriteLine("Gleich!");
            }
            else
            {
                Console.WriteLine("Nicht Gleich!");
            }
        }

10

04.05.2014, 10:19

Jetzt klappts schon besser! Der Inhalt der Datei wird jetzt eingefügt aber leider so wie er in datei steht also z.B. Hallo ich heisste Fritz statt 48:61:6c:6c:6f:20:69:63:68:20:68:65:69:73:73:74:65:20:46:72:69:74:7a.
Hier mal der kleine Codeausschnitt:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private static void ShowWindowWithFileNames(string path, string path1)
        {

            MyForm form = new MyForm();
            var hex1 = File.ReadAllText(path);
            var values = hex1.ToCharArray();
            foreach (var letter in values)
            {
                var value = Convert.ToInt32(letter);
                var hexOutput = String.Format("{0:X}", value);
                //Console.Write("{1} ", letter, hexOutput, hex1); Der benötigte Teil ist ja: ("{1} ", letter, hexOutput, readText).
                //form.lblText.Text("{1} ", letter, hexOutput, hex1); verursacht Fehlermeldung
                form.lblText1.Text = hex1;
            }
           var hex2 = File.ReadAllText(path1);
            values = hex2.ToCharArray();
            foreach (var letter in values)
            {
                var value = Convert.ToInt32(letter);
                var hexOutput = String.Format("{0:X}", value);
                form.lblText2.Text = hex2;
                //form.lblText.Text("{1} ", letter, hexOutput, hex2); verursacht Fehlermeldung
                form.Show();
            }
        }

Ich habe versucht es so zu lösen: form.lblText.Text("{1} ", letter, hexOutput, hex1); es verursacht aber 2 Fehlermeldungen: Fehler 1 Der nicht aufrufbare Member "System.Windows.Forms.Control.Text" kann nicht wie eine Methode verwendet werden.
Fehler 2 "System.Windows.Forms.Label.Text" ist ein(e) "Eigenschaft", wird aber wie ein(e) "Methode" verwendet.

Was nun? Ist die Idee komplett falsch? Oder mach ich was falsch?

Werbeanzeige