Windows Develop Bookmark and Share   
 index > Windows Forms General > Update all labels based on an event
 

Update all labels based on an event

In my code I have a bunch of event handlers like this one:

private void cbx_game_r4_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToString(cbx_game_r4.SelectedItem) == "bigBlind")
{
try
{
gr4.Text = Convert.ToString(thirdParty.d[selectedKey].game.BigBlind);
}
catch
{
}
}
else if (Convert.ToString(cbx_game_r4.SelectedItem) == "pot")
{
try
{
gr4.Text = Convert.ToString(thirdParty.d[selectedKey].game.Pot);
}
catch
{
}
}
}


In another class I have an OnMessage event handler. Whenever I recieve a message in that class I want all labels (i.e. gr4 in the above example) to refresh based on all the "xxx_SelectedIndexChanged" event handlers I have. I have the "form object" availible in the class where the OnMessage event handler is. I'm not sure if that matters.

How can I achieve this refreshing of labels every time my OnMessage event handler that resides in another class get's activated? This is the only thing that is missing in my first graphical application Smile I'm looking forward to getting it compleate.

Thanks!
RRRouter  Saturday, August 18, 2007 9:45 PM
Problem solved
RRRouter  Sunday, August 19, 2007 10:48 PM

Hi,

First of all, you don't need this, "Convert.ToString(cbx_game ...."

Instead, cbox_game_r4.Text is enough to get the selected text.

Then, assume that you have two class class A and B.

Class A will have OnMessage EventHandler, Class B has label updating code.

Then you can create event (e.g. UpdateLabelEvent) in Class A and add event hander in Class B and attach like this.

Code Snippet

class A

{

//in OnMessage, raise event

UpdateLabelEvent();

}

class B

{

//create instance of A and attach event handler to it

A a = new A();

a.UpdateLabelEvent += new EventHadler(ChangeLabelEventHandler);

}

Hope u got it

wirol  Sunday, August 19, 2007 1:55 AM
There's one thing that is stopping me from solving this and that I can't figure out. How do I access the form controls and the form events from an outside class. Here is how my code looks:

There are three classes below. That is 2 more than I can handle in this situation Smile Help would be great

//main code
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form = new Form1();
Application.Run(form);
}

//form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace WindowsApplication1
{
public partial class Form1 : Form
{
...//bunch of code using controls
}
}


//other class
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
class MainCode
{
IPC ipc;
Form1 form;
Table table;
public Dictionary<string, Table> d = new Dictionary<string, Table>();

public MainCode(Form1 host) // initializer typ
{
form = host;
ipc = new IPC(host);
ipc.OnMessage += new IPC.MessageAction(ipc_OnMessage);
}
}
}



I want to be able to use the form's events and controls in MainCode.

Thankful for help!

RRRouter  Sunday, August 19, 2007 7:31 PM
Problem solved
RRRouter  Sunday, August 19, 2007 10:48 PM

Hi RRRouter,

It would be helpful if share your solution with us.

Thank you!

Rong-Chun Zhang  Wednesday, August 22, 2007 6:07 AM

You can use google to search for other answers

Custom Search

More Threads

• 1 Millisecond Timer
• Issue with keypress event
• type mismatch spinner value and textbox in VS 2005 Beta2 and SQL Server 2005 Beta
• ToolStripContainer and MDI
• Glitches associated with controls when loading forms
• Problem with showing form with small size.
• Retrieving data from ListView
• Does GDI+ support using bitmap fonts or tiled images?
• re-defining the function keys
• Cant stop a Form from losing focus or being the active form