Autor Beitrag
okrim
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: Mi 22.01.14 23:41 
Hallo an alle,

hätte da mal wieder eine Frage!
Und zwar möchte ich mit button1 in Form2 alle TextBoxen und ein dataGridView in Form1 leeren, hab schon eine ganze weile gegooglet, finde aber nur Lösungen um das in dem Aktuellen Form zu machen.
Würde mich freuen wenn mir einer oder mehr :D helfen könnten!

Vielen Dank im Voraus
Gruß Mirko
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 23.01.14 10:21 
Hallo,

das ist ein typisches Problem der Kommunikation von 2 Forms. Je nachdem in welchem Aufrufverhältnis die beiden Forms stehen, entweder mittels Aufruf einer Methode oder aber indirekt mittels eines Ereignisses (Event).
braincom654
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Do 23.01.14 10:54 
Hallo okrim,

habe hier schnell ein Beispielscode geschrieben, hoffe der hilft dir weiter:

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:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
  public partial class Form1 : Form
  {
    readonly Form2 _frm = new Form2();

    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      MessageBox.Show("Clean Form1"string.Format("Cleaned Resources: {0}", _frm.CleanResources()));
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      _frm.Show();
    }
  }

  public partial class Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
    }

    public bool CleanResources()
    {
      try
      {
        foreach (var control in Controls)
        {
          if (control.GetType() == typeof(TextBox))
          {
            var currentResetCtrl = (TextBox)control;
            currentResetCtrl.Text = string.Empty;
          }
          else if (control.GetType() == typeof(DataGridView))
          {
            var currentResetCtrl = (DataGridView)control;
            currentResetCtrl.Rows.Clear();
            currentResetCtrl.Refresh();
          }
        }
        return true;
      }
      catch (Exception)
      {
        return false;
      }
    }
  }


Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
okrim Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: Fr 24.01.14 16:20 
Hallo braincom654,

tausend Dank, es klappt wunderbar, freu mich :lol:
Es ist halt einfach toll wenn einem so schnell und so gut geholfen wird!

Nochmal Danke
Gruß Mirko