954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to refer to something from a different windows form.

I am making a project using windows form application. i made two other window forms (vehicle.cs and Input.cs). i want to obtain the value from the textbox in input.cs and use it in vehiicles.cs. is There any way to do this. I am trying to make a for loop but need the input value to complete the procedure.

vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

Why would you do a for loop? I dont see any reasonable reson why use it.
Please explain why would you use a for loop?

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

I am making a program that involves a car. The car will have an odometer and then the gallons of gas. Then when a person clicks Drive(button) another box comes up. in there the person inputs the miles to be driven. Then i am supposed to add the miles to be driven inputted to the current miles on the odometer and show it in the TextBox. Then also using the same inputted value i am supposed to calculate the mpg of the car and decrease the gallons of gas.

vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

In order to do that you have several approaches:

1) when the user finishes to input the miles, you can launch an event to indicate the number of miles driven, to be catch by the vehicle.

2) if the input form is launched from the vehicle using a showdialog(), then the input can have a public property milesdriven that can be read from the vehicle just after the showdialog. This property can return the textbox input value.

Hope this helps

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

Could you explain how i could do that. My code is for the InputBox

public partial class InputBox : Form
    {
        public InputBox()
        {
            InitializeComponent();
        }
        private float m_theInput;
        public float InputValue
        {
            get { return m_theInput; }
        }
        private void btnOkay_Click(object sender, EventArgs e)
        {
            m_theInput = float.Parse(txtInput.Text);//This is what the user inputs and i need this
            this.Close();
        }
        private void InputBox_Load(object sender, EventArgs e)
        {

        }
    }
}

This is the code where i want to add the spot

public class Vehicle
    {
        protected float m_mpg;
        protected float m_tankCapacity;

        protected float m_odometer;
        protected float m_gasRemaining;

        public Vehicle()
        {
            m_mpg = 30;
            m_tankCapacity = 16;

            m_odometer = 0;
            m_gasRemaining = m_tankCapacity;
        }
        public Vehicle(float mpg, float tankCapacity)
        {
            m_mpg = mpg;
            m_tankCapacity = tankCapacity;

            m_odometer = 0;
            m_gasRemaining = m_tankCapacity;
        }
        public float Odometer
        {
            get { return m_odometer; }
            set { m_odometer = value; }
        }
        public float GasRemaining
        {
            get { return m_gasRemaining; }
            set { m_gasRemaining = value; }
        }
        public void Drive()
        {
            if (m_odometer >= 0)
                m_odometer += /* This is where i want to add it*/ 
                                                                                                                                                                                 /           float input = m_odometer;
            m_gasRemaining = 12 - input / m_tankCapacity; 

        }
        public void FillGas()
        {
            m_gasRemaining = m_tankCapacity;              
        }
vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

As soon as you have a class directly accessing a specific form you are asking for problems... It is probably safer to use a function that takes a reference to a form instead.

public class Vehicle
    {
        protected float m_mpg;
        protected float m_tankCapacity;

        protected float m_odometer;
        protected float m_gasRemaining;
        
        private float m_grabbedinput; //I added this
        public GetInputFromForm(InputBox myForm) //and this
        {
            m_grabbedinput = myForm.InputValue;
        }
        public void Drive()
        {
            if (m_odometer >= 0)
                m_odometer += m_grabbedinput //and this
        }


This can be implemented as follows:

InputBox myBox = new InputBox();
myBox.Show();

while (myBox.Visible) {} //Until the window is closed block the main program thread - probably a bad idea but this is just for example purposes

Vehicle myVehicle = new Vehicle();
myVehicle.GetInputFromForm(myBox);


This is the most basic way to solve your problem. Learning how to use delegates, events, and safe multithreading would be benefitial to you. Also, instead of blocking your program (like I did) it may be useful for you to add a member to your input box like m_isconfirmed to indicate that the user hit the OK button instead of the X then fire a check on this inside of a timer event.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Skatamatic, your post was confusing and when i tried fitting it in my program wouldn't do what it's supposed to. A little more specification

vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 
Skatamatic, your post was confusing and when i tried fitting it in my program wouldn't do what it's supposed to. A little more specification

This post is much less specific than my post was. What was confusing about it? Post the parts of your code that don't work and I will help you implement it. This includes the actual implementation of your classes.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

It's all ok now, I figured out what i was missing. What i needed was in the wrong method.

vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: