How to display image in text box in vb.net? - vb.net

I have this code
Dim sb As New StringBuilder
For Each c As String In TextBox2.Text
sb.AppendFormat("<img src='{0}.jpg'/>", c)
sb.AppendLine(Line)
Next
Textbox3.Text = sb.ToString()
Suppose i get image inside my textbox3 but it didn't execute. It just come out the url of the image. how I'm going to get it. I had used literal.text but my image doesn't execute line by line i want my image execute like image below

Try to use a WebBrowser control to display your images:
Dim sb As New System.Text.StringBuilder
For Each c As String In Me.TextBox1.Text
sb.AppendFormat("<img src='{0}.jpg'/>", c)
Next
Me.WebBrowser1.DocumentText = sb.ToString
This is a simple code to start your project.
To obtain the same result in your image I think you also had to:
declare a better HTML and pass it to WebBrowser.DocumentText
use a multiline textbox and handle the carriage return

if you want the same effect, you could take a vertical slice of your composite image, set it as the css background-image of the textbox, with background-repeat:repeat

Related

Read CSV to series of existing textbox in Vb.net

I have a code to import csv to auto generated text box which was a part of my previous app. However I had to re do the whole script which involves importing csv to multiple existing textbox.
Below is my old code which worked like a charm but in this code my textbox were getting auto generated based on the numbers of value present in my csv.
Dim T(100) As TextBox
Using ofd As New OpenFileDialog()
If ofd.ShowDialog() = DialogResult.OK Then
TextBox1.Text = (ofd.FileName)
End If
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(TextBox1.Text)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim numer As Integer
Dim currentRow As String()
numer = 1
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If (currentField IsNot "") Then
Dim myTB As New TextBox
T(numer) = myTB
myTB.Text = currentField
myTB.Visible = True
myTB.Location = New Point(550, 92 + (numer * 28))
myTB.Name = "ADBox" + numer.ToString
myTB.ReadOnly = True
Me.Controls.Add(myTB)
numer += 1
End If
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End Using
But that created a lot of issue in my app hence I had to load the values on an existing textboxes(Multiple) but I am somehow not able to.
Edit1:
*** The code above creates a textbox and adds my csv values to it and what I am looking for is inject csv to existing textbox which I have created and not automatically generated text box.
For Example my code creates text box called ADUser1,2,3,4 and enters all the value but my following code which will create a textfile by fetching the values from the text box is not working because when I declare
My.Computer.FileSystem.WriteAllText(aduser1, ADBox1.Text, True)
it says it doesn't exists because when a form loads it never created such textboxes. This is the challenge I am facing
Any help will be a great value
Thanks
I agree that this is a very awkward design and should be redone, but for the purpose of answering your question...
The reason your code here: My.Computer.FileSystem.WriteAllText(aduser1, ADBox1.Text, True) doesn't find ADBox1 is that it is not created and referenced like an object you drag on to the form. It could be, by the way, but that is more work than dragging and naming 100+ text boxes on your form. Nuts. Creating the textboxes in code is better.
If you "manually" add one textbox to a form, then examine the designer-generated code for the form you will see that it created a textbox for you. You would find something similar to Friend WithEvents ADBox1 As System.Windows.Forms.TextBox. This is the reason you can reference the textbox in your form code. There is no magic here and you are technically doing the same thing in your code, here:
Dim T(100) As TextBox
...
Dim myTB As New TextBox
T(numer) = myTB
You can use the reference T(n) to refer to any of your textboxes. It is not clear where the WriteAllText function is but you may need to be sure Dim T(100) As TextBox is a form global, then change the WriteAllText line like this to get at ADBox1:
My.Computer.FileSystem.WriteAllText(aduser1, T(1).Text, True)

how to add captions to a picture programatically

