Table in richtextbox - vb.net

In my previous question, I posted a code that adds a table (not actually, just adds data to the richtxtbx from dgvw) in a rich text box ..The code is:
For i As Integer = 0 To dg2.Rows.Count - 2
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "-"
For j As Integer = 0 To dg2.Columns.Count - 1
Try
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbTab + dg2.Rows(i).Cells(j).Value.ToString() + vbTab
Catch ex As Exception
End Try
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbLf
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "--------------------------------------------" + vbLf
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbLf
Next
Next
The results look like this :
--------------------------------------------
88795446
--------------------------------------------
Mr.
--------------------------------------------
Raiyan
--------------------------------------------
rashid
--------------------------------------------
Male
--------------------------------------------
I hope you see what the problem is.. Each cell's value is being added in a new line.. How do I generate a view like this:
HEADER TEXT HEADER TEXT HEADER TEXT
------------------------------------------------
Mr. ABC 0123456789
------------------------------------------------
Mr. DEF 987654321
------------------------------------------------
Mr. GHI 898989898
I mean all cell values of a row would be in one line... The next line would be filled with dashes..... Then the next will contain the cell values of the next rows....
I know my code is correct but maybe I am having some "ARRANGING THE CODE" issue... Or is this happening because of my rich textbox size? The reason why I'm thinking so is because when I add data from a dgvw containing 2/3 columns, it works as expected.. So how to fix it or achieve the look I am looking for?

maybe this would help
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "------------------------------------------------------------------------------------------" + vbLf
For i As Integer = 0 To dg2.Rows.Count - 1
For j As Integer = 0 To dg2.Columns.Count - 1
Try
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "• " + dg2.Rows(i).Cells(j).Value.ToString() + " •"
Catch ex As Exception
End Try
Next
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbLf
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "------------------------------------------------------------------------------------------" + vbLf
Next
This might even give you a better look.You must add as many "-"(dashes)as you need.Let me know if it worked

As you mentioned that when you add data to the rich text box from a datagridview that has a few columns..and then you get the result as desired..Then why not setting the WordWarp property of the Rich-text-box property to true and then add the data. Then i guess a row's cells' value wouldn't go to the next line and maybe you would be able to generate the look you want.

Related

vba parameters not received

