开发者

parse float to text box

开发者 https://www.devze.com 2023-03-16 01:01 出处:网络
simple question that is evading me at the moment.i need the 2 lines below to parse to float. with those 2 lines i get this:Error1Cannot implicitly convert type \'test.Form1.ore\' to \'string\'. so i f

simple question that is evading me at the moment.i need the 2 lines below to parse to float. with those 2 lines i get this: Error 1 Cannot implicitly convert type 'test.Form1.ore' to 'string'. so i figure parse the text boxes to float as that is what will be passed to the text boxes can anyone show me what i am missing.

textBox3.Text = books[0]; // update the first text
textBox4.Text = books[1]; // update the second text

I tried this

textBox4.Text = float.Parse(books[1]);  //update the second text

but it did not work either

adding all code for reference

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;

 namespace test
{

public partial class Form1 : Form
{
    [Serializable]
    public class ore
    {
        public float Titan;
        public float Eperton;
    }
    ore b1 = null;
    ore b2 = null;



    public Form1()
    {
        InitializeComponent();

        b2 = new ore();
        b1 = new ore();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        float tempFloat;


        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            b1.Titan = tempFloat;
        }
        else
            MessageBox.Show("uh oh");



    }


    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            b2.Eperton = tempFloat;
        }
        else
            MessageBox.Show("uh oh");


    }


    private void button1_Click(object sender, EventArgs e)
    {
        List<ore> oreData = new List<ore>();
        oreData.Add(b1);
        oreData.Add(b2);

        FileStream fs = new FileStream("ore.dat", FileMode.Create);
        Bi开发者_如何学编程naryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, oreData);
        fs.Close();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    List<ore> books = new List<ore>();
    private void button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        List<ore> books = (List<ore>)bf.Deserialize(fs);
        fs.Close();

        if (books.Count > 1)
        {
            textBox3.Text = float.Parse(books[0]).ToString();//update the first text
            textBox4.Text = float.Parse(books[1]).ToString();
            //update the second text
        }
    }
}

}


"Parse" means to process a string (text) into a particular value type, such as a float or integer. This is not what you need. In fact, you need to do the opposite: get a string representation of some other value.

To assign a non-string value to a textbox, simply call the ToString() method.

double dbl = 0;
MyTextBox.Text = dbl.ToString();


textBox4.Text requires that you assign it a string. You're assigning it a float. If you need to display a float as a string, just use the ToString method.

textBox4.Text = float.Parse(books[1]).ToString();


If you want to convert the contents of the textbox into a number, then you've written the code backwards.

What you have now attempts to assign the return value of the float.Parse method to the Text property of your textbox. That doesn't work, of course, because the function returns a float and the Text property expects a string.

Instead, to parse the string value contained in books[1], you should write this:

float myValue = float.Parse(books[1]));

And to convert a float to a string that you can display in a textbox, you should write this:

textBox4.Text = books[1].ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消