This project is read-only.

Insert tab character in text field

Sep 10, 2010 at 1:58 PM

I am trying to insert a tab character in a text field. Currently I have this contoller:

    public class TabKeyEvent : SourceGrid.Cells.Controllers.ControllerBase
    {
        public override void OnKeyPress(SourceGrid.CellContext sender, KeyPressEventArgs e)
        {
            //base.OnKeyPress(sender, e);

            if (e.KeyChar == '\t')
            {
                sender.Value = ((String)sender.Value) + "\t";
            }

        }
    }

However it does not work yet.

Another problem is that, I can manually paste a tab character in the field, and you can see the tab character in the editing mode. However once the focus is gone (not in editing mode), the tab character hide away. Do Can a custom view object fix this?

Cheers

Sep 11, 2010 at 7:36 AM

Hi lulalala,

I also did check, and looks like (sample 14) to insert a tab character is not that easy. Pressing tab will move focus to another cell.   as you also told, pasting tab character works, but only in edit mode.   I think a good way to start fix this would be create custom view, as you said.  Try experimenting with different views.

 

Here's what i have done in a several minutes check:

Text drawing is handled by TextGDI class (TextGDI.cs), actual drawing is in TextGDI.cs.87

 

There is a StringFormat class used.  Reading documentation http://msdn.microsoft.com/en-us/library/system.drawing.stringformat.aspx we can see that:

"Encapsulates text layout information (such as alignment, orientation and tab stops) display manipulations (such as ellipsis insertion and national digit substitution) and OpenType features"

So StringFormat is in some way responsible for tabs.

Ok, here is a quick workaround for you:

before calling DrawString just set up appropriate tab stops, for example like this:

float[] tabStops = {20.0f, 20.0f, 20.0f};

StringFormat.SetTabStops(0.0f, tabStops);

graphics.Graphics.DrawString(Value, Font, brush, area, StringFormat);

 

 

You will have to calcuate (or hard-code) how big are the tab stops, 20.0f are not big, and they are much smaller tabs than what you see in edit mode. So just choose your own value.

 

Ok, i also hacked a simple tech-proof example, that it works.

Open frmSample14.cs, after the for loop where rows are created, approximately at line 106 add these lines:

            var viewCell = grid1[1,0].View as SourceGrid.Cells.Views.Cell;
            var textGDI = viewCell.ElementText as TextGDI;
            float[] tabStops = {20.0f, 20.0f, 20.0f};
            textGDI.StringFormat.SetTabStops(0.0f, tabStops);

 

Hope the code is self-exaplantory.

This should make it work ;)

Sep 13, 2010 at 3:28 PM

thanks a lot. The tab display works fine :D

Will try to get this insertion work.

Oct 4, 2010 at 1:05 PM

Yes the problem solved. For those of you who want to allow tab insertion in textboxes:

Have a new new SourceGrid.Cells.Editors.TextBox editor handy, and set it as the follows.

oneClickTabEditor.Control.AcceptsReturn = false;
oneClickTabEditor.Control.AcceptsTab = true;
oneClickTabEditor.Control.Multiline = true;

You can insert tabs, however you must also disable newline insertion. Because with multiline you are then able to insert newlines, just that if you do, it can be really confusing on single line textbox.

So I have SpecialKeys = GridSpecialKeys.None | GridSpecialKeys.Enter .... to capture Enter keys.