Question

How can I select a specific cell of grid when form show?

Asked by: mikezang

I am making a tool looks like http://www.geocities.jp/mikezang/games/pics/c3ccc.jpg, I want to the cursor and focus is on the specific cell of grid when form create and show, and only this cell can be input in that grid, what can I do?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-03-26 at 10:39:08ID21365589
Tags

cell

,

select

Topic

Delphi Programming

Participating Experts
4
Points
50
Comments
32

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Seting focus to a grid column
    I need to set focus to a grid column if the part number entered begins with 00-000. Here is what I have if inventory.partnumber=left("00-000",6) thisform.grid1.column2.text1.setfocus else thisform.text1.setfocus endif The grid cell gets the focus but you have...
  2. Set Focus to a Particular Cell of a Data Grid
    I used the Data Form Wizard to create a Master/Detail object. The Detail is displayed by a Data Grid Control defined as follows: Set grdDataGrid.DataSource = adoPrimaryRS _("ChildCMD").UnderlyingValue The result of this works perfectly; however, I would like ...
  3. Grid
    how can type datas in the grids?
  4. Displaying a color in a cell in a grid
    I have a client that would like to see a different color displayed in either a cell or the entire row despending on the contents of a field holding the type of shipping (red for RED blue for Blue, etc). I need the color to come up when the grid displays - not just when the ce...
  5. Grid control Active Cell
    If there a was to determine and the exact CELL in which the cursor is resting on a grid control and if so can it be used on a read only grid?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: calinutzPosted on 2005-03-26 at 12:39:14ID: 13637129

Why does it have to be a Grid? Why not use Edits like this here:
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 260
  Height = 640
  Caption = 'Form1'
  Color = 14408667
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object GroupBox1: TGroupBox
    Left = 3
    Top = 254
    Width = 247
    Height = 95
    Caption = 'Commerce per turn'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    object Edit1: TEdit
      Left = 2
      Top = 13
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = 729840
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 0
      Text = '0%'
    end
    object Edit2: TEdit
      Left = 2
      Top = 33
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 1
      Text = 'Produce'
    end
    object Edit3: TEdit
      Left = 2
      Top = 53
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = 729840
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 2
      Text = 'Corruption'
    end
    object Edit4: TEdit
      Left = 2
      Top = 73
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 3
      Text = 'Rest'
    end
    object Edit5: TEdit
      Left = 83
      Top = 13
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 4
      Text = 'Curent'
    end
    object Edit6: TEdit
      Left = 83
      Top = 33
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clWhite
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 5
      Text = '3'
    end
    object Edit7: TEdit
      Left = 83
      Top = 53
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 6
    end
    object Edit8: TEdit
      Left = 83
      Top = 73
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 7
    end
    object Edit9: TEdit
      Left = 164
      Top = 13
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 8
      Text = 'Estimate'
    end
    object Edit10: TEdit
      Left = 164
      Top = 33
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 9
      Text = '3'
    end
    object Edit11: TEdit
      Left = 164
      Top = 53
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 10
    end
    object Edit12: TEdit
      Left = 164
      Top = 73
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 11
    end
  end
  object GroupBox2: TGroupBox
    Left = 3
    Top = 158
    Width = 247
    Height = 95
    Caption = 'Shields per turn'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    object Edit13: TEdit
      Left = 2
      Top = 13
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = 729840
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 0
      Text = '0%'
    end
    object Edit14: TEdit
      Left = 2
      Top = 33
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 1
      Text = 'Produce'
    end
    object Edit15: TEdit
      Left = 2
      Top = 53
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = 223
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 2
      Text = 'Corruption'
    end
    object Edit16: TEdit
      Left = 2
      Top = 73
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 3
      Text = 'Rest'
    end
    object Edit17: TEdit
      Left = 83
      Top = 13
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 4
      Text = 'Curent'
    end
    object Edit18: TEdit
      Left = 83
      Top = 33
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clWhite
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 5
      Text = '3'
    end
    object Edit19: TEdit
      Left = 83
      Top = 53
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 6
    end
    object Edit20: TEdit
      Left = 83
      Top = 73
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 7
    end
    object Edit21: TEdit
      Left = 164
      Top = 13
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 8
      Text = 'Estimate'
    end
    object Edit22: TEdit
      Left = 164
      Top = 33
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 9
      Text = '3'
    end
    object Edit23: TEdit
      Left = 164
      Top = 53
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 10
    end
    object Edit24: TEdit
      Left = 164
      Top = 73
      Width = 80
      Height = 19
      BorderStyle = bsNone
      Color = clSilver
      Ctl3D = False
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentCtl3D = False
      ParentFont = False
      TabOrder = 11
    end
  end
end

 

by: calinutzPosted on 2005-03-26 at 12:40:20ID: 13637132

