473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Identifying Control with Focus and AppendText

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,


--
Fritz
Nov 21 '06 #1
3 3039
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;

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 22 '06 #2
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

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
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;

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 22 '06 #3
On Wed, 22 Nov 2006 17:32:55 +0100, Fritz Switzer
<fr***********@abletfactory.comwrote:
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 :)

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Greg F. | last post by:
Dear Folks: Question #1: I have not been able to find a method to acknowledge or indicate that a form is open (not necessarily have the focus). Any ideas on how to check if a particular form...
4
by: Armando | last post by:
Hey again - I have a bunch of similar controls on a form (A2000), and I'm calling the same code (with an argument as to what was clicked) when any one of them is clicked. I'd like to generalize...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
5
by: User N | last post by:
I have a log class with static methods that grab and release a mutex as required. The log class is designed to be called from both GUI and thread pool threads, and dump output to a logfile and a...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers for...
7
by: ChrisUttenreither | last post by:
Hello folks. I'm writing a Roguelike game in C#. Fun stuff. The game includes a map that is made entirely of colored ASCII art in a monospace font. I would like to make this map a control in...
4
by: Jon Slaughter | last post by:
I've created some custom controls and forms that allow the feature to temporarily transfer focus to a control that has been entered by the mouse. Everything seems to work fine but the problem I...
2
by: John | last post by:
Hi How can I add some txt in red and other in grey to a rich text control? Thanks Regards
2
by: jontcaz | last post by:
Good Day, I have an application that will send text out of a serial port if i hold down the "1" key for 2 seconds. I am currently just turning a chime on and off but want to expand this to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.