none
RichTextBox.BackColor Cannot implicitly convert type 'int'/'double' to 'System.Drawing.Color' RRS feed

  • Question

  • Dear Colleagues,

    While I attempt to have the backcolor of a RichTextBox be reflecting the one of a cell the following piece of code fails with the relevant errors (If I use without the Try/Exception clause) as provided in form of comments.

    private void button1_Click(object sender, EventArgs e)
    {
    
        try { richTextBox1.BackColor = Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.Color; } catch (Exception) { MessageBox.Show("1. Sorry An Error"); }
    
    /*
    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException occurred
      HResult=0x80131500
      Message=Cannot implicitly convert type 'double' to 'System.Drawing.Color'
      Source=<Cannot evaluate the exception source>
      StackTrace:
    <Cannot evaluate the exception stack trace>
    */
    
        try { richTextBox1.BackColor = Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.ColorIndex; } catch (Exception) { MessageBox.Show("2. Sorry An Error Again"); }
    
    /*
    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException occurred
      HResult=0x80131500
      Message=Cannot implicitly convert type 'int' to 'System.Drawing.Color'
      Source=<Cannot evaluate the exception source>
      StackTrace:
    <Cannot evaluate the exception stack trace>
    */
    
    }

    Please see if you can help in this regard by highlighting the error I am making and the suggesting the correct mode I should be using.

    Thanks in advance.




    Wednesday, January 17, 2018 4:51 AM

Answers

All replies

  • You need the function ColorTranslator.FromOLE()

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, January 17, 2018 2:51 PM
    Moderator
  • Sorry for replying late, Cindy.

    Thanks a lot for introducing me to the said function. However, I find the Color Translator not to be working correctly enough. While I use the following piece of code I find that it does change to color of the RichTextBox & a TextBox also, but the same resulting in shades & color different than the one of the cell selected.

    An example like the unfilled cell being selected leads to have the box be colored in light/sky blue, while a yellow is resulting into a black backcolor of the box?

    Please see if you can find out & highlight the error I am making in the following piece of code and suggest the changes in form of a code:

    private void button1_Click(object sender, EventArgs e)
    {
    
        MessageBox.Show("1st Attempt");
    
        try { richTextBox1.BackColor = ColorTranslator.FromOle(Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.ColorIndex); }
        catch (Exception) { MessageBox.Show("Sorry Error 1!"); }
    
        MessageBox.Show("2nd Attempt");
    
        try { richTextBox1.BackColor = ColorTranslator.FromOle(Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.ColorIndex); }
        catch (Exception) { MessageBox.Show("Sorry Error 2!"); }
    
        MessageBox.Show("3rd Attempt");
    
        try { textBox1.BackColor = ColorTranslator.FromOle(Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.ColorIndex); }
        catch (Exception) { MessageBox.Show("Sorry Error 3!"); }
    
        MessageBox.Show("4th Attempt");
    
        try { textBox1.BackColor = ColorTranslator.FromOle(Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.ColorIndex); }
        catch (Exception) { MessageBox.Show("Sorry Error 4!"); }
    }
    

    Friday, January 19, 2018 4:50 AM
  • Well,

    I guess I finally did find out a post relevant enough:

    https://stackoverflow.com/questions/945808/how-to-convert-excel-range-interior-color-to-system-drawing-color-in-c

    a change like the following did resolve the problem:

    try { richTextBox1.BackColor = ColorTranslator.FromOle((int)((double) Globals.ThisAddIn.Application.InputBox(Prompt: "Select Sample Cell With Color", Type: 8).Cells[1].Interior.Color)); }
    catch (Exception) { MessageBox.Show("Sorry Error 1!"); }
    

    Friday, January 19, 2018 5:13 AM
  • richTextBox1.BackColor=Color.Red;

    n.Wright

    Thursday, January 25, 2018 1:56 PM