Hello,
I have a Win form application in which I get a key from a button and print the key name or text to a richTextBox.
When printing from the form class it prints OK.
When sending the key to another class and printing from there, it does not print.
The debugger shows that the data is passed OK, and the printing command has the data as it should, but it still does not print.
I suspect that the initialization of the text box clears the box immediately after printing, but I am (still) unable to fix the problem.
I know I do something wrong, but what???
Please help.
Attached is a minimal running snippet that shows it all.
Sam
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespaceWindowsFormsApplication2
{
publicpartialclassForm1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(object sender, EventArgs e)
{
}
privatevoidKey_Click(object sender, EventArgs e)
{
OtherClass other = new OtherClass();
Button currentkey = (Button)sender;
string KeyText = currentkey.Text;
//Display(KeyText); //Prints OK from here.
other.GetTheKey(KeyText); // send the key number to OtherClass.
}
publicvoidDisplay(string text) //Will print OK with call from this class.
{ // will not print from another class call.
richTextBox1.AppendText(text);
}
}
classOtherClass
{
publicOtherClass()
{
}
publicvoidGetTheKey(string KeyText) //Called by the Form1 to transfer eack keystroke.
{
Form1 form1 = new Form1();
form1.Display(KeyText);
}
}
}
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Nir Bar107 reputationPosted Sep 23, 2009, 12:28 AM EDT
You should pass the original form to your OtherClass, and call Display on that
class OtherClass { public OtherClass() { } public void GetTheKey(Form1 form1, string KeyText) //Called by the Form1 to transfer eack keystroke. { form1.Display(KeyText); } } }Please mark this as answer if it helps you
Nir