Is it possible to hijack text box functionality for custom control

T

trant

I am working on a custom control, an extension of Panel which draws a series
of rectangles in a flow chart. Each rectangle has a title and the ideal
approach would be to allow the user to edit the title by simply clicking it.

Obviously I could hook up a rename dialog to a context menu which would be
easy but it is a bit more tedious to the users so I am hoping I can
accomplish this the ideal way.

Textbox functionality does not look simple though so I'd hate to recreate it
for my control. Yeah handling the keyboard events to show what the user types
is not hard but I am not sure how the flashing cursor would play out (not
talking about the mouse cursor, but the blinking cursor which indicates you
are editing text)

So one thing I was hoping I can do is somehow hijack the textbox's
functionality, like creating a textbox which would be superimposed on the
label which would be editted. Can I do that? I have been trying a few things
but so far no dice.

Even just a broad description of how to do this would be grateful
 
J

Jeff Gaines

So one thing I was hoping I can do is somehow hijack the textbox's
functionality, like creating a textbox which would be superimposed on the
label which would be editted. Can I do that? I have been trying a few
things
but so far no dice.

Even just a broad description of how to do this would be grateful

I saw an in place editable List View that did something similar.

What it did was trap a double click, put a Text Box in place over the
existing text (sizing as appropriate) and copy the text into itself. The
Text Box itself was then disposed or hidden on picking up the Enter Key or
when it lost focus. It then copied its text back into the List View. You
could also trap the Escape key to dispose/hide the Text Box and discard
its contents.
 
J

Jeff Johnson

What it did was trap a double click, put a Text Box in place over the
existing text (sizing as appropriate) and copy the text into itself.

What Jeff is describing is pretty much the most common way of doing this
without rolling your own text box. You can also turn off the border so that
it looks like you're typing directly into the rectangle.
 
P

Peter Duniho

Jeff said:
I saw an in place editable List View that did something similar.

What it did was trap a double click, put a Text Box in place over the
existing text (sizing as appropriate) and copy the text into itself. The
Text Box itself was then disposed or hidden on picking up the Enter Key
or when it lost focus. It then copied its text back into the List View.
You could also trap the Escape key to dispose/hide the Text Box and
discard its contents.

For what it's worth, I use this technique myself, and it works fine. In
one example in my own case, it's an always-existing Label in a Form
edited using a TextBox, so I just have a dedicated TextBox aligned just
right with the Label. I show/hide the Label/TextBox as appropriate to
the user's actions, and handle Enter and Escape keys as Jeff suggests.

For this particular scenario, where the text to be edited can be
positioned arbitrarily, and there can be arbitrarily many of them, it
may make more sense to have a single TextBox that is never disposed,
positioned and shown when editing needs to happen, and then hidden again
when the user's done.

Pete
 
T

trant

Thank you gentlemen for the advice!

Here is the code I wound up doing to accomplish the in place editting:

TextBox editbox;

// during form init
editbox = new TextBox();
editbox.Visible = false;
editbox.KeyUp += new KeyEventHandler(editbox_KeyUp);
this.Controls.Add(editbox);

//my rectangle are encapsulated into objects called Node, I keep ref to one
currently being editted:
Node editnode;

// methods to handle begin/end of editting
void BeginEdit(Node node)
{
editnode = node;
editbox.Visible = true;
editbox.Bounds = editnode.Area;
editbox.Text = editnode.Title;
editbox.Focus();
editbox.SelectAll();
}

void EndEdit(bool save)
{
editbox.Visible = false;
if (save)
{
editnode.Title = editbox.Text;
editnode.ReDraw();
this.Invalidate();
}
editnode = null;
editbox.Text = "";
}

// key event handle to capture Enter/Esc
void editbox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
EndEdit(true);
}
else if (e.KeyCode == Keys.Escape)
{
EndEdit(false);
}
}

then I also call BeginEdit on MouseUp event after calling a EndEdit if the
editbox was already visible
 

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