7 Replies - 4210 Views - Last Post: 04 March 2011 - 10:56 AM Rate Topic: -----

#1 Hishman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 11

Trying to pass value from textbox string to label..

Posted 28 February 2011 - 04:43 PM

Hello, I know this question is asked a lot but I am feeling really lost. Mostly because when I run my program I'm not getting any errors so I can't identify whats stopping it from working properly.

Anyway, what i'm trying to do here is have the text a person enters in a textbox on Form2, to display in a label in Form6.

Heres my code for form 2:

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;

namespace ToL
{
    public partial class Form2 : Form
    {
        public static string HeroName;
        public Form2()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {
            string Heroname = textBox1.Text;
        }

        public void button1_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            this.Hide();
            form3.Show();
        }
    }
}



Code for form 6:

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;

namespace ToL
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }
       
        public void label1_Click_1(object sender, EventArgs e)
        {   
            Form2.HeroName = label1.Text;
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {
                       
        }
    }
}


Am I way off? I read several tutorials and im trying to work with this method. Any help would be greatly appreciated.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to pass value from textbox string to label..

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Trying to pass value from textbox string to label..

Posted 28 February 2011 - 04:50 PM

Well, I've got just the thing for you:

Passing values between forms

It's a tutorial I wrote since this topic comes up so often.

The major thing to realize is that Form2 and Form6 don't actually exist. An instance of Form2 and an instance Form 6 do. So you can't just do Form6.label1, any more than you could do int[].Length when you were trying to get a specific array's length.

Also, for the love of god, use descriptive names instead of "Form1", "Label1". There's a reason you can rename these things!
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,061
  • Joined: 02-June 10

Re: Trying to pass value from textbox string to label..

Posted 28 February 2011 - 04:53 PM

Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms and classes
Was This Post Helpful? 1
  • +
  • -

#4 Hishman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 11

Re: Trying to pass value from textbox string to label..

Posted 28 February 2011 - 08:05 PM

Ok, first of all thank you for the tutorials, they are a great help. I think its starting to make sense but I have a question about this:

"The major thing to realize is that Form2 and Form6 don't actually exist. An instance of Form2 and an instance Form 6 do. So you can't just do Form6.label1, any more than you could do int[].Length when you were trying to get a specific array's length."

So then how would I pass the data from where the player enters their name in a textbox (Form2), to the form where the name is displayed in a label (Form6), without opening Form2? Sorry if that sounds confusing, I don't even know if I'm asking the right question. I simply want the data from the textbox in Form2 to display in the label in Form6 without the player having to click it and without having to open Form2, is this possible?

I fiddled with my code a bit and heres what I have now:

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;

namespace ToL
{
    public partial class Form2 : Form
    {
        public Form2(string HeroName)
        {
            InitializeComponent();
            textBox1.Text = HeroName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            Form6 frm = new Form6();
            frm.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}



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;

namespace ToL
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }
       
        public void label1_Click_1(object sender, EventArgs e)
        {
            Form2 frm = new Form2(textBox1.Text);
            frm.Show();
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {
                       
        }
    }
}



Does this clarify what I'm asking and am I on the right track now?

Thank you!

This post has been edited by Hishman: 28 February 2011 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 Hishman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 11

Re: Trying to pass value from textbox string to label..

Posted 28 February 2011 - 09:39 PM

I solved it. In case anyone else wants to know I had to use delegates in order to pass the data to the label. Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#6 Hishman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 11

Re: Trying to pass value from textbox string to label..

Posted 28 February 2011 - 11:13 PM

Ok so I didn't quite solve it but I am making progress. Here's my code right now:

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;

namespace ToL
{
    public partial class Form2 : Form
    {

        public static string HeroName;
        public delegate void delPassData(TextBox text); 
        public Form2()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {        
            
            Form6 frm = new Form6();
            delPassData del = new delPassData(frm.funData);
            del(this.textBox1);
            Form4 form4 = new Form4();
            this.Close();
            form4.Show();           
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }
    }
}



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;

namespace RpgTutorial
{

    public partial class Form6 : Form
    {
        
        public void funData(TextBox txtForm1)
        {
            label1.Text = txtForm1.Text;
        }
        public Form6()
        {           
            InitializeComponent();
        }
       
        public void label1_Click_1(object sender, EventArgs e)
        {

        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }
    }
}


Technically it works but not the way I want it to. As you can see I'm going from Form2 where the player enters their name, directly to form 4. When I do this the label in Form6 does not return any value, it only works when I have Form6 set to Show() in the button handler of Form2.

So here's my question, is there some way I can store the textbox data from Form2 so that when I finally do open Form6 the label will show the value?
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Trying to pass value from textbox string to label..

Posted 01 March 2011 - 12:14 AM

Ok, one problem is clear to me. It seems that you're "daisy chaining" these forms together. That's not good logic. You need one set of controlling logic that handles the opening of each form, when the others close if necessary (forms expose a Closed event that you can use). This can just be the main form; the first form. Even if you don't want it shown all the time, you can use .Hide() to make it invisible.

That way, instead of using a delegate, you can use events on the form to catch changes. Store the values, and pass them (through the constructor) to other forms.

Look at the tutorial I linked to show you how to use events to get values from forms.
Was This Post Helpful? 1
  • +
  • -

#8 Denmage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 08-April 09

Re: Trying to pass value from textbox string to label..

Posted 04 March 2011 - 10:56 AM

I agree with what insertAlias said about having a controlling form, it makes your life a lot easier. I may not be the best at C# yet, but I have been having great success with treating each form as nothing more than another class. If I want to pass values between the controlling form and the children, I create properties and just pass the values through the properties of the children. So far, it hasn't failed me yet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1