string to int

Hello everyone,

Im trying to setup a progress bar, right now this is basically how it is set up, first I have the app count lines in a text file, this works ok:

textBox2.Text = rowCount.ToString();

rowCount++;

inputstring1 = StreamReader1.ReadLine();

then later, I have this where the listbox is being populated, the progress bar should show the progress of the population. textBox2 displays the number of rows, and the maximum should be this number but when I try to compile it, it gives me an error saying that it cant convert string to int, so I tried this with no luck.:

progressBar1.Minimum = 0;

progressBar1.Maximum = Convert.ToInt16(textBox2.Text);

anyone know what Im doing wrong



Answer this question

string to int

  • Dave Corun

    Hi,

    Try using:

    progressBar1.Increment(1);

    instead of --

    progressBar1.Value += 1;

    Regards,

    Vikram



  • Gogou

    The progressBar1.Increment(1); does solve it, but the progressbar immediately goes 100% when I click the button, and after that the listbox items appear, since the items in the listbox dont appear 1 by 1 but rather they all appear at once. I guess this is a little too complicated for my level, Ive been working on this too long and its not a neccessary component. Thanks for the help guys!
  • sk49

    Hi,

    In the same place where u are setting the Minimum and Maximum on the progressbar also add the following line:

    progressBar1.Step = 1;

    Regards,

    Vikram



  • Shrikant24227

    I tried it now, Im not sure if Im putting it in the right place(Ive tried everywhere, nothing worked) or if Im declaring it right, 'int num;' many time I also get an error with 'progressBar1.Value += 1; in the foreach loop.


  • Sowbhagya

    Hi,

    The Convert code that you have written should work as long as you have a numeric value in the textbox - make sure you are trimming the content in the textbox for spaces

    Let me know if you still face the same conversion error.

    Regards,

    Vikram



  • Yozz

    Hi,

    I dont see you doing anything wrong here...

    Just a thought. Why do you need to store the value in a textbox Have it as a integer property of the form at the form level and try re-using it. In that case, you would not need to re-convert it and so the error due to conversion will certainly not be an issue.

    Regards,

    Vikram



  • Pyush Kumar

    Yes, Im using trimming, I can show you a more detailed version of the code:

    {

    while(inputstring1 != null)

    textBox2.Text = rowCount.ToString();

    rowCount++;

    inputstring1 = StreamReader1.ReadLine();

    }}

    catch stuff here

    finally stuff here

    unrelated code here

    then the app starts to load the items from the text file into a listbox:


    progressBar1.Minimum = 0;

    StreamReader r = new StreamReader(@textBox1.Text);

    string ft = r.ReadToEnd();

    string[ ] textLines = ft.Split('\n');

    foreach (string tl in textLines)

    {

    string line = tl.Trim();

    if (line.Length == 0)

    continue;

    listBox1.Items.Add(line);

    progressBar1.Value += 1;

    }

    this may seem disorganized or inefficient but Im new to this. Thanks for any suggestions


  • talay

    hi,

    i have tried this and it worked for me

    private void button1_Click(object sender, EventArgs e)

    {

    this.toolStripProgressBar1.Maximum = 100;

    for (int i = 0; i < 100; i++)

    {

    this.toolStripProgressBar1.Value += 1;

    //wait for sometimes for user to watch

    System.Threading.Thread.Sleep(10);

    }

    //restore the progressbar to 0 again when finish

    this.toolStripProgressBar1.Value = 0;

    }

    i'm  not sure where is your problem but i guess in your code you can do something like this

     

    if (line.Length != 0)

    {

    listBox1.Items.Add(line);

    progressBar1.Value += 1;

    }

    hope this helps



  • Murtala Adewale

    Thats it! it works now, thanks alot for you help again, its much appreciated!
  • string to int