1
public partial class ThanglishToTamilGUI : Form { public string anz; public ThanglishToTamilGUI() { InitializeComponent(); } public void btnConvertToBraille_Click(object sender, EventArgs e) { anz = richTextBoxTamil.Text.ToString(); GUI.TamilToBrailleGUI c1 = new GUI.TamilToBrailleGUI(); c1.Visible = true; } } 

I need to pass my richtextbox (richTextBoxTamil) content to variable call anz.

I am retrriving anz variable in other form as form load event:

 private void TamilToBrailleGUI_Load(object sender, EventArgs e) { ThanglishToTamilGUI tt = new ThanglishToTamilGUI(); String apper = tt.anz; richTextBoxTamil.Text = apper; } 

My Problem: I am getting null values as result. Since if I assigned any values that invoked correctly.

public partial class ThanglishToTamilGUI : Form { public string anz = "Hai"; public ThanglishToTamilGUI() { InitializeComponent(); } ... 

Here my ans value is passed as "Hai". But my requirement is to get what ever the content in the richTextBoxTamil and pass it to that public variable call anz. What went wrong here please help me.

Thank you.

2 Answers 2

5

This is the problem:

ThanglishToTamilGUI tt = new ThanglishToTamilGUI(); String apper = tt.anz; 

How do you expect apper to ever be anything other than null? You're fetching the variable from a freshly-created form, which has never been shown, and which has never had btnConvertToBraille_Click called on it.

Presumably there's an existing ThanglishToTamilGUI object somewhere, and that's the one you want to fetch the variable from. Basically, one form needs to know about the instance of the other form.

(I'd also strongly suggest using a property rather than a public variable, but that's a different matter. You might not even need to have a separate variable at all - just declare a property which fetches richTextBoxTamil.Text.)

Alternatively, just pass the relevant string to the constructor of the new form:

public void btnConvertToBraille_Click(object sender, EventArgs e) { GUI.TamilToBrailleGUI c1 = new GUI.TamilToBrailleGUI(richTextBoxTamil.Text); c1.Visible = true; } 

Then the new form doesn't need to know about the old form at all - it only needs to know the text to display.

(You might want to pull it out of the constructor and into a settable property, but it's the same basically principle: the code creating the form pushes the data, rather than the new form pulling it.)

Sign up to request clarification or add additional context in comments.

2 Comments

@SutharshanSuthan: Yes, you need to create the appropriate constructor to call.
Yes exactly what i did. Thanks once again. :)
0

You can create a public property to access the current Text value of the textbox.

public string RichTextBoxText { get { return richTextBoxTamil.Text; } } 

The way you do it now the form is instantiated, but the click event is not fired. So there's no way you will get anything other than what you initialized the field to.

Load is not the place to look for user input. An event (like click) is where you need to check the property value:

private void SomeClick(object sender, EventArgs e) { String result = thanglishToTamilGUIObject.RichTextBoxText; //do something with text } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.