473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Problems Updating label Value

hey guys,

I've got a weird one for ya....i have a form which takes user input in
the form of textbox's etc. It then grabs some details from a file and
updates some of the labels with some info like numbers etc.

Anyway, i'm just updating the field properties like lblnumber.Text =
variable1;

Now the weird thing is that...when i run the app and it cycles through
some loops it never updates the values of those fields...even though
they havent thrown an error. So I checked if the loop was even running
by adding a messagebox.showpop up and sure enough its def running but
the text labels and textboxes dont change.

Any ideas why?

Sep 9 '07 #1
4 7592
di*******@askcybersteve.com wrote:
[...]
Now the weird thing is that...when i run the app and it cycles through
some loops it never updates the values of those fields...even though
they havent thrown an error. So I checked if the loop was even running
by adding a messagebox.showpop up and sure enough its def running but
the text labels and textboxes dont change.

Any ideas why?
The two most common reasons this happens:

* The code is using the wrong form instance. Usually because the
programmer gets the form instance by calling "new Form1()" (where
"Form1" is the name of the form class). You need to make sure you are
using the instance that already exists, rather than creating a new one.

* The code is trying to access the form from the wrong thread.
This is much less common in .NET 2.0 and later, because when you are
running under the debugger you get an MDA exception telling you that
you're doing this, and that it's wrong to do so.

So, assuming you're using .NET 2.0 or later, I'd put my money on the
first option.

If neither apply, then you need to post a concise-but-complete example
of code that demonstrates your problem.

Pete
Sep 9 '07 #2
Hi! Yeah this seems really weird...Okay so its not updating any of the
text fields when I run it..it seems to be something to do with teh
while loop because when i comment out the while loop statements...the
first field...total counts does indeed update.

Any ideas?
public frmMainPage()
{
InitializeComponent();
}

private void quitToolStripMenuItem_Click(object sender,
EventArgs e)
{
Close();

}

private void panel1_Paint(object sender, PaintEventArgs e)
{

}

private void btnStart_Click(object sender, EventArgs e)
{
// Save Form Variables upon startclick

String Name = this.txtName.Text;
String Email = this.txtEmail.Text;
String Url = this.txtURL.Text;
string Comment = this.txtComment.Text;
int test = 0;
int ctr = 0;
string read = null;
string UrlGrab = null;
string postvar = "&newname=" + Name + "&newmail=" + Email
+ "&newloc=65&newurl=" + Url + "&newtext=" + Comment;
int TotalCount;

// Grab the URL totals out of the method
TotalCount = staticclass.Counter();
this.lblTotalCount.Text = Convert.ToString(TotalCount);

// Set progress bar maximums
progressBar1.Maximum = TotalCount;

// Open Stream
StreamReader tr = File.OpenText("url.txt");
// Start the loop

while (test == 0)
{
string textcount = Convert.ToString(ctr);
this.txtsucess.Text = textcount;
// Increment Counter Variable
ctr++;

// grab the first line from file
read = tr.ReadLine();

// assign value of grab to a string

UrlGrab = read;
// Set Post Variables Tuned For Achim Winkler
byte[] buffer =
Encoding.ASCII.GetBytes("&newname=" + Name + "&newmail=" + Email +
"&newloc=65&newurl=" + Url + "&newtext=" + Comment);

// Initialise HTTP Request and Method

HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create(UrlGrab);

// Set WebRequest Method ie post or Get

WebReq.Method = "POST";

// Set the content typeof the form

WebReq.ContentType = "application/x-www-form-
urlencoded";

//The length of the buffer (postvars) is used as
contentlength.

WebReq.ContentLength = buffer.Length;

//We open a stream for writing the postvars

Stream PostData = WebReq.GetRequestStream();

//Now we write, and afterwards, we close.
Closing is always important!

PostData.Write(buffer, 0, buffer.Length);
PostData.Close();

//Get the response handle, we have no true
response yet!

HttpWebResponse WebResp =
(HttpWebResponse)WebReq.GetResponse();

//Let's show some information about the
response

// Console.WriteLine(WebResp.StatusCode);
// Console.WriteLine(WebResp.Server);

//Now, we read the response (the string), and
output it.

Stream Answer = WebResp.GetResponseStream();

StreamReader _Answer = new
StreamReader(Answer);

// Console.WriteLine(_Answer.ReadToEnd());

// Update Status Bar

progressBar1.Value = progressBar1.Value +
1;
// Update exit part of loop
if (read == "end")
{
test++;
}

}

// close the text readerstream
tr.Close();
On Sep 9, 12:21 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
direct...@askcybersteve.com wrote:
[...]
Now the weird thing is that...when i run the app and it cycles through
some loops it never updates the values of those fields...even though
they havent thrown an error. So I checked if the loop was even running
by adding a messagebox.showpop up and sure enough its def running but
the text labels and textboxes dont change.
Any ideas why?

The two most common reasons this happens:

* The code is using the wrong form instance. Usually because the
programmer gets the form instance by calling "new Form1()" (where
"Form1" is the name of the form class). You need to make sure you are
using the instance that already exists, rather than creating a new one.

* The code is trying to access the form from the wrong thread.
This is much less common in .NET 2.0 and later, because when you are
running under the debugger you get an MDA exception telling you that
you're doing this, and that it's wrong to do so.

So, assuming you're using .NET 2.0 or later, I'd put my money on the
first option.

If neither apply, then you need to post a concise-but-complete example
of code that demonstrates your problem.

Pete

Sep 10 '07 #3
Steven Rowland wrote:
Hi! Yeah this seems really weird...Okay so its not updating any of the
text fields when I run it..it seems to be something to do with teh
while loop because when i comment out the while loop statements...the
first field...total counts does indeed update.
I believe that Marc's observation hits the nail on the head. I presume
that the issue here is that the text labels don't update while the loop
is running; presumably if your loop eventually reads the "end" line and
exits, the labels are finally updated (to the last value they were set to).

This is because until your btnStart_Click() method returns, no UI
updating will be done.

As a quick hack to test the theory, you can add a call to
Control.Update() (using the "txtsuccess" control instance) or
Application.DoEvents(), which would allow the update to be made visible
on the screen. However, this technique is a very bad idea for an actual
application someone will use.

Better would be to put the code that's taking a long time into a
BackgroundWorker thread, just as Marc suggest. From there, you can use
Control.Invoke() or Control.BeginInvoke() to update text labels, and the
ReportProgress event to allow the progress bar to be updated.

By the way, the fact that you are doing all of this processing inside of
a loop executed in the Click event handler of a control is essential to
the problem description. Your original post was not clear on this
point; I hope you can see from this how important it is to at least be
very specific about the situation, if not post code (as you did in the
follow-up).

Pete
Sep 10 '07 #4
Thanks guys!

Looks like ive got alot of hacking around to do then :)

