在Jython中使用字符串格式

时间:2015-06-11 13:18:06

标签: java jython

我是Jython的新手。我想在Jython(look here)中使用字符串格式 Sub fillJobNoDropdown() cmbEditJobNo.Text = "" txtEditProjNo.Text = "" cmbEditProjNo.Text = "" txtEditProjName.Text = "" 'cmbEditProjType.Items.Clear() ERROR SHOWS HERE WHEN NOT CLEARED, ALSO TRIED = "" txtEditProjTypeDesc.Text = "" txtEditProjCost.Text = "" txtEditProjBudget.Text = "" 'get values to populate Job No. dropdown with active projects Dim fillJobNo As New SqlCommand("SELECT Distinct JobNo from PT_Project WHERE Active = '1' and Deleted = '0'") Dim dr As SqlDataReader = fillJobNo.ExecuteReader() While dr.Read() If dr.HasRows = True Then cmbEditJobNo.Items.Add(dr("JobNo)) Else End If End While dr.Close() End Sub Sub fillProjNoDropdown() cmbEditJobNo.Text = "" txtEditProjNo.Text = "" cmbEditProjNo.Text = "" txtEditProjName.Text = "" 'cmbEditProjType.Text = "" SAME THING: ERROR WHEN UNCOMMENT txtEditProjTypeDesc.Text = "" txtEditProjCost.Text = "" txtEditProjBudget.Text = "" Dim fillProjNo As New SqlCommand("SELECT ProjNo FROM PT_Project WHERE JobNo= '" & cmbEditJobNo.Text & "'") Dim dr As SqlDataReader = fillProjNo.ExecuteReader() While dr.Read() If dr.HasRows = True Then If IsDBNull(dr("ProjNo")) Then cmbEditProjNo.Visible = False txtEditProjNo.Visible = True txtEditProjNo.Text = "" txtEditProjNo.Focus() Else cmbEditProjNo.Visible = True txtEditProjNo.Visible = False cmbEditProjNo.Items.Add(dr("RICC")) End If Else cmbEditProjNo.Visible = False txtEditProjNo.Visible = True txtEditProjNo.Text = "" txtEditProjNo.Focus() 'if this happens and when saving will need to know if update or new End If End While dr.Close() End Sub Private Sub cmbEditJobNo_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbEditJobNo.GotFocus Call fillJobNoDropdown() End Sub Private Sub cmbEditJobNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEditJobNo.SelectedIndexChanged Call fillProjNoDropdown() End Sub Private Sub cmbEditProjNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEditProjNo.SelectedIndexChanged Dim fillProjProjNo As New SqlCommand("SELECT ProjectName, ProjectType, ProjectTypeDescription, ProjectCost, ProjectBudgeted FROM PT_Project WHERE JobNo = '" & cmbEditJobNo.Text & "' AND ProjNo = '" & cmbEditProjNo.Text & "'") Dim dr As SqlDataReader = fillProjProjNo.ExecuteReader() While dr.Read() If dr.HasRows = True Then txtEditProjName.Text = dr("ProjectName") Dim valuesA As List(Of String) = New List(Of String) valuesA.Add("Home") valuesA.Add("Work") valuesA.Add("Maintain") valuesA.Add("Pool") valuesA.Add("Lawn") 'Filter distinct elements, and convert back into list Dim resultA As List(Of String) = valuesA.Distinct().ToList 'Display result For Each elementA As String In resultA cmbEditProjType.Items.Add(elementA) cmbEditProjType.Text = dr("ProjectType") Next txtEditProjTypeDesc.Text = dr("ProjectTypeDescription") If IsDBNull(dr("ProjectCost")) Then txtEditProjCost.Text = 0.ToString("c") Else Dim formatProjCost As Decimal = dr("ProjectCost") txtEditProjCost.Text = formatProjCost.ToString("c") End If If IsDBNull(dr("ProjectBudgeted")) Then txtEditProjBudget.Text = 0.ToString("c") Else Dim formatProjBudg As Decimal = dr("ProjectBudgeted") txtEditProjBudget.Text = formatProjBudg.ToString("c") End If Exit Sub dr.Close() End If End While dr.Close() End Sub Private Sub txtEditProjNo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEditProjNo.GotFocus txtEditProjNo.ReadOnly = True Dim fillProj As New SqlCommand("SELECT ProjectName, ProjectType, ProjectTypeDescription, ProjectCost, ProjectBudgeted FROM PT_Project WHERE JobNo = '" & cmbEditJobNo.Text & "'") Dim dr As SqlDataReader = fillProj.ExecuteReader() While dr.Read() If dr.HasRows = True Then txtEditProjName.Text = dr("ProjectName") Dim valuesB As List(Of String) = New List(Of String) valuesB.Add("Home") valuesB.Add("Work") valuesB.Add("Pool") valuesB.Add("Maintain") valuesB.Add("Lawn") Dim resultB As List(Of String) = valuesB.Distinct().ToList For Each elementB As String In resultB cmbEditProjType.Items.Add(elementB) cmbEditProjType.Text = dr("ProjectType") Next txtEditProjTypeDesc.Text = dr("ProjectTypeDescription") If IsDBNull(dr("ProjectCost")) Then txtEditProjCost.Text = 0.ToString("c") Else Dim formatProjCost As Decimal = dr("ProjectCost") txtEditProjCost.Text = formatProjCost.ToString("c") End If If IsDBNull(dr("ProjectBudgeted")) Then txtEditProjBudget.Text = 0.ToString("c") Else Dim formatProjBudg As Decimal = dr("ProjectBudgeted") txtEditProjBudget.Text = formatProjBudg.ToString("c") End If Exit Sub dr.Close() Else End If End While dr.Close() End Sub Private Sub btnSaveProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveProject.Click Dim JobNo = cmbEditJObNo.Text Dim ProjNo = cmbEditProjNo.Text Dim ProjName = txtEditProjName.Text Dim ProjType = cmbEditProjType.Text Dim ProjTypeDesc = txtEditProjTypeDesc.Text Dim ProjCost = txtEditProjCost.Text Dim ProjBudg = txtEditProjBudget.Text 'comfirm changes Dim Reply As String = "" If ProjName = "" Or ProjType = "" Then MsgBox("Project Name and Project Type are required to be filled in.") Else Reply = MsgBox("I am ready to SAVE / UPDATE Project " & JobNo & " / Project No. " & ProjNo & ". Do you wish to continue?", vbYesNo) If Reply = vbYes Then 'Update database based on changes made my user (from stored procedure) Dim dbUpdate As New SqlCommand("PT_AddManageProjects", frmMainMenu.DB) dbUpdate.CommandType = CommandType.StoredProcedure dbUpdate.Parameters.Add(New SqlParameter("@SPUse", 1)) dbUpdate.Parameters.Add(New SqlParameter("@JobNo", JobNo)) dbUpdate.Parameters.Add(New SqlParameter("@ProjNo", ProjNo)) dbUpdate.Parameters.Add(New SqlParameter("@ProjectName", ProjName)) dbUpdate.Parameters.Add(New SqlParameter("@ProjectType", ProjType)) dbUpdate.Parameters.Add(New SqlParameter("@ProjectTypeDescription", ProjTypeDesc)) dbUpdate.Parameters.Add(New SqlParameter("@ProjectCost", ProjCost)) dbUpdate.Parameters.Add(New SqlParameter("@ProjectBudgeted", ProjBudg)) dbUpdate.Parameters.Add(New SqlParameter("@Deleted", DBNull.Value)) dbUpdate.Parameters.Add(New SqlParameter("@Active", DBNull.Value)) dbUpdate.ExecuteNonQuery() MsgBox("Project " & JobNo & " / Project No. " & ProjNo & " saved successfully.") Else 'if no do nothing, go back to form End If End If End Sub Private Sub txtEditProjectName_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEditProjCost.LostFocus, txtEditProjBudget.LostFocus 'validates that budget and cost are numeric values If txtEditProjBudget.Text = "" Then Exit Sub If Not IsNumeric(txtEditProjBudget.Text) Then MsgBox("Please enter a numeric Budget value") txtEditProjBudget.Focus() Exit Sub End If If txtEditProjCost.Text = "" Then Exit Sub If Not IsNumeric(txtEditProjCost.Text) Then MsgBox("Please enter a numeric Cost value") txtEditProjCost.Focus() Exit Sub End If End Sub Private Sub btnDeleteProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteProject.Click Dim JobNo = cmbEditJobNo.Text Dim ProjNo = cmbEditProjNo.Text Dim ProjName = txtEditProjName.Text Dim ProjType = cmbEditProjType.Text Dim ProjTypeDesc = txtEditProjTypeDesc.Text Dim ProjCost = txtEditProjCost.Text Dim ProjBudg = txtEditProjBudget.Text 'check if changes have been made to any of the data first (compared to database) Dim compareProj As New SqlCommand("SELECT ProjectName, ProjectType, ProjectTypeDescription, ProjectCost, ProjectBudgeted FROM PT_Project WHERE JobNo = '" & JObNo & "' AND ProjNo = '" & ProjNo & "'", frmMainMenu.DB) Dim dr As SqlDataReader = compareProj.ExecuteReader() While dr.Read() If dr.HasRows = True Then 'if changes have been made, show error If ProjName <> dr("ProjectName") Or ProjType <> dr("ProjectType") Or ProjTypeDesc <> dr("ProjectTypeDescription") Then MsgBox("If you are attempting to delete a project, you cannot change any fields. Please close this window and try again. If needed, clear all fields and restart.") dr.Close() Exit Sub End If End If End While dr.Close() 'comfirm delete Dim Reply As String = "" If ProjName = "" Or ProjType = "" Then MsgBox("Project Name and Project Type required to be filled in.") Else Reply = MsgBox("I am ready to DELETE Project " & JobNo & " / Project No. " & ProjNo & ". Do you wish to continue?", vbYesNo) If Reply = vbYes Then 'if no changes, confirm delete, then change deleted to '1' and active to '0' Dim dbDelete As New SqlCommand("PT_AddManageProjects", frmMainMenu.DB) dbDelete.CommandType = CommandType.StoredProcedure dbDelete.Parameters.Add(New SqlParameter("@SPUse", 2)) dbDelete.Parameters.Add(New SqlParameter("@JobNo", JobNo)) dbDelete.Parameters.Add(New SqlParameter("@ProjNo", ProjNo)) dbDelete.Parameters.Add(New SqlParameter("@ProjectName", DBNull.Value)) dbDelete.Parameters.Add(New SqlParameter("@ProjectType", DBNull.Value)) dbDelete.Parameters.Add(New SqlParameter("@ProjectTypeDescription", DBNull.Value)) dbDelete.Parameters.Add(New SqlParameter("@ProjectCost", DBNull.Value)) dbDelete.Parameters.Add(New SqlParameter("@ProjectBudgeted", DBNull.Value)) dbDelete.Parameters.Add(New SqlParameter("@Deleted", DBNull.Value)) dbDelete.Parameters.Add(New SqlParameter("@Active", DBNull.Value)) dbDelete.ExecuteNonQuery() MsgBox("Project " & JobNo & " / Project No. " & ProjNo & " deleted succesfully.") cmbEditJobNo.Items.Clear() 'call sub to clear fields and update jobno dropdown with only active non-deleted projects Call fillJobNoDropdown() Else 'if no do nothing, go back to form End If End If End Sub Private Sub optAdd_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optAdd.CheckedChanged If optAdd.Checked = True Then grpEdit.Visible = False grpAdd.Visible = True End If End Sub Private Sub btnAddNewProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNewProject.Click Dim JobNo = txtAddJobNo.Text Dim ProjNo = txtAddProjNo.Text Dim ProjName = txtAddProjName.Text Dim ProjType = cmbAddProjType.Text Dim ProjTypeDesc = txtAddProjTypeDesc.Text Dim ProjCost = txtAddProjCost.Text Dim ProjBudg = txtAddProjBudget.Text Dim readProj As New SqlCommand("SELECT JobNo, ProjNo FROM PT_Project") Dim dr As SqlDataReader = readProj.ExecuteReader() While dr.Read() If dr.HasRows = True Then If JobNo = "" Or ProjNo = "" Or ProjName = "" Or ProjType = "" Then MsgBox("Project Number, Project Name, and Project Type are all required to be filled in.") txtAddJobNo.Focus() dr.Close() Exit Sub ElseIf JobNo = dr("JobNo") AndAlso ProjNo = dr("ProjNo") Then txtAddJobNo.Focus() MsgBox("The combination of Job and Project numbers you've entered already exists.") txtAddProjNo.Focus() dr.Close() Exit Sub Else End If End If End While dr.Close() 'comfirm changes Dim Reply As String = "" Reply = MsgBox("I am ready to ADD Project?", vbYesNo) If Reply = vbYes Then Dim dbAdd As New SqlCommand("PT_AddManageProjects", frmMainMenu.DB) dbAdd.CommandType = CommandType.StoredProcedure dbAdd.Parameters.Add(New SqlParameter("@SPUse", 3)) dbAdd.Parameters.Add(New SqlParameter("@JobNo", JobNo)) dbAdd.Parameters.Add(New SqlParameter("@ProjNo", ProjNo)) dbAdd.Parameters.Add(New SqlParameter("@ProjectName", ProjName)) dbAdd.Parameters.Add(New SqlParameter("@ProjectType", ProjType)) dbAdd.Parameters.Add(New SqlParameter("@ProjectTypeDescription", ProjTypeDesc)) dbAdd.Parameters.Add(New SqlParameter("@ProjectCost", ProjCost)) dbAdd.Parameters.Add(New SqlParameter("@ProjectBudgeted", ProjBudg)) dbAdd.Parameters.Add(New SqlParameter("@Deleted", DBNull.Value)) dbAdd.Parameters.Add(New SqlParameter("@Active", DBNull.Value)) dbAdd.ExecuteNonQuery() MsgBox("Project has been added succesfully.") 'clear the form after successful addition Call clearAddGroupbx() Else End If End Sub Private Sub txtAddProjNo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAddProjNo.LostFocus Dim readProj As New SqlCommand("SELECT JobNo, ProjNo FROM PT_Project") Dim dr As SqlDataReader = readProj.ExecuteReader() While dr.Read() If dr.HasRows = True Then If txtAddJobNo.Text = dr("JobNo") AndAlso txtAddProjNo.Text = dr("ProjNo") Then MsgBox("The combination of numbers you've entered already exists.") txtAddProjNo.Focus() dr.Close() Exit Sub End If End If End While dr.Close() End Sub Sub clearAddGroupbx() txtAddJobNo.Text = "" txtAddProjNo.Text = "" txtAddProjName.Text = "" cmbAddProjType.Text = "" txtAddProjTypeDesc.Text = "" txtAddProjCost.Text = "" txtAddProjBudget.Text = "" End Sub Sub clearEditGroupbx() cmbEditJobNo.Text = "" cmbEditProjNo.Text = "" txtEditRicc.Text = "" txtEditProjName.Text = "" cmbEditProjType.Text = "" txtEditProjTypeDesc.Text = "" txtEditProjCost.Text = "" txtEditProjBudget.Text = "" End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click Call clearAddGroupbx() Call clearEditGroupbx() End Sub Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click Me.Close() Me.Dispose() End Sub

如果我尝试这样使用它:

str.format()

以及稍后

testText = 'This is a {word}'

例外文本是:

str.format(testText, word='test!')

我需要在进口/类路径或其他地方添加什么?!

感谢您的帮助!!

亲切的问候

0 个答案:

没有答案