Changing the tabPage Caption

D

Dom

In Excel, I can double click on the tab name and then change the tab
name. Can I do this with a tabControl in csharp? That is, can I set
things so that if a user double clicks the tab, he / she can type a
new caption?
 
F

Finn Stampe Mikkelsen

Dom said:
In Excel, I can double click on the tab name and then change the tab
name. Can I do this with a tabControl in csharp? That is, can I set
things so that if a user double clicks the tab, he / she can type a
new caption?

Hi Dom

The actual control does, as Peter correctly states, not support that
action...

But if you use the MouseDoubleClick event of the tabControl, you can catch
what tab the user clicked on and via your own interface you can let the user
type in an alternative text. This text you simply put into the text proeprty
of the tabControl.TabPages[tabControl.SelectedIndex].Text and you are good
to go...

Not sure if that is quite what you where looking for, but it is a way to
realize your intentions...

/Finn
 
D

Dom

Sorry, I should be more clear: even in the Designer, you can't change
the name by editing within the tab itself.

At run-time, you can simulate editing within the tab itself using the
technique I mentioned above.

Hi Guys.

Yes, I meant at run time. I was trying Peter's approach -- placing a
text box over the tab, and letting the user type a caption. I quit
that approach because I thought there might be something simpler, like
"AllowCaptionEditing". But apparantly not.

So back to square 1, and some other questions. "...putting an textbox
over the area where the tab is ..." How do I find that area?
TabPages[0].ClientRectangle is not that area, it seems to be the body
just below the tabs. Also, there is no obvious way to get the "Left"
property of the selected tab. Once I get this rectangle, or compute
it from other properties, I think I can do the rest.

Any help?

Dom
 
J

Jeff Johnson

In Excel, I can double click on the tab name and then change the tab
name. Can I do this with a tabControl in csharp? That is, can I set
things so that if a user double clicks the tab, he / she can type a
new caption?

I understand what you're trying to do, but I recommend you go a different
route for a couple of reasons:

1) I think you're going to end up beating your against a wall trying to do
this. If this were a custom control and you were aware of the area where the
tab is being drawn it would be much simpler.

2) This is very non-standard behavior. People expect to be able to do this
with Excel workbook tabs. They do NOT expect to be able to do this with a
regular tab control.

I recommend having a Rename Tab function (which could be in a context menu
on the tab strip) and then just popping up a simple, plain-jane dialog box
to take user input. Cool? No. Flashy? No. Consistent with the Windows user
experience? Yes.
 
D

Dom

Dom said:
[...]
So back to square 1, and some other questions.  "...putting an textbox
over the area where the tab is ..."  How do I find that area? [...]

Unfortunately, I know of only two ways to do that.  One is unreliable
and easy.  The other is reliable and more difficult.

Easy: just line the controls up in the Designer and hope for the best.
Even resolution/font-size changes can mess that up, and of course if the
control itself changes in the future, the alignment may no longer work.

More difficult: implement your own tab control.  The fact is, it's not
actually _that_ complicated a control, but custom controls are not
everyone's "cup of tea", and the tab control certainly involves some
extra work as compared to less dynamic controls.  But if you implement
your own tab control, then obviously you can include whatever features
you want, including editable tab names.

And then there's a third, intermediate approach that occurs to me.  I'm
not actually sure exactly how one would go about it, but if you can find
a way to intercept the drawing of the tab caption at run-time, then you
can inspect the location of the drawing and use that to position an edit
control at the times you need to.

Personally, I would just display a dialog to let the user change the
name.  The in-place effect in Excel is cool, but it's not really adding
much utility to the UI.  If you were already having to re-implement the
tab control yourself for other reasons, then it'd probably be a fun
feature to include.  But to re-implement the control _just_ for that
feature seems like serious overkill to me.  :)

Pete




Any help?
Dom- Hide quoted text -

- Show quoted text -

Good advice. Points noted and taken. Thanks a bunch.

Dom
 
K

kndg

Sorry, I should be more clear: even in the Designer, you can't change
the name by editing within the tab itself.

At run-time, you can simulate editing within the tab itself using the
technique I mentioned above.

Hi Guys.

Yes, I meant at run time. I was trying Peter's approach -- placing a
text box over the tab, and letting the user type a caption. I quit
that approach because I thought there might be something simpler, like
"AllowCaptionEditing". But apparantly not.

So back to square 1, and some other questions. "...putting an textbox
over the area where the tab is ..." How do I find that area?
TabPages[0].ClientRectangle is not that area, it seems to be the body
just below the tabs. Also, there is no obvious way to get the "Left"
property of the selected tab. Once I get this rectangle, or compute
it from other properties, I think I can do the rest.

Any help?

Dom

Hi Dom,

I agree with the others but I found that simulating an in-place tab
caption renaming is not very hard to do. Here is the code (I assume you
already have a TabControl and TextBox object - named tabControl1 and
tbxTabCaptionPlaceHolder respectively for below example),

public Form1()
{
InitializeComponent();

tbxTabCaptionPlaceHolder.BorderStyle = BorderStyle.None;
tbxTabCaptionPlaceHolder.Font = tabControl1.Font;
tbxTabCaptionPlaceHolder.Visible = false;
tbxTabCaptionPlaceHolder.Leave += tbxRenameTab_Leave;

tabControl1.MouseDoubleClick += tabControl1_MouseDoubleClick;
}

private void tbxTabCaptionPlaceHolder_Leave(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Text =
tbxTabCaptionPlaceHolder.Text;
tbxTabCaptionPlaceHolder.Hide();
}

private void tabControl1_MouseDoubleClick(object sender,
MouseEventArgs e)
{
Rectangle rect = tabControl1.GetTabRect(tabControl1.SelectedIndex);
tbxTabCaptionPlaceHolder.Location = new
Point(tabControl1.Location.X + rect.X + 2, tabControl1.Location.Y +
rect.Y + 2);
tbxTabCaptionPlaceHolder.Size = new Size(rect.Size.Width - 2,
rect.Height + 2);
tbxTabCaptionPlaceHolder.Text =
tabControl1.TabPages[tabControl1.SelectedIndex].Text;
tbxTabCaptionPlaceHolder.Show();
tbxTabCaptionPlaceHolder.BringToFront();
tbxTabCaptionPlaceHolder.Focus();
tbxTabCaptionPlaceHolder.SelectAll();
}

Regards.
 
D

Dom

should be

tbxTabCaptionPlaceHolder.Leave += tbxTabCaptionPlaceHolder_Leave;

Thanks a bunch, kndg. I never knew about GetTabRect. That's exactly
what I wanted. I kept looking at the methods in the TabPage, not the
TabControl. But this looks like it will work.
 

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