VB.net - ToString.Contains not recognising strings in Datatable - vb.net

after finding many useful answers on this site from others' questions I am now in a position where I need to ask my own question as can't seem to find a similar issue elsewhere...
I am working on a Windows Form project in VB.net. I have many text boxes which require data validation. Therefore, I am storing the limits for the text box values in a seperate xml file and the below code runs on the validating event of each text box. This has worked fine up until now....
I have tried to include more text boxes and subsequently edited the xml file (importing to MS Access then exporting back to overwrite the original xml file). Now, when debugging, I see the updated datatable has been loaded/read correctly from the xml but the ToString.Contains returns False when it should return True. Its as if the string is not being recognised correctly in the Datatable. I really can't think what is causing this as it was working perfectly well before trying to add more text boxes. Any help would be very gratefully received?!!
For Each row As DataRow In data.dtLimits.Rows
If row(1).ToString.Contains(tb.Name) Then
ul = Double.Parse(row(2).ToString)
ll = Double.Parse(row(3).ToString)
If IsNumeric(tb.Text) Then
If Double.TryParse(tb.Text, value) Then
If value > ul Or value < ll Then
e.Cancel = True
tb.Select(0, tb.Text.Length)
ErrorProvider1.SetError(tb, "Please enter a value between " & row(3).ToString & " And " & row(2).ToString)
End If
End If
ElseIf tb.Text = "" Then
tb.Text = 0
e.Cancel = False
Else
e.Cancel = True
tb.Select(0, tb.Text.Length)
ErrorProvider1.SetError(tb, "Please enter a valid datum")
End If
End If
Next
NB- The data table has four columns 0-ID Value, 1-Textbox Name,2-Upper Limit,3-Lower Limit.

Related

Pop up window with textbox after selecting checkbox

all
I am working on a project with vb.net and MySQL database.
Now for taking information I have added few checkboxes, and in case if user selects a checkbox named other I want a window to appear and it should have a text box and when user enters the text at that box, the details should be stored in db.
As you haven't provided much information I may can't answer exactly your question but check if this can help.
First Add LINQ to SQL (.dbml File) - Let's name it as XYZ and Dataset (.xsd File) - let's name this as XYZ too, to your project and then drag and drop your database table in both the files and save all.
Now going into your form which contains the check box you mentioned.
Add this code to you checkbox click_event.
If checkbox1.checked = True Then
Dim insertValue As String = ""
insertValue = InputBox("Enter text to insert", YourTitle, "")
If inserValue <> "" Then
Dim db as New XYZDataContext
Dim NewRec As New YourTableName With {.ColumnName = insertValue}
db.YourTableName.InsertOnSubmit(NewRec)
db.SubmitChanges()
Msgbox("Value added!")
End If
checkbox1.checked = False
End If
create your window in the designer and give it a name like someWindow. then in your code open the window from the click event of your checkbox. when you close the window don't dispose it. just hide it me.hide so when you window closes you can retrieve the data from your textbox.
Sub Test ()
Dim wd_SomeInfo as new someWindow
wd_SomeInfo.showdialog()
Dim result As String = wd_SomeInfo.txt_sometextbox.text
If result = "" Then cancel....
End Sub

Crystal Report formula field index changing on each new build

