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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transfering selected grid value from one form to another form textbox

Status
Not open for further replies.

alazarbaharu

Instructor
Apr 17, 2015
11
GB
Hey there how you doing i have one problem and i searched over the internet and i didn't found what i needed and i decided to post my question on this forum as i have done before and my question looks like this
i have form name form 1 and on this form one i have a grid control called form1grid1 having three columns column1, column2 and column3 and one button called button 1 and i have another from by the name form 2 and on this from i have textbox called form1textbox1.
what i want is on form 1 if i select a row value from the list in the grid and press button 1 i need the selected value of the grid column1 to be the value of form1textbox, how can i do this please help me on this. Thank You.
 
Your question is in two parts.

First, to get the value from column1 in the grid, you need to look at the underlying table or cursor - the one you used to populate the grid. Just look at the value in the field which is bound to column1, and that's your answer. Something like this:

[tt]luValue = MyTable.Field1[/tt]

Second, to get that value into a text box, just copy it into the textbox's Value property:

[tt]Form2.textbox1.value = luValue[/tt]

The place to do that is in the Click event of Button1. Of course, you can combine the above two lines into a single line. I made it two separate lines simply to clarify the technique.

In the above example, I assume that Form2 is active, and that Form2 is the name of the variable that holds an object reference to the form (not merely the form's Name property), and that variable is in scope when the above line is executed. If that's not the case, come back, and we can take it further.

Mike

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi,

I had this piece of code hanging around.

Just run the PRG and click anywhere in the left grid of the form labeled "Grid selecting from cursor" - the second form then displays the value of the 2nd column.

Please feel free to adapt to your needs.

Code:
**************************************************
PUBLIC oform1, oForm2

oForm1 = CREATEOBJECT("form1")
oForm2 = CREATEOBJECT("Form2")


WITH oForm1.grid1
	.SetAll("DynamicBackcolor", "IIF(curtemp.f3, RGB(0,125,125), RGB(255,255,255))","Column")
	.SetAll("DynamicForecolor", "IIF(curtemp.f3, RGB(255,255,255), RGB(0,0,0))","Column")
	.Column1.ReadOnly = .T.
	.Column2.ReadOnly = .T.

ENDWITH 

WITH oForm1.grid1.Column3
	.Sparse = .F.
	.AddObject("chkBoxSelection", "chkBox1")
	.CurrentControl = "chkBoxSelection"

ENDWITH 

oForm2.Show
oForm1.Show

Read Events
Close all
Clear All
RETURN


**************************************************
DEFINE CLASS chkBox1 AS CheckBox
	Visible = .T.
	Caption = ""
	
	PROCEDURE Click()
		This.Parent.Parent.Refresh()
	ENDPROC 
ENDDEFINE 

DEFINE CLASS form2 AS Form
	Caption = "Textvalue from 1st form"
	Left = 120
	Height = 480
	Width = 600
	Minheight = This.Height
	MinWidth = This.Width
	
ADD OBJECT txtName as TextBox WITH ;
	Left = 24, ;
	Top = 24, ;
	Name = "txtName"
		
ENDDEFINE 

DEFINE CLASS form1 AS Form
	AutoCenter = .T.
	Caption = "Grid selecting from cursor"
	Height = 480
	Width = 600
	Minheight = This.Height
	MinWidth = This.Width
	
	
ADD OBJECT lblOData AS Label WITH ;
	Caption = "Original Data", ;
	Left = 10, ;
	Top = 30, ;
	Anchor = 3
 
ADD OBJECT lblSelection AS Label WITH ;
	Caption = "Results", ;
	Left = 300, ;
	Top = 30, ;
	Anchor = 1 + 2

ADD OBJECT grid1 AS Grid WITH ;
	ColumnCount = -1, ;
	Left = 10, ;
	Top = 48, ;
	Width = 270, ;
	Height = 408, ;
	Anchor = 1 + 2 + 4, ;
	RecordSource = "curTemp"
	
	PROCEDURE Grid1.AfterRowColChange()
		LPARAMETERS nColIndex
		WITH oForm2
			.txtName.Value = curTemp.f2
			.Refresh()
		ENDWITH 
	ENDPROC 
	

ADD OBJECT grid2 AS Grid WITH ;
	ColumnCount = -1, ;
	Left = 300, ;
	Top = 48, ;
	Width = 270, ;
	Height = 408, ;
	Anchor = 1 + 2 + 4 + 8, ;
	Visible = .F.

	
ADD OBJECT cmdSearch AS CommandButton WITH ;
	Left = 222, Top = 6, Height = 21, Width = 90, Caption = "SQL"
	
	PROCEDURE cmdSearch.Click()
		
		SELECT f1, f2, ALLTRIM(SUBSTR(f2,4)) + ALLTRIM(STR(f1)) as f6, INT(f1/4) as f5, f3 FROM curTemp WHERE f3 ;
			ORDER BY 1 ;
			INTO CURSOR curTemp2
			
		IF _tally > 0
			WITH Thisform.grid2
				.Visible = .T.
				.ColumnCount = -1
				.RecordSource = "curTemp2"
				.DeleteColumn()
			ENDWITH
		ELSE
			WITH Thisform.grid2
				.Visible = .F.
			ENDWITH
		
		ENDIF 
		
		Thisform.Refresh()
		
	ENDPROC

ADD OBJECT cmdReset AS CommandButton WITH ;
	Left = 324, Top = 6, Height = 21, Width = 90, Caption = "Reset"
	
	PROCEDURE cmdReset.Click()

		IF FILE("curtemp2")
			SELECT curtemp2
			USE
		ENDIF 

		WITH Thisform.grid2
			.Visible = .F.
		ENDWITH

		SELECT curtemp
		UPDATE curTemp SET f3 = .F.
		LOCATE 

		Thisform.Refresh()
		
	ENDPROC

PROCEDURE Destroy
	CLOSE ALL
	Clear Events

ENDPROC

PROCEDURE Load
 CREATE CURSOR curTemp (f1 I AUTOINC NEXTVALUE 1 STEP 1, f2 C(20), f3 L)
 FOR i = 1 TO 25
  INSERT INTO curTemp (f2, f3) VALUES ("T" + SYS(2015), .F. )
 ENDFOR
 LOCATE 

ENDPROC

ENDDEFINE
*********************************************

hth

MK
 
I'll face the problem from the encapsulation principle.

Transfer of information can be done from one object to another, interactions are needed to get anything going, total encapsulation is not what the encapsulation principle is about, it's about encapsulation of the internal concerns. But why should a button of some form set a value in some other form's textbox? Every code that crosses borders like the border between two form objects makes the two physical objects part of a greater logical object and is causing dependencies you won't want to grow most of the time. It's like designing a hardware, in which you can't simply replace some component without also replacing another one.

There are several ways to decouple this to avoid a dependency. It's not against any rule a popup form knows its parent form, its purpose often is to act on something in the parent form. But that doesn't need to be the form itself or it controls, it most often will be some table, specific record or field to set, to which the parent form is bound, so there is no need to know the other form, there is the more specific need to know what data to change to indirectly interact, so the other from just needs a refresh and the information is passed by acting on a shared table/record/field.

Bye, Olaf.
 
If I understood Olaf' correctly.

The data (table) is already available everywhere(?) as selected by your first form.

add the following statement to the init of the form2

thisform.form1textbox1.value = table1.col1 && table1 is your data table as attached to form1's grid.

Note : You need to refresh form2 when changing the grid row in form1.
 
What I say would rather lead to something like

[pre]replace fieldx with table2.fieldy in table1[/pre]

Where the textbox is bound to table1.fieldx and the grid in the popup form is bound to table1, so you pick a certain record and want some field to be copied over. This makes this operation not working on the form level, but on the data level and pass on the information through the database, as it should be. You have the database to let all users see the same data, that also applies to your own session and the two forms in it.

Bye, Olaf.
 
NasibKalsi,

You suggest placing the code (to assign the value to the textbox) in the Init of Form 2. I think you are assuming that Form 2 is modal. If it is, it might be better to pass the value in question as a parameter to Form2. That would make Form 2 that little bit more self-contained.

That said, it has been a full week now since the original post, and the person who posted the question has not come back, so maybe we shouldn't make too many guesses about what he is trying to achieve.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hello guys thanks all for your support I have used Mikelewis idea and its working perfectlly and sorry for replaying letley. Thanks for your support
 
MikeLewis:

I agree with your suggestion. Table subset may be helpful and easier when displaying multiple fields on form2.

nasib

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top