|
Hi,
I've a tab control that has for ex. 2 members at the TabPages.
The first calles "one and the other "second".
i want to check if at my TabPages i have already a tab with some name
and if not' i want to add new one with that name.
this is my foreach loop:
foreach (TabControl item in tabControl1.TabPages)
and this my my code to add new tab:
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = "one";
tabControl1.TabPages.Add(tab);
Someone have an idea how to do this?
|
|
|
|
|
Did not get any idea, what you want actually..
|
|
|
|
|
like in a chat,
if a user get a private massage it opens a new tab,
but if the tab is already open the message written in the open (exist) one...
|
|
|
|
|
Probably you have to change your foreach data type. Verify the following code and let me know if you need any further details.
bool exist = false;
foreach (TabPage page in this.tabControl1.TabPages)
{
if (page.Text == "one")
{
exist = true;
break;
}
}
if (!exist)
{
}
|
|
|
|
|
If I understand correctly what you are asking, you can do something like:
private void AddTabPageIfNotPresent(string TabName, TabControl Control)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Text == TabName) return;
}
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = TabName;
Control.TabPages.Add(tab);
}
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
right. And better yet the method should return the TabPage, either the matching one or the new one.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Yep, after reading Tamir's answer to Arindam's post it's clear that he needs to either create a new tab (and return it) or return the existing one:
private TabPage GetOrAddTabPage(string TabName, TabControl Control)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Text == TabName) return item;
}
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = TabName;
Control.TabPages.Add(tab);
return tab;
}
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
HI,
I ment when a user reveic a private message a new tab opens,
unless there is already one open.
i can't do it...
|
|
|
|
|
Hi, well the answer is in my previous post: have a look at the GetOrAddTabPage function, it will give you an idea about how you can do that.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Moreno Airoldi wrote: have a look at the GetOrAddTabPage function, it will give you an idea about how you can do that.
I have found the Add function (tab.Controls.Add),
but i can't find the Get function.
only:
tab.Controls.GetChildIndex
tab.Controls.GetEnumerator
tab.Controls.GetHashCode
tab.Controls.GetType
what did you exactly ment?
|
|
|
|
|
The function I showed takes care of creating and/or getting the tab you need:
private TabPage GetOrAddTabPage(string TabName, TabControl Control)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Text == TabName) return item;
}
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = TabName;
Control.TabPages.Add(tab);
return tab;
}
You can use it in your code this way:
TabPage UserPage = GetOrAddTabPage(UserName, MyTabControl);
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Hi
i want to export my datagridView to excel. i found some example to accomplish this like this[^] and this[^]
but at runTime i got this error :
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
for solving this problem i found this link[^] but my problem does not solved. can anybody give me a better way to convert my datagridview to excel without any error ?
thanks
|
|
|
|
|
Try this[^]
PLease inform if any errors
|
|
|
|
|
Thanks Tamer
i try your component, but in this line :
extendedDataTableObject.ToExcel(@"c:\test.xlsx");
i got this exception :
Exception Occured While Generating Excel Document
my another goal is to save my dataTable in excel 2003.
|
|
|
|
|
Hello there,
I wrote a tool for importing and exporting data into ExcelSheets (better said: workbooks).
Some relevant parts of my code are:
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
excelApp.ScreenUpdating = false;
excelApp.DisplayAlerts = false;
then I open some workbooks, open some sheets, write some stuff into them and close all of them. I also dereference them if they are no more needed (mysheet = null).
But after finally shutting down the ExcelApp doing so:
excelApp.Quit();
excelApp = null;
... I have to wait quite a long time (about half a minute) until I see the EXCEL-thread diappear in my ProcessExplorer.
The problem is, that I can not open the just written xls-files if the application did not end, because it still has got its hands on the files.
So I have to know when this happens.
My questions are:
Why does it take so long?
When does the Excel-thread really end?
How do I end it correctly?
How can I provoke the thread to be ended?
Or is it just a matter of garbage collection? (a field I did not yet explore)
Thanks in advance,
Harry
no plan,
no signature
|
|
|
|
|
Put simply, there is no way to completely shut down Excel (close the task so that you don't see it in the task list anymore) when you want to. It's a COM<->gargabe collector issue.
In earlier versions of the framework, it could be done with a direct call. GC.Collect() after quitting and nulling, but starting from 2.0 or 3.0 (can't remember cleary) it's no use.
The bright side is if you open more than one Excel app, only one will stay in memory for a long time, while all others will close immediately.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Thanks a lot!
But I've still got one question left. Is there any event I can handle when the Excel Application shut down completely. I mean: I can wait some seconds but is there something I can respond to, because I want to know when I can open the xls-file?
Greets
Harry
no plan,
no signature
|
|
|
|
|
Nope, no event you can handle. You might get a list of the running processes and check when "excel" isn't there anymore, but nothing will let you handle properly the case where the user opens other instances of Excel.
As for opening the xls file, you will be able to do that safely once you call Close on it (on the corresponding Workbook object), so that won't be a problem.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Moreno Airoldi wrote: As for opening the xls file, you will be able to do that safely once you call Close on it (on the corresponding Workbook object), so that won't be a problem. Smile
Unfortunately not!
I tried opening the xls file I just wrote after myWorkbook.Close(), after myWorkbook = null, and even after myFileInfo = null. But doing so, Excel AND my little application will end up in "not responding". Maybe I can collect some garbage in order to get rid of the unreferenced workbook. But - Hey! - I do this at the end, when I wrote all the excelfiles I have to and finally end the application.
And so I still think, the best point to let anyone open the new files is when I closed and cleaned all up.
Greets
Harry
no plan,
no signature
|
|
|
|
|
Then there must be something else in your code which prevents the workbook to be released.
Consider the following example:
Microsoft.Office.Interop.Excel.Application myApp = null;
Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
try
{
myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
myApp.AskToUpdateLinks = false;
myApp.DisplayAlerts = false;
myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
myWorkbook.SaveAs("D:\\test.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
System.Type.Missing, System.Type.Missing, false, false,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (myWorkbook != null)
{
myWorkbook.Close(false, "", null);
myWorkbook = null;
}
if (myApp != null)
{
myApp.Quit();
myApp = null;
}
}
System.Diagnostics.Process.Start("D:\\test.xls");
The created workbook is successfully opened right after it's Save d and Close d (please note that Process.Start will leave you with yet another open Excel process in the task list).
I've been using this kind of interop a few times, and I assure you it works fine. I'll be more than glad to try and help you more if you want.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
|
Yes you are right! Calling Process.Start() before Application.Quit() won't work properly. I made some tests and I confirm it.
The problem is in how Excel optimizes opening documents. Consider the following example (derived from my previous):
Microsoft.Office.Interop.Excel.Application myApp = null;
Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
try
{
try
{
myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
myApp.Visible = true;
myApp.AskToUpdateLinks = false;
myApp.DisplayAlerts = false;
myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
myWorkbook.SaveAs("D:\\test.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
System.Type.Missing, System.Type.Missing, false, false,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
}
finally
{
if (myWorkbook != null)
{
myWorkbook.Close(false, "", null);
myWorkbook = null;
}
}
System.Threading.Thread.Sleep(3000);
System.Diagnostics.Process.Start("D:\\test.xls");
System.Threading.Thread.Sleep(3000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (myApp != null)
{
myApp.Quit();
myApp = null;
}
}
You will clearly see that since one Excel task is still active (the one we instanced for opening and saving the workbook), the call to Process.Start() will cause it to be the container for the re-opening of the document. Since we call Application.Quit() right after that, we close the workbook! This "task optimization" is an Excel internal, so I doubt there's any way to bypass it.
All the other differences between your code and my example don't seem to count: it's all basically the same.
I think you'll just have to call Application.Quit() before opening the file. There's probably no way to avoid that (unless you use another application - not Excel - to show the file...).
If you are working on multiple documents, and you want to open each saved file while processing the next, you may consider opening a different Excel.Application for each single one. There will sure be an overhead, but you can make some tests and see if it's acceptable (it probably is).
This (brutal) example works fine:
for (int i = 1; i < 6; i++)
{
Microsoft.Office.Interop.Excel.Application myApp = null;
Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
try
{
try
{
myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
myApp.Visible = true;
myApp.AskToUpdateLinks = false;
myApp.DisplayAlerts = false;
myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
myWorkbook.SaveAs("D:\\test" + i.ToString() + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
System.Type.Missing, System.Type.Missing, false, false,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
}
finally
{
if (myWorkbook != null)
{
myWorkbook.Close(false, "", null);
myWorkbook = null;
}
if (myApp != null)
{
myApp.Quit();
myApp = null;
}
}
System.Diagnostics.Process.Start("D:\\test" + i.ToString() + ".xls");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Good luck, let me know how it goes!
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
|
Have a nice one, man!
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|