Solved

Text Box update problem ... Strange

Posted on 2000-03-06
19
202 Views
Last Modified: 2010-05-02
I have a form with a text box (Text1) ... next to the text
box is a "Calculate" button which allows the user to pop up a second form, enter some data, and calculate the value
to be inserted into Text1.Text.
 
Problem is this ... after all the data is entered in Form2,
the next step is to click an "Update" button which attempts to update the Text1.Text value using in part the callback
procedure:
...
Form1.Text1.Text = Value
 
However, when you do this, the value of Text1.Text on
the screen does not change unless you close Form1 and
reopen it. When reopened, the value of Text1.Text is
changed to the correct value indicating that the original
callback on Form2 did trigger the value-changed callback
tied to Text1 ...
 
So why doesn't the value of Text1.Text change when I
hit the button of Form2?
 
Thanks for assistance.
--
Fred Phelan
frederick.phelan@nist.gov
0
Comment
Question by:phalanx777
  • 9
  • 4
  • 3
  • +3
19 Comments
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Is the value you are trying to set to text1.text numeric??? If so, you need to do this:

   Form1.Text1.Text = Cstr(Value)


Cheers!®©
 


0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
You may also want to do:

   Form1.Text1.Refresh


Cheers!®©
0
 
LVL 3

Expert Comment

by:groone
Comment Utility
with MCRiders comments -  if you are calling Text1 from another form and giving it a value dont follow it with the "Text" simply do as follows

Form1.Text1 = CStr(Value)
0
 

Author Comment

by:phalanx777
Comment Utility
I have tried all these things including:

Form1.Text1.Text = Cstr(Value)
Form1.Text1 = Cstr(Value)
Form1.Text1.Refresh
Form1.Refresh

I am wondering if VB does it on purpose because Text1 has a value changed callback, or if it is a bug.

Fred
0
 

Author Comment

by:phalanx777
Comment Utility
Adjusted points to 50
0
 

Author Comment

by:phalanx777
Comment Utility
I created a separate project consisting of

frmMain.Text1
frmMain.Text2
frmMain.Command1
Form2.Text3
Form2.Command2

The callback for frmMain.Command1 is:

Text1.Text = Text2.Text

The callback for Form2.Command2 is:

frmMain.Text1.Text = Text3.Text

It all works from frmMain, but not Form2.

This isolates the problem a bit ...
it tells us that if you do the text
change form another form, it will not
work, but it does work if you do it
from the same form.

Some event must not being sent. If I put
a

MsgBox "test"

command in the Text1_Changed callback
the message box comes up in both cases,
but the text only changes in the first case.

Fred
0
 

Expert Comment

by:motile
Comment Utility
Hi,

I tried your exmaples and it all worked well for me, even from a second form.
Maybe your VB is not installed properly and this is the cause for this bug.

In the meantime, to go around this problem, try to use a global variable.

Let's take your test project for example:

Add a module to the project, in which you declare a global variable (an integer for example):

Global G_Value as Integer

In the Click event of Form2.Command2 write this:

G_Value = Text3.Text
frmMain.Text1.Text = G_Value


Good luck.

0
 

Author Comment

by:phalanx777
Comment Utility
I created a separate project consisting of

frmMain.Text1
frmMain.Text2
frmMain.Command1
Form2.Text3
Form2.Command2

The callback for frmMain.Command1 is:

Text1.Text = Text2.Text

The callback for Form2.Command2 is:

frmMain.Text1.Text = Text3.Text

It all works from frmMain, but not Form2.

This isolates the problem a bit ...
it tells us that if you do the text
change form another form, it will not
work, but it does work if you do it
from the same form.

Some event must not being sent. If I put
a

MsgBox "test"

command in the Text1_Changed callback
the message box comes up in both cases,
but the text only changes in the first case.

Fred
0
 
LVL 1

Expert Comment

by:eab111098
Comment Utility
in your code do perform the update, stick a "DoEvents" as the last entry in the procedure.


Ed.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:wsh2
Comment Utility
EAB.. how can you proffer that as an answer?.. <sheesh>. Its obvious that phalanx777 has NOT provided the right  information about his problem.

The updating of a Form1.Textbox from Form2 is simplicity itself.. and to have it display immediately.. is done automatically by VB if both forms are visible. No.. there is more to this story.

I suppose the most telling symptom here is when phalanx777 writes that..

"However, when you do this, the value of Text1.Text on
the screen does not change unless you close Form1 and
reopen it. When reopened, the value of Text1.Text is
changed to the correct value indicating that the original
callback on Form2 did trigger the value-changed callback
tied to Text1 ..."

Now.. how did that happen? When you close and reopen a form.. Textbox.Text1 is reinitialized back to its DEFAULT setting, But in his case, the changed value appears.  How did the Form_Load procedure know the correct value? There is more to this story that we don't know.. <sigh>.

Is the Form1.Textbox bound to a data control? What is "Value"? A global variable?.. a class property? And how is the new "Value" being derived? (Quite candidly, I think this is where the problem rests.. phalanx777 put a debug breakpoint in and check that Value is correct before it is passed to Form1.Text.. k?)

