Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Acessing text value of a textbox from another form??? please

Status
Not open for further replies.

rahmanjan

Programmer
Apr 13, 2003
180
AU
hi all,

I have a main from with all the details if any of the detail is not available I have kept a button close to the field so the user clicks on it and another forms pop in. So what i want is if the user adds a new entry or updates an entry the textbox in the main form should be updated. Simple as that.

I doesn't work for me without any reason.

In my main form i keep a GLobal reference to my main form as :

static void Main()
{
parentForm=new frmCDs(); //public
Application.Run(new frmCDs());
}

then in my pop up form i do as follow:
//txtCategory is public
frmCDs.parentForm.txtCategory.Text=MyNewValue;

even I tryed something like:
//In my pop up form

frmCDs.parentForm.ChangeText();

//in main form
public void ChangeText()
{
this.txtCategory.Text="changed";
}

But it doesn't work. any suggestion?

regards

 
Gosh - I went through this too.
The detail is really too long to paste into this post - it involves events and delegates.

Make your popup forms classes which your main form instanciates but only shows when necessary. Set up delegates and events to mirror changes in the called form in the main form.

I know this is not specific to your problem, and the first time you try it may be very difficult, but after you work thru the first few it does become easier.

And it works.
 
Ok, so you're trying to load up a popup that returns a value to the main form?

Try this:

//Main form, event that causes popup to appear
Code:
{
   frmCDs popup = new frmCDs();
   popup.ShowDialog();
   txtCategory.Text = popup.txtBox.Text
   ...
}

Ben

 
Ok, I think I get the general idea, but let me recap what I think you're trying to do just in case I write something totally bogus.

You are making a windows forms application. In this application there is a primary window with a bunch of information on it. It shouldn't matter if it is in text boxes or labels or what not. There are also n other forms. Editing the data in these forms should change the data in the primary form.

Ok, thinking outside the box, I have to ask, "Where is all this data being stored?" Is this going into some database or xml file or cookie or something? If there is some type of data store, the easiest solution might be to simply

change the data store using the secondary forms. Then, using something like an OnFocus event (for the primary form), an OnClose event (for the secondary forms), or your own delegate, you could simply refresh the main form. Your "global variables" would be in the data store.

However, let's say that you can't do this for some reason or another (like it's just not cool enough). You could use a delegate. If you are not familiar with delegates, I can help you out with an example or at least a link to a good tutorial. I've been working with them a lot lately since I'm working with sockets.

Hope this helps
 
If this is helpful to anyone you could:
origForm = Original forms class name
newForm = Form being openined

//in origForm

loadForm newForm = new loadForm(this);
newForm.ShowDialog(this);

//In newForm

private origForm origFormInstance;
public newForm(origForm p)
{
origFormInstance = p;
InitializeComponent();
}

public newForm()
{
InitializeComponent();
}

Then you can access origForm from the new form using origFormInstance.[method/class/variable/etc]
 
My solution was to encapsulate the info I needed in a reference type that could be passed to the called form's constructor:
Code:
// new class:
public class StringBucket {
  public string Text;
}
In Form2:
Code:
private StringBucket _bucket;

public Form2(StringBucket bucket) { 
  // modified/overloaded constructor
  _bucket = bucket;
  
  // other stuff...
}
In Form2's button click method:
Code:
  _bucket.Text = textBox2.Text;
In Form1's button click method:
Code:
  StringBucket bucket = new StringBucket();
  Form2 f2 = new Form2(bucket);
  f2.ShowDialog(this);

  // bucket should now contain new value
  textBox1.Text = bucket.Text;
Does this make any sense?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top