Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 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:

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

Result = nothing happens.


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

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

Result = nothing happens.


C#
// 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
Updated 14-Jun-18 8:23am

You are just creating instances of the Form class. You actually need to show the Form to see the changes.
 
Share this answer
 
Comments
Member 10731098 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.
ZurdoDev 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.
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.
C#
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.
C#
[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.
C#
protected void UserControl1_TextChanged(object sender, DataEventArgs<string> e)
{
    Textbox.Text = e.Value;
}
 
Share this answer
 
v2
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!
 
Share this answer
 
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.
Member 10731098 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900