|
Hi, Dave's (the next) answer is exactly what I would have suggested.
Still I don't understand when you say the text comes from VB.NET controls - would that mean you would first have to grab the text from a different program? In that case it's a much (muchmuch!) harder job to do, so I hope I'm misinterpreting that remark . If you wanted to say "VBA controls" instead (i.e. inside Excel) then again it's something you can manage without going insane
|
|
|
|
|
While working in excel directly not via code, you can use ALT+Enter keystroke to achieve this.
While using code it is as simple as inserting a Carriage Return and Line feed into the string.
Private Sub doSomething()
Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlWk As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add()
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = xlWk.Worksheets(1)
xlApp.Visible = True
xlSheet.Range("A1").Value = "Some Text Value on 1 line"
xlSheet.Range("A5").Value = "Some text value on" + vbCrLf + "2 lines"
xlSheet.Columns().AutoFit()
xlSheet.Rows().AutoFit()
End Sub
|
|
|
|
|
Hi,
Is it possible to create a new instance of a control if I only has the type of it?
E.g. Type of Button -> new instance of button
|
|
|
|
|
What other information do you think you might need?
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
look for Activator.CreateInstance(), that works a lot easier than full reflection, where you would invoke a constructor.
|
|
|
|
|
Thanks
|
|
|
|
|
Hi all,
I'm using a BackgroundWorker to create a file list. It's started with BG1.RunWorkerAsync() in the Changed-Event of a form control, and for the purpose of testing I show a messagebox at the end of the BG1.RunWorkerCompleted event. This works fine.
But it doesn't seem so easy to re-use the same BackgroudWorker for the same task. Wanting to start the same BGW again (in another controls Changed-event) and with the same command (BG1.RunWorkerAsync() ) throws an InvalidOperationException telling me that the respective BackgroundWorker was still busy and couldn't handle multiple tasks at a time.
What's wrong here? I'm sure the messagebox is the last command in the RunWorkerCompleted event, and it has of course been closed before clicking the control which is supposed to restart the BGW. Would I have to dispose of the BGW and create it newly?
Thank you for advice,
Michael
|
|
|
|
|
Hi,
I've never felt a need to reuse an existing BackgroundWorker; once it is done, I forget about it; and I create a new one when needed. The expensive part of a BGW is the thread itself (and its associated stack), however that thread is borrowed from the ThreasPool so it gets recycled in an inexpensive way.
|
|
|
|
|
Didn't you use this condition If BG1.IsBusy = False Then before call BG1.RunWorkerAsync() ?
Use <pre lang="vb"> Visual Basic Code Here.</pre>
|
|
|
|
|
Thank you for having a look at this old - yet unresolved - thread.
I had thought about what you suggest, but checking IsBusy this way would just skip the task, since I found out that BG1 is busy in the respective moment. So, considering my intention to use the BG1 for a similar task again, the only way to use the IsBusy flag could be looping until the flag would become false. Not knowing how long BG1 would be busy after 'RunWorkerCompleted' (which was actually my question), this seemed to be a waste of time - so I started experimenting with the ThreadPool, which worked but is a different chapter again because of an obviously huge overhead.
If you're experienced in this field, I'd really appreciate an idea how to simply copy a filelist as fast or even faster using .NET than using a DOS batch file.
Thanks
Michael
|
|
|
|
|
Hi,
Just watch the following example & mark the point where is your problem.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim BG As New System.ComponentModel.BackgroundWorker
BG.WorkerReportsProgress = True
AddHandler BG.DoWork, AddressOf BackgroundWorker_DoWork
AddHandler BG.RunWorkerCompleted, AddressOf BackgroundWorker_RunWorkerCompleted
AddHandler BG.ProgressChanged, AddressOf BackgroundWorker_ProgressChanged
Static Cnt As Integer = 1
BG.RunWorkerAsync(Cnt)
Cnt = Cnt + 1
End Sub
Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim stp As New Stopwatch
stp.Start()
Do
If stp.ElapsedMilliseconds Mod 1000 = 0 Then
CType(sender, System.ComponentModel.BackgroundWorker).ReportProgress(stp.ElapsedMilliseconds, e.Argument)
Threading.Thread.Sleep(100)
End If
Loop
End Sub
Private Sub BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
TextBox1.AppendText(e.UserState.ToString & "->" & e.ProgressPercentage & ", ")
End Sub
Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
End Sub
End Class
Michael Schäuble Wrote:
If you're experienced in this field, I'd really appreciate an idea how to simply copy a filelist as fast or even faster using .NET than using a DOS batch file.
Follow the link to copy files using explorer.exe without any testion. It is much more faster than .NET can do. All of the APIs & Constants are not needed. If you want handle copying files yourself you should use binary file read and write operation. That is much faster than DOS batch or Explorer. Like time and space complexity, you should choose what to do. Develop binary file copyer OR use Explorer or .NET Built-in classes.
http://www.codeproject.com/Messages/3526402/Re-Is-it-the-right-approach-to-speed-up-the-copy-p.aspx[^]
Use <pre lang="vb"> Visual Basic Code Here.</pre>
|
|
|
|
|
I am trying to store a word document to an image field in SQL. I get the file to send to SQL using the first code below. The problem is that when I try to load the field into a Word document using the second code, it is corrupted. I am thinking that it has something to do with encoding, but not sure. Can someone please help? Thanks in advance.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
openFileDialog1.Title = "Save File to SQL"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
Dim MyData(myStream.Length) As Byte
myStream.Read(MyData, 0, (myStream.Length))
Me.TowerTableAdapter.Add_LOTO(MyData, Me.Monsanto_Equip_NoTextBox.Text.ToString)
Me.TextBox3.Text = myStream.Length
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim axWord As Word.Application
axWord = New Word.Application
axWord.Visible = True
Dim axDoc As Word.Document
axDoc = axWord.Documents.Add
Dim strText As String
strText = axDoc.Paragraphs(1).Range.Text
Dim conn = New SqlConnection(My.Settings.Maintenance_ManagementSQLConnectionString)
Dim EQ As String = Me.Monsanto_Equip_NoTextBox.Text.ToString
Dim AreaCMD As New SqlCommand()
AreaCMD.Connection = conn
AreaCMD.CommandText = "SELECT LOTO FROM [tower]where [monsanto equip no] = '" & EQ & "'"
conn.Open()
Dim AR As SqlDataReader = AreaCMD.ExecuteReader(CommandBehavior.SequentialAccess)
While AR.Read()
Dim binaryStream As SqlBinary = AR.GetSqlBinary(0)
Dim test As String = System.Text.Encoding.Unicode.GetString(binaryStream)
axDoc.Paragraphs(1).Range.Text = test.ToString
End While
conn.Close()
End Sub
|
|
|
|
|
Hi,
you can store binary data in an SQL field of type BLOB (and maybe others as well). It doesn't really matter what the meaning of the data is, all you can do is store it, and later retrieve it in basically the same way.
For a Word file that would mean getting all the bytes from a file, and writing them to the database; and later getting them from the database, and recreating the Word file. No more, no less.
I can't really see how you're writing to the database, as this is hidden inside the Me.TowerTableAdapter.Add_LOTO() method. I guess it is pretty straightforward.
However I have never seen a way to feed the file content as a stream to Word. So in my books the part containing
Dim test As String = System.Text.Encoding.Unicode.GetString(binaryStream)
axDoc.Paragraphs(1).Range.Text = test.ToString
is a no no.
Write the bytes to a new, possibly temporary file, then open that file with Word, probably using the Process.Start() method.
|
|
|
|
|
Hi Luc,
I have also tried this code that I think is what you are talking about. When I try to open the Word doc that is in the temp folder, it tells me that it is corrupt. It does however do a recovery and the document opens fine.
Dim conn = New SqlConnection(My.Settings.Maintenance_ManagementSQLConnectionString)
Dim EQ As String = Me.Monsanto_Equip_NoTextBox.Text.ToString
Dim AreaCMD As New SqlCommand()
Dim wordDoc As Byte()
AreaCMD.Connection = conn
AreaCMD.CommandText = "SELECT LOTO FROM [tower]where [monsanto equip no] = '" & EQ & "'"
conn.Open()
wordDoc = AreaCMD.ExecuteScalar()
File.WriteAllBytes("C:\Temp\WordDoc.docx", wordDoc)
|
|
|
|
|
A simple check: do the input and output file have the same length (when going file1 -> DB -> file2)?
Not sure what is wrong, however you could verify, without using Word, whether your file remains identical when going from file to database and back to file.
And why is it you use an explicit Open() and Read(), you could do a File.ReadAllBytes() exactly the same as in your WriteAllBytes().
|
|
|
|
|
Luc,
I know that this is probably not the proper way to handle this, but it does work. I set the open and repair to true on the open document. Thanks for your help!
Dim conn = New SqlConnection(My.Settings.Maintenance_ManagementSQLConnectionString)
Dim EQ As String = Me.Monsanto_Equip_NoTextBox.Text.ToString
Dim AreaCMD As New SqlCommand()
Dim wordDoc As Byte()
AreaCMD.Connection = conn
AreaCMD.CommandText = "SELECT LOTO FROM [tower]where [monsanto equip no] = '" & EQ & "'"
conn.Open()
wordDoc = AreaCMD.ExecuteScalar()
File.WriteAllBytes("C:\Temp\WordDoc.docx", wordDoc)
Dim appWord As New Word.Application
Dim docWord As New Word.Document
docWord = appWord.Documents.Open("C:\Temp\WordDoc.docx", , True, , , , , , , , , , True, , , )
appWord.Visible = True
|
|
|
|
|
Jason, please make use of the code block pre tag when posting code, it makes it easier to read and formats it also if you set the attribute lang="vb"
see;
Dim conn = New SqlConnection(My.Settings.Maintenance_ManagementSQLConnectionString)
Dim EQ As String = Me.Monsanto_Equip_NoTextBox.Text.ToString
Dim AreaCMD As New SqlCommand()
Dim wordDoc As Byte()
AreaCMD.Connection = conn
AreaCMD.CommandText = "SELECT LOTO FROM [tower]where [monsanto equip no] = '" & EQ & "'"
conn.Open()
wordDoc = AreaCMD.ExecuteScalar()
File.WriteAllBytes("C:\Temp\WordDoc.docx", wordDoc)
Dim appWord As New Word.Application
Dim docWord As New Word.Document
docWord = appWord.Documents.Open("C:\Temp\WordDoc.docx", , True, , , , , , , , , , True, , , )
appWord.Visible = True
|
|
|
|
|
You're welcome.
|
|
|
|
|
I am running up against a wall on this.
I am trying to save modified screen grab image objects as monochrome tiff files. I am isolating a particular color and making the image monochrome.
Dim imgProc As Image
'grabs portion of entire screen
imgProc = CropBitmap(SrcImg, rect)
'sharpens image to simplify isolation
imgProc = SharpenImage(imgProc, dChatSharpSetting)
'isolates particular color to make color monochrome
imgProc = IsolateImageColor(imgProc, ChatAlphaSetting, cChatIsoColor)
No matter what I do after this point, I have yet to figure out how to save the image properly. I come out with 32 bit tiff files (seems to be a default).
I am sure this is gotta be an oversight solution. Any help would be appreciated.
|
|
|
|
|
Hi,
I'm not sure what these methods are. Do they belong to some CodeProject article? if so, you might want to ask your questions there, in the forum underneath the article. Are you using some library? if so, you should tell us, and provide a link to it. Or did you develop them yourself?
A .NET Image is actually a Bitmap (unless it is a vector drawing); Bitmaps use one of several PixelFormats; and Bitmap.Save() knows how to generate different ImageFormat file types, although not all combinations are supported.
If you start from a general-purpose color image and want only a single color component, it makes most sense to switch to a more specific PixelFormat (provided Save is accepting it for the .Tiff format you want).
How is IsolateImageColor() going about? If it is yours, you'll have to provide more information here.
|
|
|
|
|
My apologies. new to posting problems the functions listed were ones I wrote specific to the program I am writing to give a history to the image. Basically through the three calls it cuts out a portion of a bigger image, then minimizes the shading, and then does a monochrome image based on a user-defined color to isolate. This gives me a monochrome image I am wanting to put through Tesseract via a different program. I am running into a problem in that tesseract only supports from 1 bpp (monochrome) to 8 bpp (256 color). My images keep coming out at 32bit when I try to save images as tiff images. I was wondering if there is a way to force the color depth through the Image class, or some other way I havent found?
|
|
|
|
|
There are a couple of Bitmap constructor overloads that take a PixelFormat parameter; you should use one of them when going from color to grayscale. If you simply modify an existing Bitmap, it will retain its original PixelFormat, so even if you were to turn all pixels of a color image to some shade of gray, it would still be a color image technically. I must warn again, first check Bitmap.Save takes the PixelFormat,ImageFormat combination you choose.
|
|
|
|
|
I have thought that I posted this a few days ago but cannot see it.
We are planning our migration from XP VisStudio 2005 (VB.NEt) to either Windows7 2008 or 2010 (probably 2010 if it is workable) but have some startup issues.
It seems that in VS 2005 if you had an execution error during IDE testing it stops and gives you an error msg if no Try/Catch. In 2008 and 2010 it just returns from the method.
e.g.
Sub forms_load ...
Dim i as short = 0
i = 1 / i (Error Divide by zero error - Overflow Exeption)
i = i
end sub
In VS 2005 would stop on the Error. In 2008 and 2010 no message and the form is just displayed.
The i = i is not executed.
This must be some setting or directive that we have overlooked.
When taking that same project from 2005 and opening in 2010 the results where strange. It converted then when tried to run in IDE it terminated. On retry it would not step through and seems all screwed up. I tries the bad instruction and then says not working.
Is 2010 stable and usuable to use?
|
|
|
|
|
|
Hi,
1.
you can reach a list of your latest messages through your profile page; see here[^]. The message you have somehow lost is now the second on that list.
2.
Your question was asked and answered two days ago.
3.
If you were getting strange results, I haven't found a reason yet to blame VS2010.
|
|
|
|
|