Jump to content

need urgent assistance

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
elizabethmwashuma

elizabethmwashuma

    Newbie

  • Members
  • PipPip
  • 29 posts
hi everyone, i have a couple of problems am facing and i appreciate you totally for all the replies.

1.if you had a text box in form 1 that a user fills in his name and for security you want the next form, form2, to contain a label that will pick up the text entered in the text box in form 1.how would you do the coding .i tried the following code but did not work.

Form2 tmpForm = new Form2();

            tmpForm.mylabel.Text = this.textBox2.Text;



2.

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
the line you are doing something wrong is Highlighted here in orange color
To get a value from another form


  • On the File menu, point to New and then click Project.
  • In the New Project dialog box, in the Templates pane, click Windows Forms Application, and then click OK.
    A new Windows Forms project opens.
  • From the Toolbox, drag one TextBox and one Button control onto the form.
  • In Solution Explorer, right-click the project, click Add, and then click New Item.
    In the Add New Item dialog box, in the Templates pane, click Windows Form, and then click Add.
    A new Windows Form (Form2) opens.
  • From the Toolbox, drag one TextBox control onto the form.
  • Change the Text property of the TextBox control to Sample text. The first form will read and display this property.
  • Change the Modifiers property of the TextBox control from Private to Public to enable other forms to access this control.
  • In Solution Explorer, right-click Form1.cs, and then click View Designer.
  • In the form, double-click the button to create a Click event handler.
  • Before the button1_Click event handler, add the following code to create an object that enables access to the components and properties of Form2:
private Form2 otherForm = new Form2();
11.In the button1_Click event handler, add the following code to get the value from the text box on Form2 and assign it to the text box on Form1:
textBox1.Text = otherForm.textBox1.Text;

  • Press F5 to run the program.
  • The program starts and the form appears. When you click the button on Form1, the text from the text box on Form2 appears in the text box on Form1


#3
elizabethmwashuma

elizabethmwashuma

    Newbie

  • Members
  • PipPip
  • 29 posts
wow
thank you very much
i have got the concept
i know need to make some few changes to adjust fro the label to be in the second page instead.
thanks a million
brb with another qs wen av succeded
thanks a gain

#4
elizabethmwashuma

elizabethmwashuma

    Newbie

  • Members
  • PipPip
  • 29 posts
ges wat did not work. i tried to interchange to make the label in form2 have the text of the textbox in form1.this code has been stressing it for the last 2months....kindly look into this again....i totally appreciate

#5
elizabethmwashuma

elizabethmwashuma

    Newbie

  • Members
  • PipPip
  • 29 posts
here is the code for the first form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //private Form2 otherForm = new Form2();

        private void button1_Click(object sender, EventArgs e)
        {
            //textBox1.Text = otherForm.textBox1.Text;
            Form2 frm2 = new Form2();
            frm2.Show();
            this.Hide();
        }

        
    }
}


for the 2form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private Form1 otherForm = new Form1();
        label1.Text=otherform.textBox1.Text;

        private void Form2_Load(object sender, EventArgs e)
        {
            //label1.Text = otherform.textBox1.Text;
        }
    }
}


#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
so you were not talking about static text in the textbox.So my method would work only on Labels..etc which have text set during designer time itself.Try the below,
In Form1.cs:
private Form2 otherForm;
private void GetOtherFormTextBox()
{
    textBox1.Text = otherForm.TextBox1.Text;
}

In Form2.cs:
public TextBox TextBox1
{
    get
    {
        return textBox1;
    }
}
}

#7
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
if you still aren't understanding this here is a tip:
Don't expose controls outside of the form to which they belong.

Instead, define public properties that allow you to get at information
on the form, and name the property according to what it returns:

public class Form1
{
private TextBox textbox1;
... etc. ...
 
public string FirstName
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}

}

Edited by gokuajmes, 25 June 2010 - 02:24 AM.
included code tags