English 中文(简体)
使用按钮标签
原标题:Using a Button Tag
Assuming program logic changes a button tag based on something random - but using the UI thread. Is the Button Tag value reliable to use in a click event? i.e. Will it be the same value as-at the time of the event handler as it was at the time of the click? If not, what is the best method to pass an event specific parameter into a button click event that will be safe? Update Added an example as requested. (Remember this is just theoretical). Windows.Forms.Timer timer = new Timer(); timer.Interval = 1; timer.Tick += new EventHandler(timer_tick); timer.Start(); void timer_tick(object sender, EventArgs e) { this.button.Tag = Random.NextInt(100).ToString(); } void button_click(object sender, EventArgs e) { string s = (string)((Button)sender).Tag; Console.WriteLine("Tag value as at button push: " + s); } Put another way, the question boils down to: can events be wedged into the GUI event queue that allow the state of the button to be changed between the button being pushed and the click event handling the push?
问题回答
Assuming that you are using winforms. IMHO you can use Tag property to pass control specific parameters but you must also remember that windows forms controls are not thread safe. To make a thread-safe call, you must use InvokeRequired. The following link gives an example to set text for TextBox in a thread-safe way but it should not be very different for Tag property. http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx Hope this helps.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签