C# Focus bei UserControl?

Hallo,

ich hab ein Problem mit dem Focus eines UserControls. Und zwar bekommt das UserControl trotzdem den Focus obwohl alle seine ChildControls nicht focusierbar sind. Ich hab hier mal ein kleines Beispiel erstellt, zum testen einfach ein neues Windows Forms Project anlegen und den Code in den Source Code der Form kopieren.

using System.Windows.Forms;

namespace testUserControl
{
  using System.Diagnostics.Eventing.Reader;

  public partial class Form1 : Form
  {
    private testControl testControl1;
    private testControl testControl2;
    private testControl testControl3;
    
    public Form1()
    {
      InitializeComponent();

      this.testControl1 = new testUserControl.testControl();
      this.testControl2 = new testUserControl.testControl();
      this.testControl3 = new testUserControl.testControl();

      this.testControl1.Location = new System.Drawing.Point(24, 27);
      this.testControl1.Name = "testControl1";
      this.testControl1.Size = new System.Drawing.Size(150, 37);
      this.testControl1.TabIndex = 0;


      this.testControl2.Location = new System.Drawing.Point(24, 71);
      this.testControl2.Name = "testControl2";
      this.testControl2.Size = new System.Drawing.Size(150, 45);
      this.testControl2.TabIndex = 1;


      this.testControl3.Location = new System.Drawing.Point(24, 136);
      this.testControl3.Name = "testControl3";
      this.testControl3.Size = new System.Drawing.Size(150, 47);
      this.testControl3.TabIndex = 2;

      this.Controls.Add(this.testControl3);
      this.Controls.Add(this.testControl2);
      this.Controls.Add(this.testControl1);

      testControl2.Disable();
    }
  }

  public class testControl : UserControl
  {
    private TextBox test;

    private Label TestLabel;

    public testControl()
    {
      this.test = new TextBox();
      this.test.Name = "TextBox";
      this.test.Location = new System.Drawing.Point(100, 5);
      this.test.Size = new System.Drawing.Size(80, 17);

      this.TestLabel = new Label();
      this.TestLabel.Name = "Label";
      this.TestLabel.Text = "Test";
      this.TestLabel.Location = new System.Drawing.Point(10, 5);
      this.TestLabel.Size = new System.Drawing.Size(50, 17);

      this.Controls.Add(this.test);
      this.Controls.Add(this.TestLabel);
    }

    public void Disable()
    {
      this.test.Enabled = false;
    }
  }
}

Ich weiß das das Design jetzt nicht schön aussieht aber das ist auch egal. Um was es mir jetzt geht ist, das wenn ich mit tab durchgehe das 2. Control trotzdem den Focus bekommt obwohl es aus einem lable und einer deaktivierten TextBox besteht. Warum ist das so? Und wie kann ich das umgehen?

Danke für alle Antworten.

Computer, programmieren, C Sharp, Forms
c# bmi rechner mit absoluter zahl korrektes Ergebnis, mit Variable falsch warum?

Habe folgendes Problem, das hier ist der aus dem Code entscheidende Tei.. ich habe auch mal Schritt für Schritt alles durchlaufen lassen um alles nachzuvollziehen, komme aber immer noch nicht weiter.

Folgende Werte werden eingegeben, wie in dem Kommentar teil beschrieben:

Größe = 150cm

Gewicht = 45kg somit käme genau ein bmi von 20 raus da 1,5³ = 2,25 und 45 / 2,25 = 20

allerdings erscheint im textlabel 3 also der Ausgabezelle nach dem Klick auf den Button immer genau der Wert, den ich in das Gewichtsfeld eingetragen habe.

Wenn ich jedoch ((groesse / 100) * (groesse / 100)) durch 2.25 mal zu testzwecken manuell ersetze kommt das korrekte Ergebnis von 20 raus.

ich habe es auch schon mit zwischenschritten probiert. (groesse erst im meter umrechen, dann schon quadrieren und in der Ergebnisformal habe ich dann genauso wie bei dem Zahlenwert 2.25 nur eben als Variablenwert stehen, wie man es noch an den Variablen Deklarationen erkennen kann.

 private void button2_Click(object sender, EventArgs e)
    {
        int groesse;
        double groessem;
        double groessequadrat;

        double gewicht; 
        double bmi;
        
        groesse = int.Parse(textBox1.Text);
        gewicht = double.Parse(textBox2.Text);
        bmi = gewicht / ((groesse / 100) * (groesse / 100)); //20bmi = 45kg / ((150cm / 100) * (150cm / 100)) = [warum 45] 
        label3.Text = bmi.ToString(); 
    }

hat jemand eine Idee, war hier der Fehler sein könnte?

programmieren, absolut, C Sharp, Visual Studio, Variablen

Meistgelesene Beiträge zum Thema C Sharp