And:

procedure TForm1.FormShow(Sender: TObject);
begin
Edit6.SetFocus;
Edit6.SelectAll;
end;

 

by: DavidBirch2dotComPosted on 2005-03-26 at 12:56:45ID: 13637175

A simple solution would be just to play around with the stringrid properties - like this

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 192
    Top = 64
    Width = 201
    Height = 81
    BorderStyle = bsNone
    Color = clBtnFace
    ColCount = 3
    FixedCols = 0
    RowCount = 3
    FixedRows = 0
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Bell MT'
    Font.Style = [fsBold]
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing]
    ParentFont = False
    ScrollBars = ssNone
    TabOrder = 0
    OnSelectCell = StringGrid1SelectCell
  end
end

& the code to avoid people editing the row/col headers:

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  If (Arow=0) or (ACol=0) then CanSelect:= false;
end;

David

 

by: mikezangPosted on 2005-03-26 at 13:03:34ID: 13637195

In fact, I can only edit cell by mouse in my picture. I just want to know how can I set focus on that cell when form show, I mean I don't want to select that cell by mouse, may I make sense?

 

by: DavidBirch2dotComPosted on 2005-03-26 at 13:10:05ID: 13637214

you mean to get into the edit mode of that cell yes like in the picture? *thinks*

 

by: DavidBirch2dotComPosted on 2005-03-26 at 13:20:31ID: 13637244

procedure TForm1.FormShow(Sender: TObject);
begin
  StringGrid1.Col:= 1;
  StringGrid1.row:= 1;

    StringGrid1.SetFocus;
  StringGrid1.EditorMode:= true;
end;

Should do it :)

David

 

by: aikimarkPosted on 2005-03-26 at 13:21:22ID: 13637249

something to look at are the tabindex properties of all controls that can receive focus.  the control with the lowest tabindex value has focus by default.  When the user presses the Tab key, focus moves to the control (that can receive focus) with the next higher tabindex value.

 

by: DavidBirch2dotComPosted on 2005-03-26 at 13:24:22ID: 13637255

>aikimark  
that is true, but you can override it by using StringGrid1.SetFocus; or   form1.FocusControl(StringGrid1);

 

by: mikezangPosted on 2005-03-26 at 13:27:33ID: 13637263

I tried your code, but it doesn't work, I have to click on that cell, or no any cursor on that cell.

 

by: aikimarkPosted on 2005-03-26 at 13:28:01ID: 13637266

but if the property is set at design time, no code is required.

 

by: aikimarkPosted on 2005-03-26 at 13:32:43ID: 13637275

mikezang,

The link you posted is for a dialog box with individual textbox and combobox controls, not a grid.  Sorry for the confusion.

 

by: DavidBirch2dotComPosted on 2005-03-26 at 13:33:29ID: 13637278

aikimark > true lol

mikezang > the code works for me, I am guessing that we aren't prehaphs using the same properties for the stringgrid - download this example

www.davidbirch2.com/hidden/basic.zip

David

 

by: aikimarkPosted on 2005-03-26 at 13:33:30ID: 13637279

most generally, you set the row and column properties of a grid control to make a cell the active cell.

 

by: TheRealLokiPosted on 2005-03-29 at 16:27:03ID: 13657855

in your FormCreate do this

    var
        myRect: TGridRect;
    begin
        stringgrid1.cells[2, 2] := '3'; // add some data to the grid
{ optional
        myRect.Left := 2;  // set which cell is selected
        myRect.Top := 2;  // set which cell is selected
        myRect.Right := 2;  // set which cell is selected
        myRect.Bottom := 2;  // set which cell is selected
        StringGrid1.Selection := myRect;   // set which cell is selected
}
        StringGrid1.Col:= 2; // set the selected cell as well
        StringGrid1.row:= 2; // set the selected cell as well
        StringGrid1.Options :=  StringGrid1.Options + [goEditing]; // allow editing
    end;


and then in the SelectCell event do this :-

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    begin
        if (StringGrid1.Cells[ACol,ARow] <> '') then
        begin
            StringGrid1.Options :=  StringGrid1.Options + [goEditing]; //allow editing if cell has data
            CanSelect :=  True; //allow selection if cell has data
        end
        else
        begin
            StringGrid1.Options :=  StringGrid1.Options - [goEditing]; //disallow editing if cell is empty
            CanSelect :=  False; //do not allow selection if cell is empty
        end
    end;

 

by: mikezangPosted on 2005-03-29 at 17:05:41ID: 13658009

Hi, TheRealLoki
I did the same as your code, but it doesn't work, I have a source, can you check it for me?
http://www.geocities.jp/mikezang/games/calculator.zip

 

by: TheRealLokiPosted on 2005-03-29 at 17:37:13ID: 13658112