All in all.. changing Form1.Text1.Text and having it display.. IS THAT SIMPLE. The cstr(value) comments made here, while nice, are truly marginal in what they do, because if not specified, VB does the conversion automatically. Do Events? EAB, exactly which Event Procedure does the statement Form1.Text1.Text = Value trigger that is going unprocessed? Nevertheless EAB, I love ya just the same.. and I hope you take my comments in good humor.. <smile>.

Phalanx777.. use a debug breakpoint, k? I think you will find that you are putting the original value back into the Textbox.. thus causing it to appear unchanged.. <wink>.
0
 

Author Comment

by:phalanx777
Comment Utility
Cannot accept this as the answer ... sorry. I have put DoEvents all over the place and it does not help in the slightest.

To wsh2: The entire mystery is that the value-changed callbox tied to Text1 is called  from Form2... this is proven because in this routine a global variable is set equal to the value of Text1.Text. When the form is unloaded and loaded, the value is correct, even though a change on the screen does not appear. This means that the value changed callback was called, because when the form reloads, the display value of Text1.Text is set to the global variable.

I am using VB5 ... could it be a bug in VB5 corrected in VB6?

Fred
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
I doubt VERY VERY VERY much this is a VB compiler bug.. but rather a logic one.

Ok.. by virtue of the Form1.Form_Load event (when we open and close), we know that the Global value is getting updated. However, by all evidence given, this update is happening AFTER the Form1.Text1.Text write.

The easiest / best way to test this is to use VB Debug commands, set a breakpoint, and then Step through your code.. placing your cursor on each variable when you want to examine its value. Short of doing that, the second approach would be to strategically place Msgbox statements thoughout your program.. displaying the values you want to see.

In this case.. try this.. place the following statement in the Form2.Update_Click procedure right BEFORE the Form1.Text1.Text = Value statement..

MsgBox (Value)

and then run your program. I think you will find that Value will read what Text1 does.. <smile>. For the program to work.. you have to make sure that Value IS updated BEFORE you display it.. As such, what logic / procedures / events do you use to update Value?
0
 

Author Comment

by:phalanx777
Comment Utility
A few things:

1) I put MsgBox(Form1.Text1.Text) calls in the Update_Click procedure both before and after the Form1.Text1.Text = Value statement ... it shows that the value of Form1.Text1.Text changes internally when it is called, but it does not change on the screen.

2) re your comment: "make sure that Value IS updated BEFORE you display it.. "  

This is not feasible ... in the context of the application we are talking about, Form1 is elaborate in which users enter a great number of values. The Form2 is a simple pop-up called from Form1 in which users enter about 3 values for a formula which allows them to calculate an exact value for a certain variable on Form1. I don't want to have to Unload Form1 and reload it every time I pop-up Form2 to calculate the variable ... I want Form1 to update automatically when I click "Update".

Did you see my test above? ... for some reason this only seems to work of the Update_Click procedure is in the same Form as Text1.

Strange.

Fred
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Fred.. rather than debug online as we are.. can you zip up the source code and send it to me? I think it will save our keyboards.. LOTZA punches.. <lol>.

Wsh2@aol.com
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
phalanx777,

I know you said that you have VB5.  Do you have Service Pack 3 installed??? If not, you should... There are a boatload of fixes in it. Check out the following microsoft KB article:

INFO: Visual Studio 97 Service Packs - What, Where, and Why
http://support.microsoft.com/support/kb/articles/Q170/3/65.ASP
 

Cheers!®©
0
 

Author Comment

by:phalanx777
Comment Utility
Guys, Just tested on VB6. Reproduces there. Ughh.

Fred
0
 

Author Comment

by:phalanx777
Comment Utility
Ok, next test was this ... created sample project

frmMain, frmMain.Text1, frmMain.Text2, frmMain.Command1

Form1, Form1.Text3, Form1.Command2

Form2, Form2.Text4

The callback for frmMain.Command1 is:

Text1.Text=Text2.Text

This works ok ...

The callback for Form1.Command2 is:

frmMain.Text1.Text=Form1.Text3.Text
Form2.Text4.Text=Form1.Text3.Text

In this case, the value of Form2.Text4 updates but the value of frmMain.Text1 does not update. Maybe the problem only exists when the text box is drawn on the main form. What could be the problem with that?

I wonder if there is a work around.

Fred

0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
Comment Utility
Having read your source.. you have a BAS module sub main procedure from which you start the program. In the module, you are creating a new Form1 called fMainForm.. and then displaying fMainForm rather than displaying the frmMain that we all have been so valiantly trying to update.. <smile>.

To be honest, the fMainForm is NOT necessary.. just use frmMain as it will readily suffice. As such, change your BAS sub main Module1 to read..

Sub Main()
    frmMain.Show
End Sub

----------------------------------------Change frmMain's Command1_Click () to read..

Private Sub Command1_Click()
    Text1.Text = Text2.Text ' I suspect this is only there for testing
    Form2.Show
End Sub

----------------------------------------Change Form2's Command2_Click () to read..

Private Sub Command2_Click()
  frmMain.Text1.Text = Form2.Text3.Text
  Form2.Hide
End Sub

----------------------------------------and that will give you what you want.. <smile>
0
 

Author Comment

by:phalanx777
Comment Utility
My mea culpa for sure ... for some reason the code wizard starts main as fMainForm rather than frmMain. I guess to be Hungarian about everything. Thanks wsh2 ... Fred
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now