I don’t suggest using SetText
because it will be not compitable with other programs else.
Suggested Solutions:
TextBox
Property:
Add in your user control TextBox
Property and then use it.
Code sample: In UserControl.cs
public TextBox TargetTextBox { get; set; }
public void SetText(string text)
{
TargetTextBox.Text = text;
}
And use new method that are avaliable in UserControl
Event:
Add event in the user control, and if this event fired in Form1
, use SetText
in code there.
Sample Code:
Create file called TextEventArgs
and put this code:
public class TextEventArgs : EventArgs
{
public TextEventArgs(string text)
{
Text = text;
}
public string Text { get; set; }
}
Use this code in user control
public delegate void ShowTextEventHandler(object sender, TextEventArgs e);
public event ShowTextEventHandler ShowText;
// Use this method, and this method will fire 'ShowText' event
protected override OnShowText(TextEventArgs args)
{
TextEventHandler handler = ShowText;
handler?.Invoke(this, args);
}
Variable (Not suggested):
Go in Program.cs
file and put replace it with this code:
using System.Windows.Forms;
public class Program
{
internal static readonly MainForm;
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompitableTextRenderer(false);
MainForm = new Form1();
Application.Run(MainForm);
}
}
And then you can use Program.MainForm.SetText("example")
.
CLICK HERE to find out more related problems solutions.