|
Hi all.
Sorry for the frustration here, but I always dread going to MS for any kind of help anymore, regarding what this-or-that does. For years now, they are extremely spotty in their ability to describe something simply and unambiguously. Below is a perfect example; a paragraph which talks in circles:
"The WithEvents statement and the Handles clause provide a declarative way of specifying event handlers. An event raised by an object declared with the WithEvents keyword can be handled by any procedure with a Handles statement for that event, as shown in the following example:"
Sounds like lawyer talk, so whenever I run into lawyer talk, I try to reword it into an explicit example. So here goes.
"A button's mouse click event raised by a button, declared with the WithEvents keyword can be handled by any procedure with a Handles statement for the button mouse click event..."
That's like saying "A box with a lid, has a lid on it."
It seems to be saying that if I use the WithEvents keyword, I can attach, for example, the statement, "Handles Button3.Click" on to ANY procedure and the procedure will respond to the Button3.Click event. Well...Duh! I can do that withOUT the WithEvents keyword! So what is the difference?
Or did they MEAN to say,
"A user can declare a custom event with the WithEvents keyword. Then, any procedure can use that event just like any other event, by attaching it at the end of a procedure's declaration, with the "Handles" clause."
If so, why use the keyword "WithEvents at all? Why not just,
Public Event MyCustomEvent
End Event
OR, are they trying to say this,
'Using the WithEvents keyword, a class is declared that can contain multiple events, any of which can be used at the end of a procedure's declaration with the "Handles" clause. For example,
Dim WithEvents MyEventsCollection As New EventsCollection1
Class EventsCollection1
Public Event My1stEvent()
Public Event My2ndEvent()
End Class
'Then, any procedure can have a Handles clause at the end of it, which assigns any of the events in the collection,
Private Sub ThisProcedure() Handles EventsCollection1.My1stEvent
End Sub
If THAT is what they mean, why put all of those events in a collection (a Class), instead of just calling them all out individually? That is a lot less work:
Private Sub ThisPrcoedure() Handles My1stEvent, My2ndEvent
End Sub
Please help me straighten this out, since I am obviously missing something really important about WithEvents that MS can't explain without ambiguity.
|
|
|
|
|
treddie wrote: If so, why use the keyword "WithEvents at all? Why not just,
"VB6"
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Do you mean that it is a left-over from vb6?
|
|
|
|
|
It was already there[^], yes
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Try reading this old article on MSDN.[^]
Truthfully, I haven't used WithEvents for anything in about 10 years. I found that I prefer to wire up the events I need myself instead of relying on Handles.
|
|
|
|
|
Thanks for the links. It's taking a while, since I have to fit reading them into my schedule. I am in the middle of Article 2.
|
|
|
|
|
How to find the size of a structure in vb.net?
Here is my structure
Public Structure MyStruct
Public a As Byte
Public b As Integer
Public c As Double
Public d As Double
End Structure
|
|
|
|
|
Find out the size of the different data types and add them together, this will give you the max size.
The exercise is left to the student!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thanks Mycroft Holmes. Earlier in visual basic 6.0 we can use simply Len(structName). If the structure is too lengthy, it will be difficult to use this method..? Is there any other way ?
|
|
|
|
|
Doesn't the managed layout use the default packing/alignment as well, or is that only for P/Invoke?
The C# sizeof (the unsafe operator, not the Marshal.SizeOf method - see Dave's comment and Eric Lippert's blog) reports the size of this structure as 24, but the individual fields would only account for 21 bytes.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That depends on what you're doing with the structure.
Read this[^].
|
|
|
|
|
Hi everyone.
Built small package in vb 2005 with reportviewer. However when i converted it in vb 2010, all the forms with a reportviewer control do not work and give errors. I tried to drag and drop the control onto the form anew. It shows the action is taken place but no effect meaning the control is not placed onto the form at all.
This is my first time using vb 2010.
Thanks
|
|
|
|
|
I know your question states VS2010, but by chance are you using the express edition?
|
|
|
|
|
Thanks for replying
This is the exact error I am getting:
Error 1 Value of type 'Armory_Database.Microsoft.Reporting.WinForms.ReportViewer' cannot be converted to 'System.Windows.Forms.Control'. D:\Database Development\Armory Database Files\Report\FrmAmunition_Chart.Designer.vb 68 25 Armory Database
This error relates to a form that was created in visual studio 2005 with reportviewer control.
I am trying to open it in visual studio 2010 professional edition and I am getting the above error on all the forms with reportviewer control.
I search the web but no solution.
Thanks again for your help.
|
|
|
|
|
I was hoping that this may have been a issue with the express version as there is a simple hack to work around MS's decision to drop support for the control in the express versions[^].
I suggest you copy the error message you posted to your original post (use the edit link at the bottom of the post) so that others can see it easily. You should also show the referenced code line (68) from FrmAmunition_Chart.Designer.vb. I suspect that it is Me.Controls.Add(xxx) , but it is best not to guess.
|
|
|
|
|
Thanks for the response.
---------------------------------------------------
The error message:
Error 1 Value of type 'Armory_Database.Microsoft.Reporting.WinForms.ReportViewer' cannot be converted to 'System.Windows.Forms.Control'. D:\Database Development\Armory Database Files\Report\FrmAmunition_Chart.Designer.vb 68 25 Armory Database
-------------------------------------------
On the designer below, the error is the( Me.Controls.Add(Me.ReportViewer1).
Me.AutoScaleDimensions = New System.Drawing.SizeF(5.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.ClientSize = New System.Drawing.Size(687, 508)
<b> Me.Controls.Add(Me.ReportViewer1)</b>
Me.Font = New System.Drawing.Font("Arial", 6.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Margin = New System.Windows.Forms.Padding(2, 3, 2, 3)
Me.Name = "FrmAmunition_Chart"
Me.Text = "Service d'Armurerie->Graphe Munitions"
CType(Me.AmmunitionTD_ViewBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.Armory_ReportAmmunition_ViewDataSet, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
----------------------------------------------------------------
I also noticed if I tried to add another Reportviewer control the form, it takes the name Reportviewer2. That means there is a Reportviewer1 already on the form but can't see it.
Thanks
|
|
|
|
|
Hello everyone;
Any feedback will be appreciated.
Thanks
|
|
|
|
|
I'm making a file downloader for game that I'm developing but I' at a loss on how to show the download speed, size, current amount downloaded, and how to execute the file once it's been downloaded from the VB app. If anyone can help me with this I'd be very grateful.
Imports System.Net
Public Class Form1
Dim WithEvents WC As New WebClient
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btndownload.Click
WC.DownloadFileAsync(New Uri("http://software-files-a.cnet.com/s/software/13/11/34/40/avast_free_antivirus_setup.exe?lop=link&ptype=3001&ontid=2239&siteId=4&edId=3&spi=47930a5e8f8651a2bc6c9fa6deeb7e68&pid=13113440&psid=10019223&token=1372207673_29dcdb8042510e0ccc215fea7880786d&fileName=avast_free_antivirus_setup.exe"), "C:\avast.exe")
End Sub
Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
lblpercent.Text = ProgressBar1.Value & "%"
End Sub
End Class
|
|
|
|
|
Hi! I want to compare two datagridviews, in order to have a list of items that are common in both datagridviews, and a List of non matched itens, based on the values of an specific column - column(1) - which is found in both dgv. Also, these lists would be consolidated by the values from a column - column(2) - from the second datagridview.
An example to illustrate:
Datagridview1 Datagridview2
idProduct Item IdProduct Item IdSupplier
1 Item A 1 Item A 1
1 Item B 1 Item B 1
2 Item C 2 Item C 1
1 Item A 2
1 Item B 2
So, my desired results for the matched items would be:
IdSupplier 1 () = {Item A, Item B, Item C}; non-mathced items () = {""};
IdSupplier 2() = {Item A, Item B}; non-mathed items () = {Item C};
What I've made is a loop that filters the second datagridview, through the values from the third column, and added items that are corresponding to the first datagridview column(1) by another loop through datagridviews (Cells or Rows) and using IF statements. However, I end with a "big" list that adds all the elements, because I still do not know how to consolidate this list for each element of the third column of datagridview2.
So, from the example above, my results would be:
{Item A, Item B, Item C, Item A, Item B}
I also have tried LinQ by the following code:
For Each row As DataGridViewRow In dgv1.Rows
If Not row.IsNewRow Then
Dim row1 = row
Dim myTableSummary = (From myRow As DataGridViewRow In dgv2.Rows.Cast(Of DataGridViewRow)() Where Not myRow.IsNewRow AndAlso myRow.Cells(1).Value.ToString.Equals(row1.Cells(1).Value.ToString) Select ID1 = myRow.Cells(3).Value, _
ItemValue = myRow.Cells(1).Value Group By ID1 Into G = Group, match = Count())
Dim myArrayOfAnonymousTypes() = myTableSummary.ToArray
For Each myElement In myArrayOfAnonymousTypes
MsgBox(myElement.ID1.ToString & " - " & myElement.match.ToString)
Next
End If
Next
modified 26-Jun-13 0:37am.
|
|
|
|
|
Hi Friends,
I want to some data from Access table first save CVS File format after than it convert text format and also want that "," (Comma) replaced with "#~#" as it open
does it possible. please help me
|
|
|
|
|
Ashok Kumar9978 wrote: does it possible. Yes, if you write some code:
- Open file 1 as .csv
- Open file 2 as .txt
- Read the next record from the Access database
- Format and write to the .csv file
- Format and write to the .txt file
- Repeat until all records are processed
Use the best guess
|
|
|
|
|
|
Hi all.
I have a curious question. I know how to do this the "official" way, but I want to try to do it backwards, and it seems it should be possible. But I am new to delegates.
I have read a few articles on how to get a button to respond to mouse clicks when the main thread is super busy running some really long task. The methods always revolved around putting that task in a background thread, so that the form was not held up in any way in terms of handling events. I understand these methods fairly well right now.
But I am curious if the reverse is possible? Can the main thread be stuck in the middle of some long task, and can its form have a button that can respond to a delegate in a background task. All the button does is set/reset a public flag. If the button could respond, the flag would get valued, and that would impact what the main thread is doing.
Everything I have tried has failed, because I do not understand how to use a delegate that can support a user-interactive feature, like a button click.
I have a kludge idea that might work, but it is a bit clunky. It would involve a transparent second form on a separate thread that overlays the main thread's form, and has that button on it in the exact position where it WOULD be, if it had been on the main thread's form. So to the user, it just looks like another button on the main form. But I don't like the idea because it has a clunk factor of ten. All that work for a single button, especially since if I want to resize or move the main form, the second form has to move along with it. Just plain messy.
So it just seems that some sort of delegate approach should work to avoid all that mess.
Thanks to anyone who can offer advice on a reverse-logic idea that may be leaving a bad taste in your mouth.
|
|
|
|
|
A button has a PerformClick method - if he button is public, you could call that.
But actually that's not the way to do it. In the event handler of the button click event, you call a method to do the things you want to do on that button click. If that method is public, you could directly call it. However, if UI elements are accessed, you should check for InvokeRequired.
|
|
|
|
|
Bernhard Hiller wrote: However, if UI elements are accessed, you should check for InvokeRequired.
That's exactly where my problem is, I think. I am doing InvokeRequired, but am getting screwed up somewhere. Now, I also just got an error back at one point saying that delegates cannot handle events. I am assuming that means you can pass arguments to a control, but if you need to fire one of its events, like a button click, a delegate won't work. Not sure about that, though.
At any rate, here is my code so far. I took the guts from a demo and attempted to modify it to do the reverse of what it did originally...Run some really long task as a background thread and let the controls be free for use in the main thread. This experiment is attempting to do the opposite...Run the task in the main thread, and work the form's button via a background task. It doesn't toggle the Pause/Continue state just yet...At this point, I was just trying to get the button to respond to a simple click.
Also, the Run method at the very bottom does the Invoke, but it causes the program launch to hang during form load. I figured I might run into problems there, since I may be trying something impossible with a delegate, or I've just got everything set up wrong.
Imports System.Threading
Public Class Form1
Private Delegate Sub Pause_Continue_cmd_ClickDelegateType(_
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Private Pause_Continue_cmd_ClickDelegate As _
Pause_Continue_cmd_ClickDelegateType
Private BG_Thread As Thread
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
FormIsLoadingFlag = 1
Threading.Thread.CurrentThread.Name = "UI Thread"
ProgramLaunchFlag = 0
FormIsLoadingFlag = 0
Dim New_DelegatePauseContinueButton As New _
Delegate_Pause_Continue_Button(Me, BGThreadNumber:=1)
BG_Thread = New Thread(AddressOf New_DelegatePauseContinueButton.Run)
BG_Thread.IsBackground = True
BG_Thread.Start()
End Sub
Private Sub StartLoop_cmd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles StartLoop_cmd.Click
Call StartLoop()
End Sub
Private Sub StartLoop()
Dim loopo As Long
Dim loopo2 As Long
For loopo = 1 To 100000000
For loopo2 = 1 To 100000000
loopo = loopo
Next loopo2
Call Pause_Continue()
Counto = Counto + 1
Me.CounterVal_echo.Text = Str(Counto)
Me.CounterVal_echo.Refresh()
Next loopo
End Sub
Public Sub Pause_Continue()
EndlessLoop:
If CountStopFlag = 0 Then Exit Sub
GoTo EndlessLoop
End Sub
Public Sub Pause_Continue_cmd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Pause_Continue_cmd.Click
CountStopFlag = 1
End Sub
End Class
Public Class Delegate_Pause_Continue_Button
Private OwnerForm As Form1
Private Delegate Sub Pause_Continue_cmd_ClickDelegateType( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Private Pause_Continue_cmd_ClickDelegate As Pause_Continue_cmd_ClickDelegateType
Private PrivateBGThreadNumber As Integer
Public Sub New(ByVal my_form As Form1, _
ByVal BGThreadNumber As Integer)
OwnerForm = my_form
PrivateBGThreadNumber = BGThreadNumber
Pause_Continue_cmd_ClickDelegate = AddressOf OwnerForm.Pause_Continue_cmd_Click
End Sub
Public Sub Run()
Try
Do
SyncLock OwnerForm
If OwnerForm.InvokeRequired() Then
Dim args() As Object = {Nothing, Nothing}
OwnerForm.Invoke(Pause_Continue_cmd_ClickDelegate)
End If
End SyncLock
Loop
Catch ex As Exception
Debug.WriteLine("Unexpected error in thread " & PrivateBGThreadNumber & vbCrLf & ex.Message)
End Try
End Sub
End Class
|
|
|
|
|