Its funny...a textbook can really only show you so much. Until you
actually get into the technology and applying it all these sort of
little problems dont crop up. Clearly, C# textbooks cant teach you to
be a great programmer or to design really good logic.

Thanks for bearing with my newbie questions :)

steve
On Sep 10, 12:26 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
Steven Rowland wrote:
Hi! Yeah this seems really weird...Okay so its not updating any of the
text fields when I run it..it seems to be something to do with teh
while loop because when i comment out the while loop statements...the
first field...total counts does indeed update.

I believe that Marc's observation hits the nail on the head. I presume
that the issue here is that the text labels don't update while the loop
is running; presumably if your loop eventually reads the "end" line and
exits, the labels are finally updated (to the last value they were set to).

This is because until your btnStart_Click() method returns, no UI
updating will be done.

As a quick hack to test the theory, you can add a call to
Control.Update() (using the "txtsuccess" control instance) or
Application.DoEvents(), which would allow the update to be made visible
on the screen. However, this technique is a very bad idea for an actual
application someone will use.

Better would be to put the code that's taking a long time into a
BackgroundWorker thread, just as Marc suggest. From there, you can use
Control.Invoke() or Control.BeginInvoke() to update text labels, and the
ReportProgress event to allow the progress bar to be updated.

By the way, the fact that you are doing all of this processing inside of
a loop executed in the Click event handler of a control is essential to
the problem description. Your original post was not clear on this
point; I hope you can see from this how important it is to at least be
very specific about the situation, if not post code (as you did in the
follow-up).

Pete

Sep 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: | last post by:
Hello, Sorry to ask what is probably a simple answer, but I am having problems updating a table/database from a PHP/ PHTML file. I can Read From the Table, I can Insert into Table/Database, But...
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
2
by: kaczmar2 | last post by:
I have an ASP.NET page written in VB.NET that has a label: <asp:Label runat="server" ID="lblStatus" CssClass="LabelTxt"></asp:Label> In my code behind, I am running some stored procedures and...
3
by: Christian Filzwieser | last post by:
Hy I created a WebControl called StatusBar with a static function public static void SetText(string text) { MyVariable = Text } and on Page Load I set MyVariable to the Label in my WebControl....
3
by: =?Utf-8?B?U21pdGE=?= | last post by:
Hello all, I have a gridview, and a label inside the <Columnsand ItemTemplate. The label is a hidden. I want to retrieve the value of this label on the gridview_OnPageIndexChanged event, but i...
2
by: berry | last post by:
Hi all... I want to delete the data that over 5years. But my code has such error: "vb6 error: Row cannot be located for updating. Some value may have been changed since it was last read." And...
23
by: tanya2001 | last post by:
hi all.. I am trying to update my datagrid in my webform...but its not getting updated..though in the database it removing the <null> and inserting a blank field....its not taking the input which m...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
1
by: pv003220 | last post by:
Hi Guys, I wonder if anybody could help me with a bit of VB coding. Im after some code so that I can select a value from a table, and then be able to update that value. My aim is to be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.