Home > other >  I cannot get the "firstplace" etc to print to textbox in .net forms, what can i do to fix
I cannot get the "firstplace" etc to print to textbox in .net forms, what can i do to fix

Time:02-23

How do i get this to run in the form all the textboxes are set right, i have tried a couple things i need the "1st 2nd and 3rd" to print inside the textbox. I have no errors and to me the code looks right. But however I am still a beginner at this. This is for a class project, and it has stumped me honestly c# can be difficult at times. I was wondering if someone could help me fix this I would appreciate it!

namespace Swimmers_Race
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ///Define Variables
            string swim1name = "";
            string swim2name = "";
            string swim3name = "";
            double swim1time = 0;
            double swim2time = 0;
            double swim3time = 0;
            string firstplace = "";
            string secondplace = "";
            string thirdplace = "";
            ///Change from a Variable to String
        
        



            ///Getting Info From textbox to Var
            swim1name = swim1nametxt.Text;
            swim2name = swim2nametxt.Text;
            swim3name = swim3nametxt.Text;
            swim1time = double.Parse(swim1timetxt.Text);
            swim2time = double.Parse(swim2timetxt.Text);
            swim3time = double.Parse(swim3timetxt.Text);



            ///If Statement
            if (swim1time < swim2time && swim2time < swim3time)
            {
                firstplace = swim1name;
                secondplace = swim2name;
                thirdplace = swim3name;
            }
            else if (swim2time < swim1time && swim1time < swim3time)
            {
            firstplace = swim2name;
            secondplace = swim1name;
            thirdplace = swim3name;
            }
            else if (swim2time < swim3time && swim3time < swim1time)
            {
                firstplace = swim2name;
                secondplace = swim3name;
                thirdplace = swim1name;
            }
            else if (swim1time < swim3time && swim3time < swim2time)
            {
                firstplace = swim1name;
                secondplace = swim3name;
                thirdplace = swim2name;
            }
            else if (swim3time < swim1time && swim1time < swim2time)
            {
                firstplace = swim3name;
                secondplace = swim1name;
                thirdplace = swim2name;
            }
            else if (swim3time < swim2time && swim2time < swim1time)
            {
                firstplace = swim3name;
                secondplace = swim2name;
                thirdplace = swim1name;
            }
            // Display The Results from Textbox To Variable


            firstplace = firstplacetxt.Text.ToString();
            secondplace = secondplacetxt.Text.ToString();
            thirdplace = thirdplacetxt.Text.ToString();
        
        }

        
    }
 }

CodePudding user response:

original code

firstplace = firstplacetxt.Text.ToString();
secondplace = secondplacetxt.Text.ToString();
thirdplace = thirdplacetxt.Text.ToString();

I put "****.ToString()" before making the textbox equal to the places

firstplacetxt.Text.ToString();
secondplacetxt.Text.ToString();
thirdplacetxt.Text.ToString();
firstplacetxt.Text = firstplace;
secondplacetxt.Text = secondplace;
thirdplacetxt.Text = thirdplace;

CodePudding user response:

/// Define Variables
   ...
/// Getting Info From textbox to Var
   ...
/// If Statement (rank the swimmers)
   ...

All good till this point.

// Display The Results from Textbox To Variable
firstplace = firstplacetxt.Text.ToString();
    ...

Once you have ranked the swimmers by time, you need to assign the variables to the respective textboxes; you are doing the opposite. Instead, you need to do

// Assign The Results from variables to Textbox
firstplacetxt.Text = firstplace;
   ...
  • Related