7 Replies - 567 Views - Last Post: 10 September 2010 - 12:38 AM Rate Topic: -----

#1 rapo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 27-November 08

Sending a component as parameter to a method

Posted 09 September 2010 - 06:42 AM

I've made a method to write some text in some RichTextBoxes. The code is :
private void Log(LogMsgType msgtype, string msg,RichTextBox rtb)
        {
            rtb.Invoke(new EventHandler(delegate
            {
                rtb.SelectedText = string.Empty;
                rtb.SelectionFont = new Font(rtb.SelectionFont, FontStyle.Bold);
                rtb.SelectionColor = LogMsgTypeColor[(int)msgtype];
                rtb.AppendText(msg);
                rtb.ScrollToCaret();
            }));
        }



And I have many RichTextBoxes and i want to call the method Log(blabla,blabla,rtbSensor1); It's not working, but I need to know if it's possible to send this kind of parameter, or if someone have a better idea...

Is This A Good Question/Topic? 0
  • +

Replies To: Sending a component as parameter to a method

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Sending a component as parameter to a method

Posted 09 September 2010 - 06:46 AM

It's possible, can you define "not working"?

It's probably best practice for you to change the delegate you're passing from EventHandler to one of your own custom types, which includes the same parameters as the Log method, being LogMsgType and msg. You can then pass those 2 in as parameters to .Invoke().

I'm sorry if that doesn't make much sense, but reading about delegates and specifically Invoke on MSDN will help you out quite a lot.

This post has been edited by RudiVisser: 09 September 2010 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Sending a component as parameter to a method

Posted 09 September 2010 - 06:59 AM

Don't try to send the messagebox as part of the parameters. Just raise an event with the message. You can wire that event to whatever controls you like.

        // Send the text to be logged as the object
        public event EventHandler LogThis;

        public void RaiseLog(string MessageToLog)
        {
            if (LogThis != null)
            {
                string Message = string.Format("{0} - {1}: {2}", DateTime.Now.ToString("HH:mm:ss"), Application.ProductName, MessageToLog);
                LogThis(Message, EventArgs.Empty);
            }
        }


// How to use it

        Public void Form1_Load()
        {
           this.LogThis += LogHandler; // wire the event to a handler method
        }

        void LogHander(object message, eventargs e)
        {
           // in this case the object is the message not the sending object
           rtbLogRTB.Append((string)message); // cast the object back to a string.


        void MyFancyMethod()
        {
           RaiseLog("Start Fancy Method");
           // Do fancy stuff
           RaiseLog("Fancy stuff complete");
        }


This post has been edited by tlhIn'toq: 09 September 2010 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Sending a component as parameter to a method

Posted 09 September 2010 - 08:01 AM

That won't work as, I assume (from the fact he's using Invoke) that he's trying to call it from a different thread.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Sending a component as parameter to a method

Posted 09 September 2010 - 08:17 AM

you could do it as an extension method...

// this would need to be in a static class
public static void Log(this RichTextBox rtb, LogMsgType msgtype, string msg)
{
    rtb.Invoke(new EventHandler(delegate
    {
          rtb.SelectedText = string.Empty;
          rtb.SelectionFont = new Font(rtb.SelectionFont, FontStyle.Bold);
          rtb.SelectionColor = LogMsgTypeColor[(int)msgtype];
          rtb.AppendText(msg);
          rtb.ScrollToCaret();
     }));
}



then you could call it like this...

rtbSensor1.Log(myMsgType, "blahblah");



now as for "not working", we need a little more information as to what is happening vs. what you are expecting to happen.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Sending a component as parameter to a method

Posted 09 September 2010 - 08:24 AM

View PostRudiVisser, on 09 September 2010 - 07:01 AM, said:

That won't work as, I assume (from the fact he's using Invoke) that he's trying to call it from a different thread.


I've had no problem doing this from across threads. That's why I do it this way. There is no attempt to directly affect a control created on a separate thread. Event is raised in one class/thread and responded to in another. The subscriber just has to be on the same thread as the controls were created. If it weren't, then he has bigger design issues.

Whichever thread has the controls can subscribe to the event.
Was This Post Helpful? 1
  • +
  • -

#7 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Sending a component as parameter to a method

Posted 09 September 2010 - 10:51 AM

This works for me:

using System;
using System.Windows.Forms;
using System.Threading;

namespace RtfThreadTest
{
    public partial class Form1 : Form
    {
        public enum LogMsgType { General };

        public Form1()
        {
            InitializeComponent();
        }

        private delegate void MsgDelegate(RichTextBox rtb, LogMsgType msgtype, string msg);

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(PrintDate));
            th.Start();
        }

        private void PrintDate()
        {
            Log(richTextBox1, LogMsgType.General, DateTime.Now.ToString());
        }

        private void Log(RichTextBox rtb, LogMsgType msgtype, string msg)
        {
            // do we need to switch threads?
            if (InvokeRequired)
            {
                MsgDelegate method = new MsgDelegate(Log);                
                Invoke(method, rtb, msgtype, msg);
                return;
            }

            // and update
            rtb.Text += string.Format("[{0}]: {1}\r\n",
                msgtype.ToString(), msg);

            /* this is where your code would go */
        }
    }
}


Read more at http://imdsm.blogspo...-made-easy.html

This post has been edited by Imdsm: 09 September 2010 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 rapo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 27-November 08

Re: Sending a component as parameter to a method

Posted 10 September 2010 - 12:38 AM

I've made my version and it works, I asked there how to do it to know which is the most accurate method. I will try this method also:

View Posteclipsed4utoo, on 09 September 2010 - 07:17 AM, said:

you could do it as an extension method...

// this would need to be in a static class
public static void Log(this RichTextBox rtb, LogMsgType msgtype, string msg)
{
    rtb.Invoke(new EventHandler(delegate
    {
          rtb.SelectedText = string.Empty;
          rtb.SelectionFont = new Font(rtb.SelectionFont, FontStyle.Bold);
          rtb.SelectionColor = LogMsgTypeColor[(int)msgtype];
          rtb.AppendText(msg);
          rtb.ScrollToCaret();
     }));
}



then you could call it like this...

rtbSensor1.Log(myMsgType, "blahblah");



now as for "not working", we need a little more information as to what is happening vs. what you are expecting to happen.

Thank you to all.

This post has been edited by rapo: 10 September 2010 - 12:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1