Autor Beitrag
#5h0rty#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 16:36 
Hallo alle zusammen.

ich öffne eine Form mit einem Button.
ausblenden C#-Quelltext
1:
2:
3:
4:
 private void key1_Click(object sender, EventArgs e)
        {
            new Form2().Show(); 
        }

nur wenn ich ein zweites mal auf den Butten Klicke öffnet sich die Form noch einmal.
So das ich 2x die selbe Form offen habe.

Frage: Wie kann ich das vermeiden?

danke schon mal im vorraus.

MFG
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 16:57 
Hallo!

Du hast die Klasse Form2, von der erzeugst Du bei jedem Buttonklick mittels new eine neue Instanz. Daddurch wird jedes Mal eine neue Form angezeigt.

Du musst die Instanz einmal erzeugen und dann in einem Feld speichern. Dann kannst Du immer wieder auf dieselbe Instanz zugreifen.

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 18:27 
hallo und danke erst mal für die schnelle Antwort.

Sorry wenn ich das nicht ganz verstehe, bin Anfänger.
Was heist:
"und dann in einem Feld speichern"
wie macht man so was?
Mitmischer 1703
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 754
Erhaltene Danke: 19

Win 7, Debian
Delphi Prism, Delphi 7, RAD Studio 2009 Academic, C#, C++, Java, HTML, PHP
BeitragVerfasst: Di 22.01.08 18:34 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
 private void key1_Click(object sender, EventArgs e)
 var Form2 *TForm
        {
            Form2 = new TForm (Form1->Handle) 
            Form2->Show(); 
        }



Glaub' ich zumindest :), hab mit C in der letzten Zeit nichts gemacht! :zwinker:

_________________
Die Lösung ist nicht siebzehn.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 18:37 
@Mitmischer: C# != C(++/CLI) ;-) Außerdem wird dann immer noch ständig eine neue Instanz erstellt.

Ich meine das so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
public class Form1 : Form
{

  /* ... */

  private Form2 form2;

  private void key1_Click(object sender, EventArgs e)
  {
    if (form2 == null)
      form2 = new Form2();

    form2.Show();
  }
}


Bitte mal das OpenBook konsultieren.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Mitmischer 1703
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 754
Erhaltene Danke: 19

Win 7, Debian
Delphi Prism, Delphi 7, RAD Studio 2009 Academic, C#, C++, Java, HTML, PHP
BeitragVerfasst: Di 22.01.08 18:40 
@Christian: != versteht mein Compiler! :D

_________________
Die Lösung ist nicht siebzehn.
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 19:03 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
public class Form1 : Form
{

  /* ... */

  private Form2 form2;

  private void key1_Click(object sender, EventArgs e)
  {
    if (form2 == null)                                             //Fehler (1)
      form2 = new Form2();                                         //Fehler (2)

    form2.Show();                                                  //Fehler (3)
  }
}


also bei dem 1. und 2. sagt der Comp. mir das es ein "Type" ist, wird aber wie eine "Variable" verwendet.
beim 2. sagt er mir das: form2 enthält keine Definition für "show".

wie muß ich das verstehen?

LG
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 19:13 
Hast Du auf Groß- und Kleinschreibung geachtet?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 19:24 
ja!

ich konte ja auch alles auswählen. also es ist ja auch alles blau und hell blau! nur beim Compelieren meckert er mir das an.

LG
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 19:28 
Folgender Quelltext funktioniert bei mir:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
        private Form2 form2;

        private void button1_Click(object sender, EventArgs e)
        {
            if (form2 == null)
                form2 = new Form2();

            form2.Show();
        }

