PDA

View Full Version : [C#]Problema con Convertitore


Kleidemos
17-02-2003, 19:54
Ho questo cod:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace EuroConv
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class FrmPrinc : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtEuro;
private System.Windows.Forms.TextBox txtLire;
private System.Windows.Forms.Button btCalcola;
private System.Windows.Forms.ComboBox select;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public FrmPrinc()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtEuro = new System.Windows.Forms.TextBox();
this.txtLire = new System.Windows.Forms.TextBox();
this.btCalcola = new System.Windows.Forms.Button();
this.select = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// txtEuro
//
this.txtEuro.Location = new System.Drawing.Point(296, 8);
this.txtEuro.Name = "txtEuro";
this.txtEuro.Size = new System.Drawing.Size(144, 20);
this.txtEuro.TabIndex = 0;
this.txtEuro.Text = "";
//
// txtLire
//
this.txtLire.Location = new System.Drawing.Point(8, 8);
this.txtLire.Name = "txtLire";
this.txtLire.Size = new System.Drawing.Size(144, 20);
this.txtLire.TabIndex = 2;
this.txtLire.Text = "";
//
// btCalcola
//
this.btCalcola.Location = new System.Drawing.Point(184, 72);
this.btCalcola.Name = "btCalcola";
this.btCalcola.Size = new System.Drawing.Size(88, 24);
this.btCalcola.TabIndex = 1;
this.btCalcola.Text = "Calcola";
this.btCalcola.Click += new System.EventHandler(this.calcola_Click);
//
// select
//
this.select.Items.AddRange(new object[] {
"€ -> &",
"& -> €"});
this.select.Location = new System.Drawing.Point(184, 8);
this.select.Name = "select";
this.select.Size = new System.Drawing.Size(72, 21);
this.select.TabIndex = 3;
//
// FrmPrinc
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(448, 101);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.select,
this.btCalcola,
this.txtLire,
this.txtEuro});
this.Name = "FrmPrinc";
this.Text = "EuroConv";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FrmPrinc());
}
private void calcola_Click(object sender, System.EventArgs e)
{
double lir = double.Parse(this.txtLire.Text);
double eur = double.Parse(this.txtEuro.Text);
double tot_lire, tot_euro;
int CAMBIO = 1936;
if (this.txtEuro.Text != " " && this.select.Items[0])
{
tot_euro = lir/CAMBIO;
this.txtEuro.Text = tot_euro.ToString();

}
if (this.txtLire.Text != " " && this.select.Items[1])
{
tot_lire = eur*CAMBIO;
this.txtLire.Text = tot_lire.ToString();
}
}
}
}



Ma mi da errore!
Come risolvo la clausala dell'if?

atragon
17-02-2003, 20:07
In effetti così a occhio....
this.select.Items[0] questo cos'è?? cosa vuoi dire?

Kleidemos
17-02-2003, 20:16
Originally posted by "atragon"

In effetti così a occhio....
this.select.Items[0] questo cos'è?? cosa vuoi dire?

è quello che fa decidere se fare euro-lire o lire-euro :D

atragon
18-02-2003, 00:25
Usare una combobox e fare riferimento a SelectedIndex?

Kleidemos
18-02-2003, 06:41
Originally posted by "atragon"

Usare una combobox e fare riferimento a SelectedIndex?

e a casa dovrei riferirmi?

P.S: allego il progetto, magari ti serve a capire le cazzate che sto facendo

atragon
18-02-2003, 19:37
Definisci una comboBox e scrivi una istruzione del tipo
if ((this.txtEuro.Text != " ") && (comboBox1.SelectedIndex == 0)
ecc...

Kleidemos
18-02-2003, 19:53
ora lo compila ma mi da:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info)
at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
at EuroConv.FrmPrinc.calcola_Click(Object sender, EventArgs e) in c:\documents and settings\admin\documenti\visual studio projects\euroconv\form1.cs:line 149
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.0
CodeBase: file:///c:/windows/microsoft.net/framework/v1.0.3705/mscorlib.dll
----------------------------------------
EuroConv
Assembly Version: 0.0.1.1
Win32 Version: 0.0.1.1
CodeBase: file:///C:/Documents%20and%20Settings/Admin/Documenti/Visual%20Studio%20Projects/EuroConv/bin/Debug/EuroConv.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.0
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.3300.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.0
CodeBase: file:///c:/windows/assembly/gac/system/1.0.3300.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.0
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.3300.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.0
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.3300.0__b77a5c561934e089/system.xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.

atragon
18-02-2003, 20:07
System.FormatException: Input string was not in a correct format.

E' un problema di cosa gli metti in input...non puoi infilarci dentro, che so io, una 'a' laddove si aspetta un numero, e dal momento che non gestisci le eccezioni, il risultato è un bell'impallamento.

Kleidemos
18-02-2003, 20:12
gli do

double tot_lire, tot_euro;

in questo modo:

this.txtEuro.Text = tot_euro.ToString();
this.txtLire.Text = tot_lire.ToString();

atragon
18-02-2003, 20:22
Non è il codice il problema, se compila, è quello che scrivi in input....magari è il separatore dei decimali che è scorretto, oppure metti degli spazi, non lo so, prova prima con degli interi

Kleidemos
18-02-2003, 20:24
Originally posted by "atragon"

Non è il codice il problema, se compila, è quello che scrivi in input....magari è il separatore dei decimali che è scorretto, oppure metti degli spazi, non lo so, prova prima con degli interi

ti allego l'exe!

atragon
18-02-2003, 20:38
Ragiona: ho visto nel tuo codice

double lir = double.Parse(this.txtLire.Text);// le lire
double eur = double.Parse(this.txtEuro.Text);// l\' euro

In pratica gli fai parsare i campi textanche se non gli metti nulla in input e il pgm si incavola....infatti se metti un valore significativo in entrambi i campi non ti da + quel problema...come ti ho detto quell'errore è molto chiaro: ti dice che è erata la stringa in input...comprendido?

Kleidemos
18-02-2003, 20:52
Originally posted by "atragon"

Ragiona: ho visto nel tuo codice

double lir = double.Parse(this.txtLire.Text);// le lire
double eur = double.Parse(this.txtEuro.Text);// l\' euro

In pratica gli fai parsare i campi textanche se non gli metti nulla in input e il pgm si incavola....infatti se metti un valore significativo in entrambi i campi non ti da + quel problema...come ti ho detto quell'errore è molto chiaro: ti dice che è erata la stringa in input...comprendido?

e lo risolvo?
riempiendo con 0 i campi all'inizio :confused:

atragon
18-02-2003, 21:01
Eh, eh, quella può essere una soluzione, o meglio un aggiramento del problema ma sforzati un po' e vedrai che puoi fare di meglio....

Kleidemos
18-02-2003, 21:57
Originally posted by "atragon"

Eh, eh, quella può essere una soluzione, o meglio un aggiramento del problema ma sforzati un po' e vedrai che puoi fare di meglio....

e ke la skuola rompe!
Cmq sta settimana ho l'autogestione( :D ) e vedemo un po