I am trying to make a program that searches for specific content in cells for excel:
rows represent hours of the day from 00:00 to 23:00
columns represent a day of the month
The following code matches the input of the user to the content of the cells either by using the name. It can also add or skip a time interval for the event
However the following code always runs the instructions under Else even if the user inputs 2 numeric values for the 2 param fields. Some help would be appreciated:
pNume = paramNume
aux = ""
aux1 = paramHBegin - 1
aux2 = paramHEnd - 1
If IsNumeric(paramHBegin) And IsNumeric(paramHEnd) Then
For i = 1 To 31
For j = aux1 To aux2
If Cells(i + 1, j + 1) Like pNume & "*" Or Cells(i + 1, j + 1) Like "*" & pNume & "*" _
Or Cells(i + 1, j + 1) Like "*" & pNume Then
aux = aux + Cells(i + 1, j + 1) + " la ora " + CStr(i) + vbCrLf
End If
Next j
Next i
Else
For i = 1 To 31
For j = 1 To 24
If Cells(i + 1, j + 1) Like pNume & "*" Or Cells(i + 1, j + 1) Like "*" & pNume & "*" _
Or Cells(i + 1, j + 1) Like "*" & pNume Then
aux = aux + Cells(i + 1, j + 1) + " la ora " + CStr(i) + vbCrLf
End If
Next j
Next i
End If
displayInfo.Text = aux
This statement
If IsNumeric(paramHBegin) And IsNumeric(paramHEnd) Then
will only be true if both of the values you pass are numeric. But you wrote that "However the following code always runs the instructions under Else even if the user inputs 2 numeric values for the 2 param fields."
Assuming paramHBegin and paramHEnd are defined as strings, the only way I can see that happening is for one or both of the values having a character that isn't numeric. In Excel 2013 IsNumeric ignores carriage returns, tabs and spaces so they aren't the cause of the problem. If they are defined as objects, then you should specify the correct property of those objects.
Sorry I can't try it myself, but try something like this:
If IsNumeric(paramHBegin.Text) And IsNumeric(paramHEnd.Text) Then
or this:
If IsNumeric(val(paramHBegin.Text)) And IsNumeric(val(paramHEnd.Text)) Then
I think it's better to handle user inputs right at the input session itself rather then passing everything forward and then trying to handle exceptions
for instance, to force (to some extent) numeric inputs you could place in the UserForm code pane what follows:
Option Explicit
Private Sub paramHBegin_AfterUpdate()
TextBoxValidate Me.paramHBegin
End Sub
Private Sub paramHEnd_AfterUpdate()
TextBoxValidate Me.paramHEnd
End Sub
Private Sub paramNume_AfterUpdate()
TextBoxValidate Me.paramNume
End Sub
Sub TextBoxValidate(ctrl As MSForms.TextBox)
Dim number As Double
Me.CommandButton1.Enabled = False
With ctrl
If Not ValidateText(.text, number) Then
MsgBox "invalid input", vbCritical
.SetFocus
Else
.text = number
Me.CommandButton1.Enabled = True
End If
End With
End Sub
Function ValidateText(text As String, number As Double) As Boolean
On Error Resume Next
number = CDbl(WorksheetFunction.Substitute(text, " ", ""))
ValidateText = IsNumeric(number)
End Function
in essence:
add an AfterUpdate event handler for every relevant TextBox
that event handler would simply pass the validation dues to a specific sub (TextBoxValidate()) where you can put code to handle general TextBox validation environment
for instance I
disabled CommandButton1 button (the "OK" one in my test)
call a function (ValidateText()) for validating the Text property of a TextBox control which would return:
True and the validated number, if the Text property were actually a possible number
False otherwise
if validation result is True:
update the TextBox with the validated number
enable the "OK" Button
if validation result is False:
prompt a message and return the focus to the "invalid" TextBox
of course you can tune all those Subs and Functions to your actual needs like:
checking for specific text formats (with Like operator or - better - with RegEx object)
checking for specific types ( using Clng() or CInt() or CDate() instead of CDbl() in a sort of TryParse() fashion
changing validation rules according to each control type (TextBox rather than ListBox and so on) and property (Text Property rather than Value ...)
Finally
use "&" operator instead of "+" one to concatenate strings
If Cells(i + 1, j + 1) Like pNume & "*" Or Cells(i + 1, j + 1) Like "*" & pNume & "*" _
Or Cells(i + 1, j + 1) Like "*" & pNume Then
can be reduced to:
If Cells(i + 1, j + 1) Like "*" & CStr(pNume) & "*" Then
hope all this can help

VBA Excel Dynamically Display Added Results

So I am modeling my question with a simple Grocery List applications.
Program GUI:
Now what I want is for the Customer to enter: Eggs, Milk, and Bread and for that to enter and output to a .txt file.
Current Code:
Private Sub CreateList_Click()
Dim myFile As String, myString As String
myFile = "C:\Reformatted.txt"
Open myFile For Output As #1
myString = First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
Print #1, myString
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub
Desired Operation:
What I want to happen is that ther enter there first 5 items. Then it prompts them if they want another 5. If they do then they can add another line.
So I understand that I can add a MsgBox in VB and just design a while loop for that. My question is how to display the results of their first/previous submissions?
Desired Result:
I understand that VB stores the values as variables, but how can I show them to the user while they still have a chance to enter more entries. Also how to add all this with the preferred formatting to a notepad file?
----------------------------After Miss Palmer's Answer--------------------------
Private Sub AddEntry_Click()
Dim UserEntry As String
UserEntry = First.Value + DDPP.Value + Filer.Value + EntryNumber.Value
myString = myString & Chr(13) & UserEntry
GroceryList.UserDisplay.Caption = "You have entered:" & myString
End Sub
Scenario 1 - First Addition
Scenario 2 - Second Addition
The two additions should be placed one after the other. But currently it just replaces it.
You can update a label on the form on each iteration of your while loop using something of the form:
FormName.LabelName.Caption = "you have entered:" & myString
and then add to the string each loop with
myString = myString & First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
EDIT
myString = myString & chr(13) & First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value

VB.NET Code for datagridview to textbox in one line

How can I code this in vb.net. I want to show all data in textbox area
datagridview data
name purchase Group
vince 26 G1
jenny 28 G1
david 31 G1
Expected Output
what will be the code for this result in vb.net?
vince-26-G1, jenny-28-G1, david-31-G1
Something like this should work.
dim myString as string = ""
For Each dataR as datagridviewrow in datagridview1.rows
mystring = mystring + dataR.cells(0).value.tostring + "-" + dataR.cells(1).value.tostring + "-" + dataR.cells(2).value.tostring + ", "
Next
textbox1.text = mystring
Untested, but that's about what you're looking to do

Making text go to a new line when in a while loop

I have a richTextbox, and a while loop, x = 0, and every time this loop runs, x += 1 till it reaches a certain value.
Heres what I want to happen:
while x <> 10 then
it will say item 0 +1 on a new line, and then item 1 + 1 on the line under it, etc, so you will see all 10 values after.
What happens is, it will change that line into the new value.
My question is: How do I make it put the words on a new line instead?
Here is my code:
While readListItem <> PaymentList.Items.Count
If readListItem > PaymentList.Items.Count Then
readListItem = 0
Exit While
End If
readListItem += 1
MessageBox.Show(readListItem)
resultBox.Text = vbNewLine + PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "." + vbNewLine
End While
readListItem is "x", and that is being iterated by 1 every time the loop runs
PaymentList is my listbox containing an unknown value (the user sets the number of items in this list)
The If if there because, after x = lets say 10, it would add another to x (so now it = 11) and try to print out the 11th item, as it doesnt exist, it crashes. This is to attempt to prevent that. I didnt want to go with a try catch
Try adding the new value instead of replacing the entire value:
resultBox.Text &= Environment.NewLine() & PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "."
The expression &= is equivalent, in this case, to resultBox.Text = resultBox.Text & "...".
And once advice, you can simplify your loop by using this condition:
While readListItem < PaymentList.Items.Count
'...
End While

Visual Basic - picturebox name variable increment

Is there a way to set the image from one PictureBox object to another? For example:
pictureboxA1.image = pictureboxB1.image
pictureboxA2.image = pictureboxB2.image
pictureboxA3.image = pictureboxB3.image
I did a similar thing with TextBox controls, but this code isn't working for me:
For i = 0 To 2
Me.Controls("picturebox" + "A" & i + 1).image = Me.Controls("picturebox" + "B" & i + 1).image
Next i
When I run this, I get an error stating .image is not a member of 'System.Windows.Forms.Control'.
i know that is pictureboxA1.image = pictureboxB1.image.
but if i try like this
For i = 0 To 2
Me.Controls("picturebox" + "A" & i + 1).image = Me.Controls("picturebox" + "B" & i + 1).image
Next i
it says that image is not a member of controls.
because i have to do this pictureboxA1.image = pictureboxB1.image, 21 times.
like
pictureboxA1, pictureboxA2, ... 3, 4, 5, 6, 7 until 21.
and if i do it like this
pictureboxA1.image = pictureboxB1.image
i have to right the code 21 times and if i do it with for its just one time xD.
and i tryed .picture it says the same thing
Sorry, I should have seen this sooner. You need to set the control to a PictureBox in order for it to recognize the property. I think. Something like this might work. I don't have access to a PictureBox control, so it's a guess:
Dim pic as PictureBox
For i = 0 To 2
set pic = Me.Controls("picturebox" + "A" & i + 1)
pic.image = Me.Controls("picturebox" + "B" & i + 1).image
Next i