Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fix the column width during interactive search

Status
Not open for further replies.
Sep 26, 2020
11
IN
Hi Friends,
I am a new comer to VFP 9. I have made a form to search a record from ABC.DBF by Student's Name or By Father's Name from Command window just writing Modify Form Search. On that form there is a text box (Text1), one option group (1-Student's Name or 2-Father's Name) and a grid to show the searched data. I have given the code as below in InteractiveChange method of Text1.
==========
thisform.grid1.Column1.resizable=.F.
IF thisform.optiongroup1.option1.Value=1
SELECT s_name, f_name, stud_id FROM ABC;
WHERE ALLTRIM(UPPER(this.Value)) $ UPPER(s_name) ORDER BY s_name INTO CURSOR sf_name
thisform.grid1.RecordSource="sf_name"
ELSE
IF thisform.optiongroup1.option2.Value=1
SELECT s_name, f_name, stud_id FROM ABC;
WHERE ALLTRIM(UPPER(this.Value)) $ UPPER(f_name) ORDER BY f_name INTO CURSOR sf_name
thisform.grid1.RecordSource="sf_name"
ENDIF
ENDIF
thisform.refresh
==========
Also in Autofit method of grid I have the code as below

with thisform.Grid1
.Columns( .columnCount ).Width = 75
.Columns( .columnCount ).resizable = .f.
endwith
==========
The problem is : The columns of grid are being squeezed when any record matches to the search criteria of Text1.
So please suggest how to solve the grid squeeze problem.

Thank you all.
 
What exactly do you mean when you say the columns are being "squeezed"? What are you seeing? And what do you expect to see?

In general, you would never set a column's Resizable to .F. or change its Width from within Autofit. The whole point of Autofit is that it calculates the column widths automatically. Also, Autofit won't work on any column's whose Resizable is .F.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 


SANJIB KR KANUNGO said:
The problem is : The columns of grid are being squeezed when any record matches to the search criteria of Text1.
So please suggest how to solve the grid squeeze problem.

Once you've followed Mike's advice you may like to consider adding a little more width to the columns. This Link is how I do it.

Regards,

David.

Recreational Developer / End User of VFP.
 
Did you perhaps mean: The columns of grid are being squeezed when [highlight #FCE94F]no[/highlight] record matches to the search criteria of Text1.

So how about setting AllowAutoColumnFit =2 to disallow it interactively? Then also don't call AutoFit, because a call overrides the AllowAutoColumnFit setting.

Code:
*thisform.grid1.Column1.resizable=.F.
thisform.grid1.allowautocolumnfit=2 && autofit disallowed
IF thisform.optiongroup1.option1.Value=1
SELECT s_name, f_name, stud_id FROM ABC;
WHERE ALLTRIM(UPPER(this.Value)) $ UPPER(s_name) ORDER BY s_name INTO CURSOR sf_name
thisform.grid1.RecordSource="sf_name"
ELSE
IF thisform.optiongroup1.option2.Value=1
SELECT s_name, f_name, stud_id FROM ABC;
WHERE ALLTRIM(UPPER(this.Value)) $ UPPER(f_name) ORDER BY f_name INTO CURSOR sf_name
thisform.grid1.RecordSource="sf_name"
ENDIF
ENDIF
if reccount("sf_name")>0
   thisform.grid1.allowautocolumnfit=0
endif  
thisform.refresh

REad the help on allowautocolumnfit and notice calling Autofit always autofits, so you also need to not call autofit. If you have code in the refreh method of the form that should only do autofit, if a grids recordsource has a reccount(grid.recordsource)>0.

Chriss
 
Hi Friends,
Thank you a lot for your response. Actually the word "Shrink" should be used in place of "Squeeze". Now I attached two screenshots of my problem on a word document file herewith. So please suggest in this regard.

And inadvertently posted my problem to another thread.

Thank you.
 
 https://files.engineering.com/getfile.aspx?folder=7aaf6564-edea-4c5a-bef6-049918f80c89&file=SHRINKING_OF_GRID_COLUMNS.docx
We've already given you the information you need. To summarise:

- Set AllowAutoColumnFit to 2.

- Call the AutoFit method after you finish a search and refresh the grid.

- Don't write any code in the AutoFit method; and don't set the column's Resizable to .F.

I suggest you try that, and report back.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Friends,
Thank you all for your support.
Your suggestion has improved the appearance of grid. Now the column remains as per the length of the searched data. But I want to fix the column width of grid as such.
Thank you all
 
You either want fit or not, there are even multiple ways to fix the width of columns after you or autofit set them as you want and you've already been pointed to that:
Set resizable to .f. for all columns you want to have fixed against both interactive changes and changes by calling AutoFit. There also are several Allow... properties of the grid, read about them. You'e also been warned that AllowAutoColumnfit=2 only disallows interactive autofit by doubleclicking the upper left corner of a grid. Only the resizable property set to .f. protects a columns width against calls to autofit.

I also think any code setting width can't be hindered to take affect with any seetting. So even a resizable=.f. column will take the width code sets into its width property.

I think I don't really understand what you want, I also see you didn't get the change I made to your code. This has to go back to where the code came from you initially posted, not into a click of another button or whatever else. The major change I made is to only allow autofit when there is result data. See th help on the valid values of allowautocolumnfit and what they mean.

All in all, just to summarize: Code that does make use of autofit should care for having data that results in sensible width. That's needing even more care than to check ofr reccout>0, if any field of a ccursor is empty in all rows that means autofit will set its width to 0. Autofit help tells you what does and what doesn't hinder it to change columns.

Then there is no virtue in putting code into the AutoFit method itself. It's too late to care for individual columns in there. To mend what autofit does I'd always expect code to do that in the merthod that calls the AutoFit, and let AutoFit itself do its native behviour, which can be partly controlled by the resizable property of columns.

In the simplest case you could autofit and then give any column that has 0 width from autofit a minimum width. If you think of Minwidth, well, take a look into the help, it only applies to forms (and the screen, as it is one), but you would need to have code in resize events to implement a minwdith behaviour.


All that said, the simplest summary is: Make use of resizable =.f. where you don't want resizing to happen. Also look for any code that doesn't call autofit but acts on width directly, that'll even do its job when resizable=.f.

If you look for a grd property fixing all columns, there is non, use SetAll to set resizable for all columns.

Chriss
 
Another point to keep in mind is that auto fitting only affects visible data. I mention this because it is something that many programmers don't appreciate, and they therefore think AutoFit is going wrong when in fact it is not.

By "visible" data, I mean the cells of the grid that are visible at the time that the auto-fit takes place (whether automatically or by user action). So if the widest value in a given column in the visible window is 60 pixels, and there is a record lower down the table (below the visible window) where the width is 100 pixels, the width of the column will still be 60 pixels. If the user then scrolls down to bring the wider value into view, that won't change the width of the column.

This is something you should keep in mind.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Answering with the title of your question in mind: You act in the textbox interactive change. Nothing there changes column width. somewhere in any code triggered by the form Refresh autofit is called. And if you don't want autofit behavior you could set all columns resizable to .F. or track where autofit is called.

The code in autofit only "protects" just hte last columns width. So the refresh process seems to be designed to want autofit of all except the last column.

As you're new to VFP9 you may also not realize that Thisform.Refresh() is not just calling the method of the form, it causes a cascade of everyrthing contained in the form to refresh itself. So code changing the grids columns widths or calling grid autofit change the grid columns. If not even the last column becomes 75 pixels in the end, then this means no refresh method calls autofit, but something acts on the widths.


Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top