Click here to Skip to main content
13,730,590 members
Rate this:
 
Please Sign up or sign in to vote.
I've been trying to do the following in multiple ways but it simply doesn't work.

I have a Form with Textbox and a User Control, from the User Control I need to be able to change the text of the Textbox.



It seems like whatever I do it's sandboxed or something? because when I set the value of a string from another form and get it immediately afterwards within the same function it shows the correct value, whoever when I try to get that value somewhere else a little later its an empty string..

What I have tried:

// At User Control:
Form form = new Form();
form.Textbox.Text = "sample text";

Result = nothing happens.


// At Form:
public string test
{
	set { Textbox.Text = value; }
}

// At User Control:
Form form = new Form();
form.test = "sample text";

Result = nothing happens.


// At Form:
public string test1;

public test2
{
	Textbox.Text = test1;
}

// At User Control:
Form form = new Form();
form.test1 = "sample text";
form.test2();

Result = nothing happens.
Posted 12-Jun-18 9:58am
Updated 14-Jun-18 8:23am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You are just creating instances of the Form class. You actually need to show the Form to see the changes.
  Permalink  
Comments
12-Jun-18 16:10pm
   
Although that works its not the solution, I don't want to show to form because its already shown and it's the main form, the User Control is docked inside the form.
‭011111100010‬ 12-Jun-18 16:13pm
   
Then you need to get a handle on that form, not start a new instance of the class. So, wherever you first open the form, store it in a variable and use that.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The user control should have no knowledge of its parent. Instead, it should raise an event when it wants to change the text. The parent form can then subscribe to that event, and update the textbox as required.

Event args:
A utility EventArgs class to pass any value via an event. You only need to declare it once. Also, a factory method to simplify creating it.
public class DataEventArgs<T> : EventArgs
{
    public DataEventArgs(T value)
    {
        Value = value;
    }
    
    public T Value { get; }
}

public static class DataEventArgs
{
    public static DataEventArgs<T> Create<T>(T value)
    {
        return new DataEventArgs<T>(value);
    }
}
User control:
Add a TextChanged (or whatever name you want) event, and raise it when you want to change the parent form's text.
[field:NonSerialized]
public event EventHandler<DataEventArgs<string>> TextChanged;

private void OnTextChanged(string newText)
{
    var handler = TextChanged;
    if (handler != null)
    {
        handler(this, DataEventArgs.Create(newText));
    }
}

...

// Raise the event to change the text:
OnTextChanged("sample text");
Form:
Open the designer, click on the user control, go to the "events" tab on the properties window, and double-click on the "TextChanged" event.
protected void UserControl1_TextChanged(object sender, DataEventArgs<string> e)
{
    Textbox.Text = e.Value;
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I was able to fix this myself, and i coudn't find the answer anywhere on the internet, so for anybody else having the same frustration, here's what i did:

* I opened the "Form.Designer.cs"
* I searched for the actual Textbox, and replaced;

private System.Windows.Forms.TextBox ExampleTextbox;
TO:
public static System.Windows.Forms.TextBox ExampleTextbox;


Then, in that same file, remove "this." in front of every value anywhere you see it:
this.ExampleTextbox.ExampleProperty = Example;
TO:
ExampleTextbox.ExampleProperty = Example;


Then, i could just call the textbox from any form or user control i wanted to, like this:

Form.Textbox.Text = "Sample text."


The only downside to this is, is that if you ever change any property of that form via the Visual Studio property editor (so not via programming), it all changes back and you'll have to redo it.


So if theres any better way of doing this i'd like to see it!
  Permalink  
v3
Comments
Richard Deeming 14-Jun-18 14:13pm
   
A really bad idea. If you ever create more than one instance of your form, they'll all end up trying to share the same TextBox instance.
14-Jun-18 14:14pm
   
Could you show me how this is done in a professional way?
Richard Deeming 14-Jun-18 14:23pm
   
See solution 3. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.180920.1 | Last Updated 14 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100