0

I have form1 and class1. I would like to use a method to read/write the text in a textbox in form1 at class1. This is the segment of the program. I currently able to interact between those two. The only problem is I do not how to convert the string to a form which can be read.

In form1:

public string readTxt(string txtbox_name) 
{
string txtbox_val;
txtbox_val = (txtbox_name).Text; //problem here
return txtbox_val;
} 

public void writeTxt(string txtbox_name,string txtbox_val)
{
    (txtbox_name).Text = txtbox_val; //problem here
} 

For example i need to read/write value in textbox1.

In class1:

string text1 = (readTxt(textbox1)); //for get the text in textbox1
string text2 = "Hello"
writeTxt(textbox1,text2); //for overwrite the text in textbox1 to "Hello"

Sorry for the question quite confusing due to not good in ask question.

Royce Ang
  • 3
  • 3
  • Can you explain the use case of what exactly you are trying to achieve? – Chetan Oct 31 '19 at 05:55
  • 1
    Use events. c# is event driven language. You dont need any method to get text on the text box, in your class just write txtbox_name.Text. It will return the text on your text box. you can assign text in to variable to like this var text= txtbox_name.Text. if you need to log in console when user is on typing just use keyup event in text box. – Eranga Gamagedara Oct 31 '19 at 05:56
  • 2
    TextBox and Console. What's that for a funny app? – gsharp Oct 31 '19 at 05:57

2 Answers2

3

For winforms, you could use Control.ControlCollection.Find(String, Boolean)

Searches for controls by their Name property and builds an array of all the controls that match.

and Enumerable.OfType(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

public string ReadText(string txtbox_name) 
{
    // insert false if you expect not to be in a sub control
    return Controls.Find(txtbox_name,true) 
                  .OfType<TextBox>()
                  .FirstOrDefault()?.Text
}

However i fear your problem is actually, how can i get text from my TextBox.

var text = MyControl.Text;

Update

How about if i would like to write into Textbox.

var textBox = Controls.Find(txtbox_name,true) 
                      .OfType<TextBox>()
                      .FirstOrDefault();

if(textBox != null)
textBox.Text = "yehaa";

Clarification

The difference between calling Find, and the other approaches, is that if your Textbox is buried in a Panel or other SubControls the other approaches wont find it. Other than that, they are exactly the same. Find works on the second parameter flag, and I'd an option to search subcontrols

searchAllChildren

true to search all child controls; otherwise, false.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
3

You could Search for all TextBox with the name and get the Text using the Controls Property. For Example,

public string ReadText(string txtbox_name)
{
   return Controls.OfType<TextBox>().Single(x => x.Equals(txtbox_name)).Text;
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51