Identifying Control with Focus and AppendText

F

Fritz Switzer

I'm trying to AppendText to a TextEdit. I have a procedure that will
go through the controls on a Windows form and return the control that has
"focus".
I have a number of TextEdits on my form and don't know which control
has focus.

I then want to paste text from the ClipBoard to this TextEdit control.

The following code is what I'm using. Everything works fine, it identifies
the correct control,
however, in a previous version I had this narly switch/case process and
would AppendText to
hard coded TextBox1.AppendText for example. Now that I just get back the
Control.Name,
I can figure out how to AppendText when I get back the Control object.


object o;

o = Clipboard.GetDataObject().GetData("Text");

if (o != null)

{

if (o.GetType().ToString() == "System.String")
TextEdit1.AppendText((string)o);

}


How do I modify the above code to use the returned Control Name instead of
the actual "TextEdit1.AppendText" ?

TIA,
 
M

Morten Wennevik

Hi Fritz

Assuming you mean a TextBox when you talk about TextEdit, and you have a
reference to a Control, you can easily cast Control to TextBox (if the
object being referenced in Control is actually a TextBox)

// c is the focused Control
string text = Clipboard.GetDataObject().GetData("Text") as string;

TextBox tb = c as TextBox;
if (text != null && tb != null)
tb.AppendText(text);

The 'as' keyword will cast the object to another type if possible. If
not, the result will be null.
Another method is using the 'is' keyword

object o = ClipBoard....
string text = null;

if(o is string)
text = o.ToString();

Now, you don't really need a TextBox to append text to a Control as
Control has a Text property. The code below would do the same as the above

string text = Clipboard.GetDataObject().GetData("Text") as string;

if (text != null)
c.Text += text;
 
F

Fritz Switzer

Morten,

Yes, I have a custom control extended from a textBox.

Your solution worked.

Thank you for the response and explanation and your contributions to the C#
community. It was greatly appreciated.



--
Fritz



Hi Fritz

Assuming you mean a TextBox when you talk about TextEdit, and you have a
reference to a Control, you can easily cast Control to TextBox (if the
object being referenced in Control is actually a TextBox)

// c is the focused Control
string text = Clipboard.GetDataObject().GetData("Text") as string;

TextBox tb = c as TextBox;
if (text != null && tb != null)
tb.AppendText(text);

The 'as' keyword will cast the object to another type if possible. If
not, the result will be null.
Another method is using the 'is' keyword

object o = ClipBoard....
string text = null;

if(o is string)
text = o.ToString();

Now, you don't really need a TextBox to append text to a Control as
Control has a Text property. The code below would do the same as the above

string text = Clipboard.GetDataObject().GetData("Text") as string;

if (text != null)
c.Text += text;
 
M

Morten Wennevik

Morten,

Yes, I have a custom control extended from a textBox.

Your solution worked.

Thank you for the response and explanation and your contributions to the
C#
community. It was greatly appreciated.

Happy to help :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top