I've got a Crystal Report which I made in CR2008, which I'm printing from my vb.net application. In this report, I have an image and a formula field.
The image formula (Under Format Graphic > Picture > Graphic Location is set to be the formula field called #imgLocation.
In my vb.net code, I have the following block to select the saved file path from the database, then fill #imgLocation with this value, to set the image that is being shown.
Try
Dim logoPath As String = ds.Tables(0).Rows(0).Item("reportLogoPath") & ""
If logoPath = "" Then
MessageBox.Show("No logo path has been defined in the System Settings. Please set " & _
"one and try again.", "Load Report Failed", MessageBoxButtons.OK)
Exit Sub
Else
cReport.DataDefinition.FormulaFields.Item(2).Text = Chr(34) & logoPath & Chr(34)
End If
Catch ex As Exception
errorLog(ex)
End Try
I've seen this working twice. The image I want replaced the placeholder image when I had FormulaFields.Item(2), but then I resized the image and it didn't work. I then changed the value of the index, and it worked with FormulaFields.Item(4), but again stopped working when I resized the image.
Why is the index changing, and what is the way around it?
I tried using the named parameter option, but FormulaFields.Item("#imgLocation") gave me an error of
Invalid Index
EDIT
As per #Bugs request, this is the report showing the correct string and image
Then, after deleting and re-saving the image (But in a slightly bigger size), the parameter was passed correctly still, but the placeholder image was shown instead.
This makes me think now that it's to do with image size, however, re-sizing it back to the original size still didn't show it again.
EDIT 2
Could it be do to with the fact that I'm loading a new form which contains the report viewer, rather than opening it from the same form?
cReport.RecordSelectionFormula = selectionFormula
Me.Cursor = Cursors.Default
Dim f As New frmReportViewer(con, cReport, 0)
f.Show()
Private Sub frmReportViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
setFormSizes(Me, con)
Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2), 10)
Me.Text = "Report Viewer"
Dim logOnInfo As New TableLogOnInfo()
Dim i As Integer
For i = 0 To cReport.Database.Tables.Count - 1
logOnInfo.ConnectionInfo.DatabaseName = "comm_db"
logOnInfo.ConnectionInfo.Password = "Acplus2016!"
cReport.Database.Tables.Item(i).ApplyLogOnInfo(logOnInfo)
Next i
cReport.VerifyDatabase()
crViewer.ReportSource = cReport
crViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
crViewer.Zoom(87)
Catch ex As Exception
errorLog(ex)
End Try
End Sub
Please note my answer is addressing the part regarding adding parameters. For the moment the no image available placeholder is proving difficult to resolve on my side.
To add a parameter please follow these steps:
Add a parameter within Crystal Reports called imageLocation:
After pressing OK you should have a parameter in your list like this:
The following code will pass a value to the parameter:
Dim cReport As New ReportDocument
cReport.Load("C:\Report.rpt")
If cReport.ParameterFields.Item("imageLocation") IsNot Nothing Then
cReport.SetParameterValue("imageLocation", "\\SAGE200FS\Sage\StockImages\sc002.jpg")
End If
reportViewer.ReportSource = cReport
This is the output (I have added the parameter onto the report):
Note that if you are passing parameters and they are shown on the report, make sure you don't have cReport.VerifyDatabase() in place. This often throws up a dialog box asking for the parameter values.
Screenshot of dialog box:
By not calling cReport.VerifyDatabase() this will stop the dialog box from showing.
Now through Crystal Reports you can set the Graphic Location using a parameter. To do this, add the parameter to the report and suppress the field (as you don't want to see it). Then you can add the Report Field as the location.
Add parameter to report and suppress:
Add Report Field as the Graphic Location for the OLE Object:~=
Run the report and input the parameter. This will change the image accordingly. See both screenshots:
SC001::
INT4303:
This however does not seem to work through VB. The ReportViewer does not handle the change of images. Instead you are left with the default image. I'm unsure why this is the case and I'm guessing it's a bug with Crystal. If anyone knows of why this happens, feel free to comment.
EDIT
After discussing the smaller points in chat, we seem to have worked out that the issue arose because of the file path. Saving it to the database as \\server\...\...\image.png was not displaying, but after changing it to Z:\...\...\image.png it has worked fine every time. It was because of the file path, all along.

Making a form that users enter a value into text boxes a certain number of times

I have a series of text boxes that are hidden, except one which users enter a value into. After a button is pressed, this text box disappears and the rest appear. From there, I want users to be able to enter different values into each text box. These values will be saved as variables. From there, users press a button and enter new values into the text boxes, until values have been entered into the text boxes for each number up to the first value entered into the hidden text box.
I'm not sure how else to explain this, but please let me know if you have any questions.
Correct me if im wrong. i think you want an input in different textbox?
Like textbox1 = 1, textbox2 = 2 and so on. If this is right then try this.
Lets try this to your 3 textbox for an example.
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
If TextBox1.Visible = True Then
If Trim(TextBox1.Text) <> "" Then
TextBox1.Visible = False
TextBox2.visible = True
Else
MsgBox("Required")
'or user error provider.
End If
Exit Sub
End If
If textbox2.visible = True Then
If Trim(TextBox2.Text) <> "" Then
TextBox2.Visible = False
TextBox3.visible = True
Else
MsgBox("Required")
'or user error provider.
End If
Exit Sub
End If
'and so on
End Sub
hope this is what you want. just basic but maybe usefull :)
This is the workflow of what you want to accomplish in JavaScript. VB.Net has similar functions to Visibility. https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
In the visual editor you can create buttons and add Click events to them, this is basic so I won't tell you how to do it. Google if you can't.
var options = document.getElementsByClassName("options");
var optionsSetOne = document.getByClassName('optionsSetOne');
This will select all your options. Now that you have all your options selected and in a variable called options you can play around with the visibility, as the user progresses thought the buttons.
First I would set all my options to hidden with:
options.style.visibility = 'hidden';
After the users progresses and clicks the button, I will make the first set of the options visible. With:
$('#buttonOne').click(function(){
optionsSetOne.style.visibility = 'visible';
});
This is the basic workflow of what you want to accomplish, you have to figure out how to save all the information the user gives to you.

