I created a tcp connection in vb6 to grab the weight off of a scale and display that weight after pressing a button. The problem is that the weight is not displayed until the SECOND (2nd) click of the button, not the first. I have set a break point in various spots and upon the first click of the button, it takes me to that break point, so I know the event is firing as it should, but nothing is displayed until the 2nd click. I have done a bunch of research but can't seem to find anyone with the exact problem (or solution).
Public tcpC As New Winsock
'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub CFixPicture_Close()
tcpC.Close
End Sub
Private Sub CFixPicture_Initialize()
tcpC.LocalPort = 0
tcpC.Connect "192.168.0.1", 8000
End Sub
Private Sub CommandButton1_click()
On Error GoTo errHandler
Dim strData As String
tcpC.SendData "S" & vbCrLf
tcpC.GetData strData
Text1.Caption = "Weight: " & strData
Exit Sub
errHandler:
MsgBox "error:" & Err.Description
End Sub
I am making an assumption that your code is in the form and you are just declaring a new object of type Winsock. My code declares a Winsock variable using the keyword WithEvents to get access to the events raised by the Winsock object. The particular event you're interested in is DataArrival. It is fired by the Winsock control when data is received. I moved setting the text to this event. Also, you cannot use WithEvents and "As New" (you really don't want to use As New anyway), so I create the object before I set the properties in the CFixPicture_Initialize() method. Finally, I added setting the object to nothing after closing it.
Option Explicit
Private WithEvents tcpC As Winsock
Private Sub CFixPicture_Close()
tcpC.Close
Set tcpP = Nothing
End Sub
Private Sub CFixPicture_Initialize()
Set tcpC = New Winsock
tcpC.LocalPort = 0
tcpC.Connect "192.168.0.1", 8000
End Sub
Private Sub CommandButton1_click()
On Error GoTo errHandler
Dim strData As String
tcpC.SendData "S" & vbCrLf
'there is no data here yet - moved to the DataArrival event
'tcpC.GetData strData
'Text1.Caption = "Weight: " & strData
Exit Sub
errHandler:
MsgBox "error:" & Err.Description
End Sub
Private Sub tcpC_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpC.GetData strData
Text1.Caption = "Weight: " & strData
End Sub
Related
I am delving into territory unfamiliar to me. I'm creating a User Control that I'd like to use across multiple forms. (thus reason for User Control). I am only familiar with VB.NET language. I've googled and I think I'm close to understanding, but I just can't figure this out. I've got multiple buttons on my User Control. When the "upload" button is pressed on my user control, I'd like it to run a Sub on my parent page called UploadFile(), which has one string parameter. I will have the UserControl on my page mulitple times like this:
<uc1:UploadFile ID="UploadFile1" runat="server" />
<uc1:UploadFile ID="UploadFile2" runat="server" />
In my code behind of the parent page, I'd like to execute this function on the Click event of the upload button within the user control. Where the unique parameters will then tell me what folders I will be uploading my file to. So that when user uploads a file within my first User Control on the page, it will run my Upload Sub on my parent page, take the parameter "a" and run my Sub that uploads my document and saves to database for files specific to "a" type. I will then tell the user control to change it's label to show that File A has been uploaded
Sub something somethingUploadFile1_Click
UploadFile("a")
End Sub
Sub something somethingUploadFile2_Click
UploadFile("b")
End Sub
Sub UploadFile(ByVal myString as string)
Select Case myString
Case "a"
If UploadFile1.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile1.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
Case "b"
If UploadFile2.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile2.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
End Select
End Sub
I know that in above code I'm supposed to be somehow referencing the button on my UserControl, but I'm not sure how to do it. I also know that Raising Events and Event Delegation are two major components of this, but again, I'm just not figuring it out. Can you please show me how to complete my code using the existing examples I provided?
Here is the complete code behind of my existing UserControl:
Public Class UploadFile
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
End Sub
Protected Sub btnDelete_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click
End Sub
Public Property lblViewFile() As Label
Get
Return _lblViewFile
End Get
Set(ByVal value As Label)
_lblViewFile = value
End Set
End Property
Public Property btnDelete() As ImageButton
Get
Return _btnDelete
End Get
Set(ByVal value As ImageButton)
_btnDelete = value
End Set
End Property
Public Property pnlUpload() As Panel
Get
Return _pnlUpload
End Get
Set(ByVal value As Panel)
_pnlUpload = value
End Set
End Property
Public Property FileUpload() As FileUpload
Get
Return _FileUpload
End Get
Set(ByVal value As FileUpload)
_FileUpload = value
End Set
End Property
Public Property btnUpload() As Button
Get
Return _btnUpload
End Get
Set(ByVal value As Button)
_btnUpload = value
End Set
End Property
Public Property lblReplaceMsg() As Label
Get
Return _lblReplaceMsg
End Get
Set(ByVal value As Label)
_lblReplaceMsg = value
End Set
End Property
Private _lblViewFile As Label
Private _btnDelete As ImageButton
Private _pnlUpload As Panel
Private _FileUpload As FileUpload
Private _btnUpload As Button
Private _lblReplaceMsg As Label
End Class
asp.net vb user control raising an event on the calling page
derive the arguments class you need from EventArgs according to your structure and data and use that.
I am trying to pass a value from the field ReportID (which is a number field) on form 1 to the field ReportID on the form called Budget_fsub after I click a button to open that second form. I've tried this code to pass the value to the second form, and it looks like it works but when I go to the table which holds the values for the second form, the value has not been recorded. What am I doing wrong?
Form1 with button that opens form2:
Private Sub cmdEnterBudgetInfo_Click()
On Error GoTo Err_EnterBudgetInfo_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Budget_fsub"
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ReportID]
Exit_cmdEnterBudgetInfo_Click
Exit Sub
Err_EnterBudgetInfo_Click:
MsgBox Err.Description
Resume Exit_cmdEnterBudgetInfo_Click
End Sub
And here's the code on the form Budget_fsub:
Private Sub Form_Open(Cancel As Integer)
Dim defaultID As Long
defaultID = CLng(Nz(Me.OpenArgs, 0))
If defaultID = 0 Or IsNull(Me.OpenArgs) Then
Cancel = True
Exit Sub
End If
Me.ReportID.DefaultValue = defaultID
End Sub
So, here is a working solution. I assume you have bound form and ReportID textbox is bound to the column. Please note, I have never been fan of using macros to manipulate the data..
Here is code for your Form1:
Private Sub cmdEnterBudgetInfo_Click()
On Error GoTo Err_EnterBudgetInfo_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Budget_fsub"
'close the form first
DoCmd.Close acForm, stDocName, acSaveYes
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ReportID]
Exit_cmdEnterBudgetInfo_Click:
Exit Sub
Err_EnterBudgetInfo_Click:
MsgBox Err.Description
Resume Exit_cmdEnterBudgetInfo_Click
End Sub
Here is code for your sub form (Budget_fsub):
Option Compare Database
Option Explicit
'global variable
Dim repID As Long
'form load event
Private Sub Form_Load()
Me.ReportID.Value = repID
DoCmd.RunCommand acCmdSave
End Sub
'form open event
Private Sub Form_Open(Cancel As Integer)
SetReportID
If repID = 0 Then
Cancel = True
Exit Sub
End If
End Sub
'function to set reportID from the openargs
Private Function SetReportID()
repID = CLng(Nz(Me.OpenArgs, 0))
End Function
Hope this helps.
Developed a slightly dynamic page that builds a Question and Answer page based on information from a database. Everything works great except i cant get the RadioButtonList event to trigger with a minor MsgBox response to validate object sender and selected ListItem information.
Page_Load
If Not Page.IsPostBack Then
With Globals.tst
.GetBrandInformation(Page.RouteData.Values("brand"),
Page.RouteData.Values("year"),
Page.RouteData.Values("month"))
'Load up Question DataSet
For Each quest As Question In .Questions
Dim tr As New HtmlControls.HtmlTableRow
Dim td As New HtmlControls.HtmlTableCell
td.Attributes.Add("class", "tdQ") 'Add class attribute to <td> element, creating 'class="tdQ"'
Dim lbl As New Label
lbl.ID = "lbl" & quest.ID
lbl.Text = quest.Text
td.Controls.Add(lbl)
tr.Cells.Add(td)
tblContent.Rows.Add(tr)
tr = New HtmlControls.HtmlTableRow
td = New HtmlControls.HtmlTableCell
td.Attributes.Add("class", "tdA") 'Add class attribute to <td> element, creating 'class="tdA"'
Dim rbl As New RadioButtonList
rbl.ID = "rblT" & .ID & "_Q" & quest.ID
AddHandler rbl.SelectedIndexChanged, AddressOf rbl_SelectedIndexChanged 'Attach generic event handler to control
Dim li As New ListItem
'Load up Answer Dataset
For Each answ As Answer In quest.Answers
li.Text = answ.Value
li.Value = "T" & .ID & "-Q" & quest.ID & "-A" & answ.ID & "-C" & answ.Correct
'Add built ListItem to RadioButtonList
rbl.Items.Add(li)
li = New ListItem
Next
td.Controls.Add(rbl)
tr.Cells.Add(td)
tblContent.Rows.Add(tr)
Next
End With
'
End If
Below is the Generic Event Handler Logic i am trying to attach to the RadioButtonList(s).
Protected Sub rbl_SelectedIndexChanged(sender As Object, e As EventArgs)
MsgBox(CType(sender, RadioButtonList).ID & " Clicked.")
MsgBox("Radio Button Selected: " & CType(sender, RadioButtonList).ID & " is Correct? " & CType(sender, RadioButtonList).SelectedValue)
End Sub
Anyone see anything wrong with the design, or know why the Event is not being triggered?
Edited 2012-11-20
Ok changed the rbl object to do AutoPostBack = true but that made life even more unbearable ;) Then quickly reverted back as the Q&A list i have is randomly generated on the Questions and Answers so the end-user would be given a new random order of questions upon every click of the ListItem.
Although i am getting some response back from the call backs still not getting the JavaScript response i am expecting by using either a defined JavaScript file or inline Response.Write/ClientScript.Register.
Created a debugging method into the Content Page:
Public Shared Sub Show(msg As String, Optional pg As Page = Nothing)
Dim cmsg As String = msg.Replace("'", "\'")
Dim scr As String = "<script type=""text/javascript"">alert('" & cmsg & "');</script>"
If pg Is Nothing Then
pg = CType(HttpContext.Current.CurrentHandler, Page)
End If
If (pg IsNot Nothing) And Not (pg.ClientScript.IsClientScriptBlockRegistered("alert")) Then
pg.ClientScript.RegisterClientScriptBlock(GetType(Alert), "alert", scr)
End If
End Sub
On page Load this method fires and as expected, but when placed inside the SelectedIndexChanged event it never fires off.
Still perplexed on why it works on general execution but not triggered execution.
Userful Method for those that like to create .Net methods that implement Javascript. .Net Slave - Javascript Alert.Show Class
You have to take it OUT of the If Not Page.IsPostBack Then
this is because when you DO postback, the...
AddHandler rbl.SelectedIndexChanged, AddressOf rbl_SelectedIndexChanged
...needs to be rebound before it can fire.
So basically, remove the If Not Page.IsPostBack Then and End If.
Oh, and get rid of the MsgBox() code too - this will kill the page from doing anything. If you want to output an Alert() then do something like:
Dim script As String = String.Format("alert('{0}');", ""Radio Button Selected: " & CType(sender, RadioButtonList).ID & " is Correct? " & CType(sender, RadioButtonList).SelectedValue")
page.ClientScript.RegisterClientScriptBlock(page.[GetType](), "alert", script, True)
UPDATE:
you also want to set AutoPostback = true; on your RBL. This tells the page to postback as soon as it's changed. the handler only fires if AutoPostBack is true.
I'm attempting to do a GET request in VB6 using CSocket. The data is sent successfully however no response is received (tested on multiple sites). My code is below.
Option Explicit
Dim WithEvents WinSock As CSocket
Private Sub Form_Load()
Set WinSock = New CSocket
End Sub
Private Sub btnConnect_Click()
WinSock.Protocol = sckTCPProtocol
WinSock.Connect "winhome.de", 80
MsgBox "Connecting..."
End Sub
Private Sub WinSock_OnConnect()
MsgBox "Connected."
Dim Data$
Data = "GET http://www.winhome.de/index.html HTTP/1.0" & vbCrLf & "Accept: */*" & _
vbCrLf & "Accept: text/html" & vbCrLf & vbCrLf
WinSock.SendData Data
MsgBox Data
End Sub
Private Sub WinSock_OnDataArrival(ByVal bytesTotal As Long)
Dim Data$
WinSock.GetData Data, vbString
MsgBox Data
End Sub
The OnConnect event never seems to trigger, but works fine with a normal WinSock control, Any help?
For these scenarios you don't want any "blocking" debugging, including MsgBox or IDE breaks -- the events get lost.
E.g. accumulate your debug logs into a string or a file and look at the results after a run.
I'm trying to sort records in the gridview right after a radio button is selected. My approach is with the dataview, but because the dataset variable doesn't survive a round trip to the server, I don't know how to make this happen. please help!
Public Sub GetCustomers()
db.RunProcedure("usp_customers_get_all")
db.doSort(radList.SelectedValue)
gvCustomers.DataSource = db.MyView
End Sub
Protected Sub radList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radList.SelectedIndexChanged
If radList.SelectedValue = 0 Then
db.doSort(0)
gvCustomers.DataSource = db.MyView
End If
If radList.SelectedValue = 1 Then
db.doSort(1)
gvCustomers.DataSource = db.MyView
End If
End Sub
Public Sub doSort(ByVal strIn As Integer)
If strIn = 0 Then
MyView.Sort = "lastname, firstname"
Else
MyView.Sort = "username"
End If
End Sub
Public Sub RunProcedure(ByVal strName As String)
Dim objConnection As New SqlConnection(mstrConnection)
Dim mdbDataAdapter As New SqlDataAdapter(strName, objConnection)
Try
mdbDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
Me.mDataset.Clear()
mdbDataAdapter.Fill(mDataset, "tblCustomers")
MyView.Table = mDataset.Tables("tblCustomers")
Catch ex As Exception
Throw New Exception("stored procedure is " & strName.ToString & " error is " & ex.Message)
End Try
End Sub
You could store the dataset in one of the following places and then when the post back happens just load it again from there. I have done many of these on a corporate intranet.
Session Variable
ViewState
QueryString
Cache
I cant really provide more help as you didn't specify if this is done in Ajax or if you do a full postback etc. If you provide more info I would love to help you.