|
hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow();
frm.Show();
}
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow();
frm.ShowDialog();
}
|
|
|
|
|
I think your problem may be because using Form.Show() you have two threads running UI displays, and they are probably interfering with each other. Whereas ShowDialog() stops all activity until the dialog is dismissed by user interaction. You should put all your UI code (showing and manipulating forms) in your main thread and use the background thread(s) to do other work.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
thank you very much. At least, I got to know that all UI actions must be executed within the same thread.
|
|
|
|
|
You should create and access all GUI parts from the main thread; the ProgressChanged and RunWorkerCompleted handlers of a BGW do run on the main thread (provided the main thread was used to create the BGW!). So maybe you could create the one Form in the ProgressChanged handler.
If not, look here[^] for a recommended way to get GUI things done from another thread.
|
|
|
|
|
On a technical level, you need to marshal this into the UI thread, as the others said. (The reason it's hanging is because there is no message loop in that thread.) But if you find yourself doing that there's a good chance that your design is in need of revision; you shouldn't generally be doing direct updates of the UI from a worker thread anyway, even if it did work correctly. Almost by definition, a background thread is business logic, and you should always keep that separate from UI.
A better solution is to have your background worker signal events (either the built in progress event or new events that you define), and have the UI hook onto them and update itself. Note that these events are dispatched in the context of the background thread so you need to use Invoke or BeginInvoke in the handlers to update the UI.
|
|
|
|
|
Alright, I have two questions but this is the first on my list. Currently I am developing a custom control which I plan to create in a manner similar to Microsoft Office Word's documents. I have a custom control (derived from the Panel control) which contains a RichTextBox control. The text box is larger in size (8.5" x 11" sheet, actually) and so it cannot display entirely on the screen at once. The Panel has AutoScroll set to true, which works as expected.
However, I would like to capture mouse wheel events inside the RichTextBox control on the panel and scroll the Panel accordingly. I have hooked the "MouseWheel" event from my Panel class. This works fine as well. I used Debug.WriteLine inside the event handler to ensure that the event was being caught, in which case it was. However, I could not get the panel to scroll. I tried changing the Panel's AutoScrollPosition as well as a few other properties, calling some methods, etc., but to no avail.
I looked online, via Google Search, and I had no luck there. How can I scroll the panel programmatically?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Are you calling the Panel's 'Focus()' method in the Panel MouseWheel Event ? This is a frequently reported requirement in discussions of this issue I've seen before.
But, perhaps that's not useful here since you may not wish to lose Focus from the RTF document ?
Possibly this may be helpful:[^]
Unfortunately, my mouse-wheel has never worked, so I can't have a go at this interesting challenge.
best, Bill
"I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone." Bjarne Stroustrop circa 1990
|
|
|
|
|
Thank you but that did not do the trick. I even tried to give the Panel focus regardless of whether it would take away from the document. Still no luck. The only solution I have come up with is detecting the mouse wheel and moving each "page" up/down using "page.Top += e.Delta". It works fairly well. The only downfall is that the panel's (auto)scrollbar gets smaller as I scroll DOWN the document. And it will scroll practically infinitely past the beginning/end of the document. But until I resolve this, that will suffice. I have another question but I will be placing it in another thread.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
The chances are that you won't get Panel.MouseWheel events - instead, you will get RichTextBox.MouseWheel events, particularly if the RTB has the focus or is docked to fill the panel.
Also note that the RTB will have it's own scroll bars - so scrolling the panel is likely to just scroll the RTB off the screen...
In my case, the mouse wheel scrolls the RTB automatically, with no code required at all.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
I am aware of the RTB receiving the MouseWheel event. That is the event that I hooked. I DO want the entire panel to scroll. The RTB is a fixed size and does not fit the entire screen at once. I do not want the RTB to scroll. I am trying to somewhat mimic Microsoft Word, with multiple "pages" and you scroll through them. I have already removed the scroll bars from the RTB.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Using COM interop with Excel|Ace 12 driver
Short background: Vendor delivers excel spreadsheet to be imported into our system. We had an agreed upon interface, and vendor did not meet the interface. I have been called in to shoehorn their data into the process.
Essentially, they put a piece of data in the incorrect column and it needs to be extracted from the existing column into a column of its own. They messed up on the datatype of another column, and TypeGuessRows is causing the Excel engine to infer that the column is numeric when in fact it is text.
If I can COM interop with the spreadsheet, extract the requisite data out of the current column, place it in its proper location within the sheet, then programmatically alter the datatype of the offending column from "General" directly to "Text", our existing process will be able to pick up fine. If not, the changes will be more invasive.
I cannot find any information at all on how to programmatically extract a column nor can I find any information on the interface that would allow me to change the datatype on an existing worksheet.
Does anyone know where I might find this information? Can anyone provide any examples of how either of these 2 tasks might be accomplished?
Would COM interop even be the way to go with this? Can you think of a better way?
"I need build Skynet. Plz send code"
modified 11-Nov-11 13:31pm.
|
|
|
|
|
Couldn't you write a function that uses OleDb to tranfer the column to the correct place and convert the data at the same time. Using OleDb you could treat the data as strings right from the start. This would be much easier than trying to do it with Interop. Then you could use your existing methods as they were meant to.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
How would *you* go about using OleDb to perform this task?
The only thing I can think of would be to use an OleDbDataReader to walk through the sheet and build another that matches the correct format in memory and when finished, save out to the original location.
"I need build Skynet. Plz send code"
|
|
|
|
|
I assumed you knew the name of the sheet and the index of the column the data is currently in and the index of the column you wish the data was in. Then it is merely a case of using a datareader to swop the columns with a data type change in the process.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
oh, I must have described the problem wrong then...
This is contrived, but I think it traps everything:
Sheet looks like this:
ColumnA | ColumnB | ColumnC | ColumnD
1)ColumnA contains data that needs to have a substring extracted and inserted as a new column between the existing ColumnA and ColumnB.
2)ColumnC is the wrong datatype.
maybe I'm just overthinking the problem.
"I need build Skynet. Plz send code"
|
|
|
|
|
never mind...I just needed to actually start writing the code to get the juices flowing
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string foo = reader[0].ToString();
string newcolumn = foo.Substring(foo.IndexOf(":") + 1);
object[] meta = new object[reader.FieldCount];
reader.GetValues(meta);
List<string> list = meta.ToList().ConvertAll(o => o.ToString());
}
}
"I need build Skynet. Plz send code"
|
|
|
|
|
Glad to help, even if it was just to get you thinking on a different track
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Ok...another mental block:
I can read the rows in the sheet, I can access the cells from the row via an OleDbDataReader
...but how do I hook directly into a row in the Excel spreadsheet to update it once I have my update record crafted?
using (OleDbCommand command = connection.CreateCommand())
{
int idx = 0;
command.CommandText = string.Format("select * from [{0}$]", "Report_12251104");
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string foo = reader[0].ToString();
string newcolumn = foo.Substring(foo.IndexOf(":") + 1);
object[] meta = new object[reader.FieldCount];
reader.GetValues(meta);
List<object> list = meta.ToList();
list.Insert(2,newcolumn);
}
OleDbCommand update = connection.CreateCommand();
update.CommandText = "update ??what exactly?? Can I use idx to id a row by number??";
update.ExecuteNonQuery();
idx++;
}
}
"I need build Skynet. Plz send code"
|
|
|
|
|
Hi
I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job
Is there anything else i should be doing to be able to catch errors like this ?
thanks
Simon
[WebMethod]
public DataSet GetOrganisation(int organisationId)
{
DataSet ds = new DataSet();
try
{
.....
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message);
}
catch (ArgumentException argerr)
{
WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message);
}
return ds;
}
|
|
|
|
|
huh? you can't pass anything BUT an int to that method. Its not an optional parameter.
|
|
|
|
|
Notice that it's a web method. If somebody tries to call the web method via a web service, an exception will be thrown, but it can't be caught in that method. There may be another place to catch such errors on the server-side (before the client gets an exception), but I'm not sure.
Somebody in an online forum wrote: INTJs never really joke. They make a point. The joke is just a gift wrapper.
|
|
|
|
|
You mean if somebody builds their own raw XML and calls the method directly? If you use a generated proxy class and its "out of date", the proxy class will handle the exception. Not quite sure what you are trying to accomplish.
|
|
|
|
|
I'm not trying to accomplish anything. I was just clarifying what the OP seems to be trying to get at. Seems like they want to have control over what is returned in the case that a bad request is received (e.g., return a message that details which parameters were missing from the call).
Somebody in an online forum wrote: INTJs never really joke. They make a point. The joke is just a gift wrapper.
|
|
|
|
|
As long as you pass an integer, any integer, to this method no exception will be generated.
What you can do is check for a valid range then throw an exception if the variable passed to this method doesn't fall within that range. This can be done in several ways, hard coded, using Validation Application Block or Code Contracts. It's up to you.
No comment
|
|
|
|
|
Normally, in .NET I create wrapper classes for all of my web methods.
[WebMethod]
public FooBarResult FooBar(FooBarParameters parameters){
FooBarResult result = new FooBarResult();
try{
... do work here
}
catch(Exception e1){
... Perform logic to determine how much of the exception you can return and
... assign that to the result object in a special errors property
}
}
While not perfect in any manner it lets me consistently write web methods and always know how the results are coming back. I do a lot of cross platform WS work which is a real pain because both the Java tools and the .NET tool behave differently. This pattern gives a small semblance of sanity.
|
|
|
|