The 'If' statement part is not being carried out

I'm currently working on a project for school and it includes a login and register system.
This is a function that I have made. The "taken" variable will be passed back to the main program.
Dim taken As Boolean
Dim temp As String
For counter = 1 To totalrecords
FileGet(1, player_info)
temp = player_info.username
If TextBox2.Text = temp Then
msgbox("this is a messagebox")
taken = True
End If
Next
This is the part of the code that checks if any usernames are in use. If so, taken = true and then a message is displayed.
Now, for some reason the if statement part is not being carried out. The message box does not show at all. I have tested this by using more than one same username and the second (same) username is still added to the file. I'm very confused.
Part I believe is not working -
If TextBox2.Text = temp Then
taken = True
End If
You should use String.Equals(String1, String) to compare TextBox text and the temp variable.
In your case it should be written as this.
If String.Equals(TextBox2.Text, temp) Then
taken = true
End if

Remove end character in a textbox without rewriting all the data

I am writing a program to communicate over the serial port. All data that is sent is mirrored back. Everything works fine except backspaces. When I hit the backspace button the only way I know how to remove the last character in the text box is to use a mid function then overwrite the current data with the new data. When a lot of data is inside the richtextbox it starts to flicker. I have tried using the richtextbox.text.remove function but I get this error. "Index and count must refer to a location within the string. Parameter name: count"
RichTextBox1.Text.Remove(RichTextBox1.TextLength, 1)
I have tried to throw some number into the function that does not cause it to error out but no data is removed from the richtextbox.
Here is the code that transmits data
KeyCharString = e.KeyChar 'stores key being pressed into KeyCharString
Try
SerialPort1.Write(KeyCharString) 'tx data for key being pressed
Catch ex As Exception
MsgBox(ex.Message) 'Displays error if serialport1 cannot be written to
End Try
If Asc(KeyCharString) = 8 Then 'If char is a backspace remove precious character and exit sub
RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.TextLength, 1)
'RichTextBox1.Text = Mid(RichTextBox1.Text, 1, RichTextBox1.TextLength - 1)'Old code used to remove the character. Causes the richtextbox to flicker when rewriting the data
Exit Sub
End If
This is the code that receives data
receivedString = SerialPort1.ReadExisting.ToString
If Asc(receivedString) = 8 Then 'deletes the received data if it is a backspace
receivedString = ""
Exit Sub
End If
RichTextBox1.AppendText(receivedString) 'adds new data to the richtextbox
Is there a way to remove 1 character from the richtextbox without rewriting all the data inside it? Also, the richtextbox is read only.
The String.Remove method that you are using returns a String it does not do anything with the original string.
from MSDN Link:
Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
Try this but I am not sure of the flicker:
RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.TextLength - 1, 1)
or something like this:
RichTextBox1.SelectionStart = RichTextBox1.TextLength - 1
RichTextBox1.SelectionLength = 1
RichTextBox1.ReadOnly = False
RichTextBox1.SelectedText = ""
RichTextBox1.ReadOnly = True