Der Fehler muss bei Dir also woanders liegen. :nixweiss: Poste doch einfach mal ein größeres Stück Quelltext.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 20:06 
ausblenden 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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HMI
{
    public partial class FCentral : Form
    {
        public FCentral()
        {
            InitializeComponent();
        }


        private void Browserr_DoubleClick(object sender, EventArgs e)
        {
//
//  Alterung
//
            if (Browserr.SelectedNode.Name == "LED Alterung 5")
            {
                if (LEDAlterung == null)
                    LEDAlterung = new LEDAlterung();

                LEDAlterung.Show();
            }
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 20:10 
Wo ist das Feld? Und Du hast doch alles groß geschrieben :roll:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 20:27 
Wie wo ist das Feld?

ausblenden C#-Quelltext
1:
if (Browserr.SelectedNode.Name == "LED Alterung 5"// Das stimmt so das geht.					


ausblenden C#-Quelltext
1:
if (LEDAlterung == null)                            //LEDAlterung ist der Name der Form die ich öffne					


hir ist schon das erste Problem.

FehlerMeldung: LEDAlterung ist ein "Type" wird aber wie eine "Variable" verwendet!!!
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.01.08 20:31 
oh mann jetzt sehe ich das erst.. :shock: ...ich bin doch blöd.... :roll:

Danke Leute so gehts, ist nur ein haufen fleißarbeit bei ca 90 "show" aufrufen.

Danke
mutterholzbein
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 47
Erhaltene Danke: 1

[Win XP pro,Win 7pro, Ubuntu 10.04]
[MS Visual Studio 2008 pro TeamSystem,VS-2010-Ultimate [C, C++ (u.a. mit QT4), C#, PHP]
BeitragVerfasst: Di 22.01.08 23:09 
user profile icon#5h0rty# hat folgendes geschrieben:

private void key1_Click(object sender, EventArgs e)
{
new Form2().Show();
}
[/cs]


hoi user profile icon#5h0rty#,

ich hätte es ja so versucht (geht eigentlich immer ohne probs!)
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
private void key1_Click(object sender, EventArgs e)
{
  Form f2=new Form2();
  
  f2.Show(); //Für einfaches aufrufen
  /* f2.ShowDialog(); //Für Dialog basiertes verhalten...*/
}


denn einfach nur:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
private Form2 f2;

private void button1_Click(object sender, EventArgs e)
{
  if (f2 == null)
    f2 = new Form2();
  f2.Show();
}


wirft nach dem schliessen und erneutem öffnen der form exceptions...

(System.ObjectDisposedException)
greetz mhb :wave:

_________________
Software is like Sex, if you do a mistake you have to support it for the rest of your life.


Zuletzt bearbeitet von mutterholzbein am Fr 28.03.08 03:47, insgesamt 1-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 23:12 
Er wollte ja aber gerade nicht bei jedem Button-Klick ein neues Fenster :zwinker:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
mutterholzbein
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 47
Erhaltene Danke: 1

[Win XP pro,Win 7pro, Ubuntu 10.04]
[MS Visual Studio 2008 pro TeamSystem,VS-2010-Ultimate [C, C++ (u.a. mit QT4), C#, PHP]
BeitragVerfasst: Di 22.01.08 23:45 
Titel: rööh:
user profile iconChristian S. hat folgendes geschrieben:
Er wollte ja aber gerade nicht bei jedem Button-Klick ein neues Fenster :zwinker:


^^hmh, na dann zb so:
als erstes verweis zur Microsoft.VisualBasic-Library ins projekt einbinden.
dann die Program.cs öffnen:
ausblenden volle Höhe 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:
...
using Microsoft.VisualBasic.ApplicationServices;
...
static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main(string[] commandLine)
        {
            Application.SetCompatibleTextRenderingDefault(false);
            CApplication myApp = new CApplication();
            myApp.Run(commandLine);
        }
    }

    class CApplication : WindowsFormsApplicationBase
    {
        public CApplication()
        {
            this.IsSingleInstance = true;
            this.EnableVisualStyles = true;
            this.ShutdownStyle = ShutdownMode.AfterMainFormCloses;
        }

  /* hier wird die "sicherung der hauptform zu gewiesen, lässt sich aber locker anpassen...!"*/
        protected override void OnCreateMainForm()
        {
            this.MainForm = new Form1();
        }
/*natürlich kannst du diese funktion auch anpassen...*/
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            MessageBox.Show("Läuft schon!""Achtung! "+Application.ProductName);
        }
    }
...


greetz mhb :wave:
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 22.01.08 23:58 
Und es geht auch nicht darum, nur eine Instanz des Programmes zu starten, sondern ein Fenster innerhalb des Programmes nicht immer neu zu erstellen ... Das Problem ist ja nun auch schon gelöst :roll:

(Das mit der einen Instanz würde ich über einen Mutex machen)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
#5h0rty# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 23.01.08 08:45 
hi Leute...

Christian S. hat recht.
So wie er es beschrieben hat geht es auf jedenfall und relativ unkompliziert.
Nur für mich währe es noch einfacher wenn ich direkt abfragen kann ob die Form bzw. die Instanz schon geladen ist.

zb.:

ausblenden C#-Quelltext
1:
2:
 if (NewForm = false)                        //direkt die zu öffnende Form (NewForm) abfragen ob true oder false
    new NewForm().Show();                    // (if=false) dann (NewForm)laden...


Weil ich habe einen treeview mit ca 90 Formen die ich von da öffnen kann.
alles andere wäre ein haufen arbeit.

LG
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mi 23.01.08 10:31 
user profile icon#5h0rty# hat folgendes geschrieben:

Nur für mich währe es noch einfacher wenn ich direkt abfragen kann ob die Form bzw. die Instanz schon geladen ist.

Weil ich habe einen treeview mit ca 90 Formen die ich von da öffnen kann.
alles andere wäre ein haufen arbeit.

[OT]Falsches Deutsch: "... weil ich einen Treeview habe..." oder "Denn ich habe..."[/OT]

Zur Sache: Dafür gibt es mehrere Möglichkeiten. Zum einen könntest Du eine allgemeine Methode machen, die die Prüfung auf null vornimmt, bei Bedarf das neue Formular erzeugt und anschließend öffnet. Probleme dabei sind, aus Type bzw. typeof() eine spezielle Instanz von Form2 zu erzeugen und zu prüfen, ob ein solches Formular überhaupt schon existiert.

Im Prinzip kannst Du auf Application.OpenForms-Eigenschaft zugreifen und prüfen, ob ein Formular mit dem betreffenden Namen schon geöffnet ist:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
Form currentForm = Application.OpenForms["Form2"];
if (currentForm == null) {
    currentForm = new Form2();
}
currentForm.Show();


Am einfachsten und sichersten dürfte ein Dictionary<Type, Form> sein. Beim Programmstart trägst Du alle vorhandenen Typen ein:
ausblenden C#-Quelltext
1:
2:
3:
Dictionary<Type, Form> formTypes = new Dictionary<Type, Form>();
formTypes.Add(typeof(Form2), null); 
// oder so ähnlich; mit typeof muss ich immer noch probieren

Bei Bedarf kannst Du direkt prüfen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
CheckForm(typeof(Form2));
//  erledigt folgende Prüfung:
void CheckForm(Type currentType) {
    Form currentForm = formTypes[currentType];
    if (currentForm == null) {
        currentForm = xxx.CreateInstance() as currentType;
        formTypes[currentType] = currentForm;
    }
currentForm.Show();
}

In anderem Zusammenhang habe ich CreateInstance o.ä. schon genutzt, aber nicht bei Formulare. Ich weiß deshalb nicht genau, wie es zu realisieren ist (und habe jetzt keine Zeit zur Suche).

Das sollte aber trotzdem schön weiterhelfen. Jürgen


Zuletzt bearbeitet von JüTho am Mi 23.01.08 10:38, insgesamt 1-mal bearbeitet