you do need the
    grd.Options := grd.Options + [goEditing];
line, which you had commented out, to enable editing again if the user clicks in that area.
you might also like to add an "onEnter" event that fouses that cell and enables the editing (so you can tab into it and make changes instead of needing the mouse)

 

by: mikezangPosted on 2005-03-29 at 17:42:47ID: 13658138

Sorry, I sent a old source, I can click that cell by mouse without comment.
What I need is that I can use TAB to select that cell or select that cell when form first show?
What can I do? can you show me the code in event onEnter?

 

by: TheRealLokiPosted on 2005-03-29 at 17:54:20ID: 13658182

procedure TForm1.StringGrid1Enter(Sender: TObject);
begin
    (sender as TStringGrid).col := 2;
    (sender as TStringGrid).row := 2;
    (sender as TStringGrid).Options :=  (sender as TStringGrid).Options + [goEditing]; //allow editing if cell has data
    (sender as TStringGrid).EditorMode := True;  // so the 1st character you type is written
end;

 

by: mikezangPosted on 2005-03-29 at 18:00:47ID: 13658214

Hi, Can you try it in my source? I did as your code, it looks like doesn't work, the cell is never selected when I click TAB.

 

by: TheRealLokiPosted on 2005-03-29 at 19:07:58ID: 13658467

Try just this, it is a lot simpler, but I think it does everything you need

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    begin
        CanSelect := ( (ACol = 2) and (ARow = 2) );
    end;

and set your StringGrid.Options to have
goDrawFocusSelected, goEditing, goAlwaysShowEditor set to true


 

by: mikezangPosted on 2005-03-29 at 19:16:46ID: 13658510

The result is the same as before. did you try it? There is no any focus on cell if I just click TAB, of course, there is a focus if I click on that cell.

 

by: DavidBirch2dotComPosted on 2005-03-30 at 01:34:05ID: 13660198

erm I was just looking at your source - on the first table on the onselectcell event you had

  if (ACol = 1) and (ARow = 1) then
  begin
  //  grd.Options := grd.Options + [goEditing];
    CanSelect := false;
  end
  else
  begin
    grd.Options := grd.Options - [goEditing];
    CanSelect := false;
  end

if both options are false then you will never be able to select the cell !?

try changing it to

  if (ACol = 1) and (ARow = 1) then
  begin
    grd.Options := grd.Options + [goEditing];
    CanSelect := true;
  end
  else
  begin
    grd.Options := grd.Options - [goEditing];
    CanSelect := false;
  end

Which works for me

David

 

by: mikezangPosted on 2005-03-30 at 01:41:21ID: 13660233

Hi, David
That source is old, I used that to try something, but I didn't get what I need, I have to click on that cell to get focus.
Or The focus will be set to cells[0, 0], even I set to cells[1,1] or col=1 and row=1.
You can try again and tell me the result. Can you use TAB to get focus on cells[1, 1]?

 

by: DavidBirch2dotComPosted on 2005-03-30 at 04:07:32ID: 13660942

mikezang > I am on a machine which is delphi-less :( but can you check if the string grid has a ongetfocus event ? if so put something like this on it:

 (sender as TStringGrid).col := 2;
    (sender as TStringGrid).row := 2;
    (sender as TStringGrid).Options :=  (sender as TStringGrid).Options + [goEditing]; //allow editing if cell has data
    (sender as TStringGrid).EditorMode := True;  // so the 1st character you type is written

David

 

by: mikezangPosted on 2005-03-30 at 05:10:18ID: 13661401

Hi
there is OnGetfocus event in Delphi's StringGrid.

 

by: mikezangPosted on 2005-03-30 at 05:11:03ID: 13661407

sorry, I made mistake, there is no this event.

 

by: DavidBirch2dotComPosted on 2005-03-30 at 06:13:15ID: 13661922

Sorry I meant the OnEnter Event

procedure TForm1.StringGrid1Enter(Sender: TObject);
begin
  StringGrid1.Col:= 1;
  StringGrid1.row:= 1;

    StringGrid1.SetFocus;
  StringGrid1.EditorMode:= true;
end;

try that

David

 

by: mikezangPosted on 2005-03-30 at 16:50:02ID: 13667397

Well, I tried, Like I said, the focus is always on the cells[0,0], not cells[1,1].

 

by: TheRealLokiPosted on 2005-03-30 at 16:51:57ID: 13667403

hmm, when I use the "OnSelectCell" event, and only allow the one cell for editing, the focus always go to that when I tab to it, not sure why it doesn't for you.

 

by: mikezangPosted on 2005-03-30 at 17:06:57ID: 13667454

Can you show me a simplest full sample? My screen looks like http://www.geocities.jp/mikezang/games/pics/SelectGridCell.jpg

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...