How I Can add some text in pictures automatically?
-- I have a bunch of products and i want to add descriptions and price below of image . And I want to do this job by writing some line of code in vb.net
Because i have more than 2000 images which names is incremental numbers.
Here is a sample image:
sample
Like this image.
U can use(if u don't wanna use a database) a RichTextBox Control
Use this code to add image to the RichTextBox:
'add openfiledial to add image to RichTextBox
img = Image.FromFile(OpenFileDialog1.FileName)
Clipboard.SetImage(img)
Richtextbox1.Paste()
'add some text beneath your image.
Save the richTextBox to file :
Private sub Btn_save_click
'add a saveFileDialogBox
SaveFileDialog1.ShowDialog
Private Sub SaveFileDialog1_DialogResultOk
richTextBox1.SaveFile(saveFiledialog1.FileName, _
RichTextBoxStreamType.RichText)
This is one solution but there are other solutions.Comment if u need them
To load text from file
Richtextbox1.text=file.readalllines(use file location here/openfiledialog.filename)
USING A DATABASE IS THE PERFECT ANSWER
As u said, u have a text file containg the prices...I suggest u to use either MSSQL/ACCESS DATABASE/MYSQL
Now to generate records depending on the text file..Use this(load the textfile in the rtb first)
Dim con as new SqlConnection(Your connectionstring here)
Con.open
Dim cmd as new sqlcommand("Insert into [table name-remove brackets if required](columnname) values("#price")",con)
Dim i as integer
For each line in RichTextbox1.Lines
cmd.parameters.add("#price", sqldbtype.varchar).value=Richtxtbox1.lines(i).text
cmd.executenonquery
Next
Con.close

Saving Columns of listview in VB with separator

So I have a listview which has two columns. the listview view is details.
I have successfully imported a file into the list view with correct splits. The code i used is,
Using sr As StreamReader = File.OpenText( file path )
While (-1 < sr.Peek())
Dim line As String = sr.ReadLine()
Dim item As New ListViewItem(line.Split(":"c))
ListView1.Items.Add(item)
End While
sr.Close()
End Using
So this imports the lines from my file to the program into correct columns with : as split.
Now I also have a option for users to add data from my program to the file the same way, I used this code,
Using sw As StreamWriter = File.AppendText(file path)
For Each item As ListViewItem in ListView1
Dim line As String = Nothing
For Each entry As String in item.SubItems
line.Append(entry & ":")
Next For
sw.WriteLine(line)
Next For
sw.Close()
End Using
Taken from : Separating text from .txt into colums in listview (VB.net mobile)
But my bad, vb gives this error,
Error 1 Expression is of type 'System.Windows.Forms.ListView', which is not a collection type. C:\Users\xxxx\documents\visual studio 2012\xxxxx\Form1.vb 97
I am not sure why i am getting this error, is it because of my list views properties ?
I want to be able to save the data to the text file when user click a button.
This line:
For Each item As ListViewItem in ListView1
should be this:
For Each item As ListViewItem in ListView1.Items
and this line:
For Each entry As String in item.SubItems
should be this:
For Each entry As ListViewItem.ListViewSubItem in item.SubItems
You then get a String from the Text property of the subitem.

WP use string as name of control

Please, can anyone help me with this problem:
I have a name(s) of control(s) in string format (str) and I want to set property (in code) of that controls using that string-name.
I try something like this but it doesn't work. Actually, I have a problem with expression. When I put exactly the name it works but when i use variable in string format it doesn't.
Dim str as String
str="k3"
Dim g As Image = CType(str, Image)
g.Source = New BitmapImage(New Uri("/APP;component/Icons/hero.png", UriKind.Relative))
This works:
Dim g As Image = CType(k3, Image)
While this does not:
Dim g As Image = CType(str, Image)
I think I understand what you are trying to do, to declare an object by a string...
Essentially for this to work you will need a custom function that returns the Object Type that you are seeking...
You will need to loop through each control and check the name of the control as a comparison, e.g. If oControl.Name.ToString = sString then Return oControl
Example
' A function to return a Control by the Control's name...
Public Function GetControlByName(ByVal oForm As Form, ByVal sName As String) As Control
Dim cReturn As New Control
Dim ctrl As Control
For Each ctrl In oForm.Controls
cReturn = ctrl
If ctrl.Name.ToString = sName Then
Return ctrl ' this is what we want!
End If
Next
Return cReturn
End Function
' Example Usage
Dim oButton As Button = GetControlByName(Me, "Button44")
If oButton.Name.ToString = "Button44" Then
MessageBox.Show("I have found your Button!")
Else
MessageBox.Show("Your button was NOT Found!")
End If
Obviously there is room for error with this function, because if sName is NOT found, then it will return the last ctrl found, therefore, you will need to ensure that the control you seek is indeed found, via the If statement as provided in the example above...
Furthermore, it may not loop through controls inside of containers, menus, etc, but I'm not sure on that, so you will need to check to ensure it's not having that problem...
(The Me in the statement will most likely be used more often than not, though Me could be the name of the form you are searching if you are running the code outside of the form you are searching the form with the function.)
FINALLY, to answer your question, you will need to change Control to Image, and Set CReturn as a New Image, and then use Return ctrl.BackgroundImage (etc) to return the image..

VB.Net using databindings with a picture box

A row, in a data table, iscalled FirstImage contains a url to an image file on a web server. I am trying to bind the data of this row to the image source of the picture box.
My current code:
For Each row As DataRow In ListData.Rows
Dim ImageDecode = ser.Deserialize(Of PropertyImage())(row("Images"))
row("FirstImage") = "http://rental.joshblease.co.uk/propertyimages/" & ImageDecode(0).Image
'Returns http://rental.joshblease.co.uk/propertyimages/image1.jpg
Next row
TxtListName.DataBindings.Add("Text", ListData, "Name")
TxtListSlug.DataBindings.Add("Text", ListData, "Slug")
TxtListCreated.DataBindings.Add("Text", ListData, "Created")
ImgListItem.DataBindings.Add("Image", ListData, "FirstImage", True)
DataRepeater1.DataSource = ListData
But at the moment, the image is still blank. I have tried entering the location into a hidden textbox and copying the data over, but I can;t figure out how to use the controls in a data repeater.
This was the experimental copy from a hidden text box code:
If Me.DataRepeater1.ItemCount > 0 Then
Dim n As Integer = Me.DataRepeater1.ItemCount
For i As Integer = 1 To n
Me.DataRepeater1.CurrentItemIndex = i - 1
Dim item = Me.DataRepeater1.CurrentItem
item.Controls("ImgListItem").ImageLocation = item.Controls("TxtImageLocation").Text
Next
End If
Simply add Picture Box property ImageLocation
ImgListItem.DataBindings.Add("ImageLocation", ListData, "FirstImage", True)
The databinding for the image expects binary image data and in this case your passing it a string. What we can do is convert the image location into a format the binding can understand. Take a look at this link C# Code Snippet - Download Image from URL. Then once you have the image in memory, you will be able to bind it to your PictureBox.
Also, keep in mind that the simplest way shown in this answer would not work for you since URIs are not supported by the BitMap class.