Reputation: 4480
I am trying to add a group of textboxes to a list. I keep getting an error cannot convert from 'System.Windows.Forms.TextBox' to 'Microsoft.Office.Interop.Excel.TextBox'. Not sure why I am getting this error. Do I need to add a cast? Here is my code. The textboxes I want to add are in getValues. I haven't worked a lot with windows forms so this error is a mystery for me.
class DiagnosticsExcelFile: ConfigForm
{
string fileName = "";
private ConfigForm configForm;
Excel.Application xlApp = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
List<TextBox> list = new List<TextBox>();
public DiagnosticsExcelFile(ConfigForm config)
{
fileName = "Z:\\UCA\\Diagnostics.xlsx";
this.configForm = config;
this.list.Add(this.configForm.HighOffSetX); // get error here
}
private void getValues()
{
this.configForm.HighOffSetX.Text = "5.0";
this.configForm.HighOffSetY.Text = "5.0";
this.configForm.HighOffSetZ.Text = "5.0";
}
}
Upvotes: 0
Views: 372
Reputation: 1
Your definition is correct. The problem occurs when adding new members to the list. Try the following:
list.Add(HighOffSetX);
You dont need the whole "this" stuff at this point.
Upvotes: 0
Reputation: 223217
Define your list like:
List<System.Windows.Forms.TextBox> list = new List<System.Windows.Forms.TextBox>();
Currently your List
is referring to TextBox from Microsoft.Office.Interop.Excel.TextBox
.
Both Microsoft.Office.Interop.Excel=
and System.Windows.Forms
has a class TextBox
, since you haven't shown your using directives, I assume you have
using Microsoft.Office.Interop.Excel;
and you don't have a using directive for System.Windows.Forms
, therefore your code is considering your List
to contain TextBoxes from Office Interop. (By the way, if you have both references, then you will get an error for ambiguous reference to TextBox
)
By explicitly defining System.Windows.Forms.TextBox
, your error will go away.
Upvotes: 3
Reputation: 12073
using TextBox = System.Windows.Forms.TextBox;
I usually find this approach the most efficient. It doesn't require many code changes and allows to keep lines short.
Upvotes: 3
Reputation: 3813
You're not specifying a full namespace, but you probably have:
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
in your code file.
The compiler can't tell which TextBox you mean. You can fully qualify each type name, or alias one of the using
s.
using xl = Microsoft.Office.Interop.Excel;
Then you would refer to the Excel Textbox as xl.TextBox. Specifying just TextBox
would give you the System.Windows.Forms
one that you expect.
Upvotes: 1
Reputation: 23793
You are hiding the TextBox type from System.Windows.Forms.TextBox (maybe with a using TextBox = Microsoft.Office.Interop.Excel.TextBox).
Try declaring your list with:
List<System.Windows.Forms.TextBox> list = new List<System.Windows.Forms.TextBox>();
Upvotes: 0