2017-07-01 2 views
1
public partial class displayvoltage : UserControl 
{ 
    public displayvoltage() 
    { 
     InitializeComponent(); 
     if (!this.ratio_1.Checked && !this.ratio_12.Checked && !this.ratio_34.Checked && !this.ratio_14.Checked) 
      this.ratio_1.Checked = true; 
    } 

    public double Ratio 
    { 
     get 
     { 
      if (this.ratio_1.Checked) return 1.0; 
      if (this.ratio_1.Checked) return 4.0/3.0; 
      if (this.ratio_1.Checked) return 2.0; 
      return 4.0; 
     } 
    } 

    public int SetRatio 
    { 
     set 
     { 
      if (value == 1) this.ratio_1.Checked = true; 
      if (value == 2) this.ratio_34.Checked = true; 
      if (value == 3) this.ratio_12.Checked = true; 
      if (value == 4) this.ratio_14.Checked = true; 
      SetRatio = value; 
     } 
    } 

    [DefaultValue(0.0)] 
    public double Voltage 
    { 
     get { return Voltage * this.Ratio; } 
     set { Voltage = value; } 
    } 

    private bool DisplayVoltage = false; 
    private bool Pause = false; 

    private void ratio_CheckedChanged(object sender, EventArgs e) 
    { 
     RadioButton r = (RadioButton)sender; 

     if (r.Checked) Invalidate(); 
    } 
} 

créé avec le concepteur seulement 4 radios et un panneau. Même si je veux afficher les propriétés du contrôle VS se bloque, si je démarre le programme il se bloque. Quel peut être le problème?Ce code de contrôle utilisateur simple bloque VS2015. Ne sais pas pourquoi

Puis-je avoir une propriété avec seulement obtenir?

+1

Vous allez créer des exceptions de débordement de pile dans SetRatio et Voltage. Lorsque VS charge votre contrôle, l'exécution de ce code et le système d'exploitation vont l'arrêter. –

Répondre

2

Peut-être quelques raisons, mais plus il est probable que cela provoque une boucle infinie qui provoque une StackOverflow:

public int SetRatio 
{ 
    set 
    { 
     if (value == 1) this.ratio_1.Checked = true; 
     if (value == 2) this.ratio_34.Checked = true; 
     if (value == 3) this.ratio_12.Checked = true; 
     if (value == 4) this.ratio_14.Checked = true; 
     SetRatio = value; 
    } 
} 

La dernière ligne setRatio pourrait être appeler le poseur de propriété setRatio qui provoque le code exécuter à nouveau à partir de:

if (value == 1) this.ratio_1.Checked = true; 
if (value == 2) this.ratio_34.Checked = true; 
if (value == 3) this.ratio_12.Checked = true; 
if (value == 4) this.ratio_14.Checked = true; 
SetRatio = value; 

Et en boucle pour toujours. VS et .Net ne gèrent pas les dépassements de capacité et les exceptions de mémoire insuffisantes.

Essayez:

int setRatio; 
public int SetRatio 
{ 
    set 
    { 
     if (value == 1) this.ratio_1.Checked = true; 
     if (value == 2) this.ratio_34.Checked = true; 
     if (value == 3) this.ratio_12.Checked = true; 
     if (value == 4) this.ratio_14.Checked = true; 
     setRatio = value; 
    } 
} 

Si cela ne fonctionne pas essayez de changer votre constructeur pour voir si cela est à l'origine de la question parce que les contrôles avec les constructeurs qui jettent des exceptions peuvent provoquer VS crash ainsi:

public displayvoltage() 
{ 
    InitializeComponent(); 
    //if (!this.ratio_1.Checked && !this.ratio_12.Checked && !this.ratio_34.Checked && !this.ratio_14.Checked) 
    // this.ratio_1.Checked = true; 
} 
+0

le constructeur était à l'origine du problème, il a commencé à se bloquer même lorsque je construisais la solution. Je vous remercie. –