Open picturefiles with Openfiledialog and list them in CheckedListBox without filepath and show fullpath in Textbox for each item when selected VB - checkedlistbox

I am using Visual Basic code:
Openfiledialog > I want to keep using this.
CheckedListBox
Textbox
Picturebox
My code below is working but I want the CheckedListBox to show only the filename(safefilename) for each image.jpg item from folder and show the fullpath with filename for each CheckedListBox.item selected to show in my textbox, I don't know and can't find the right code for this.
(now I am using the fullpath aka openfiledialog1.filenames) somehow I need to combine the openfiledialog.safefilenames with openfiledialog.filenames..
my code sofar:
Private Sub Button1_Click(sender As Object, e As
EventArgs) Handles Button1_Click
openFiledialog1.Filter = "Pictures(*.jpg*)|*.jpg"
If openFiledialog1.ShowDialog =
Windows.Forms.DialogResult.OK Then
For Each f As String In openFiledialog1.FileNames
SuspendLayout()
CheckedListBox1.Items.Add(f.ToString)
ResumeLayout()
TextBox1.Text = f
For i As Integer = 0 To
CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i,True)
Next
end if
Private Sub
CheckedListBox1_SelectedIndexChanged(sender As
Object, e As EventArgs) Handles
CheckedListBox1.SelectedIndexChanged
TextBox1.Text = CheckedListBox1.SelectedItem
If Not CheckedListBox1.SelectedIndex < 0 Then
Try
Dim img As Image =
Image.FromFile(CheckedListBox1.
SelectedItem.ToString())
PictureBox1.BackgroundImage = img
Catch ex As Exception
End Try
End If

Related

a method to prevent duplicate entry in multiline textbox

So I try to write a a code to prevent duplicated entry from register at all. So I've tried this but it doesn't work like how I want it to.
Private Sub NoDuplicate1()
For Each line As String In Me.TxtResult4.Text.Split(vbLf)
If line = TxtResult4.Text Then
LblMsg.Text = ""
TxtResult4.Text = TxtResult4.Text.Remove(TxtResult4.Text.LastIndexOf(Environment.NewLine))
End If
Next
End Sub
I've also try this code and put in postback and it work:
If Not IsPostBack Then
TxtResult3.Text = String.Join(Environment.NewLine,
TxtResult3.Text.Split({Environment.NewLine}, StringSplitOptions.None).Distinct())
TxtResult4.Text = String.Join(Environment.NewLine,
TxtResult4.Text.Split({Environment.NewLine}, StringSplitOptions.None).Distinct())
End If
but the problem that I have with the code is that the duplicate data only deleted when the page refresh. when what I want to do is to block/prevent the duplicate data from being enter at all . Is there any suggestion on how I can modified my code ?
This should remove any duplicate lines in the TextBox.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim splitter() As String = {Environment.NewLine}
Dim lines() As String = TextBox1.Text.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
Dim dupFree = lines.Distinct.ToArray
TextBox1.Text = Join(dupFree, Environment.NewLine)
End Sub

how to get the text value of dynamically created textbox

I want to get the value of my textbox that I created dynamically when I click a button
I need to do this cause the value of my textbox is used for retrieve data from database
how could I achieved this thing??
the flow is Button click - creating textbox - filling textbox with value - Button Click - Get Text of textbox
here is my code to make the textbox
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 4
textbox = New TextBox With {.ID = "TextBox" & i}
plchld.Controls.Add(textbox)
Next
End Sub
I have tried something like this but the code didn't work
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
thanks in advance for any help
edit for the answer
I've found the way to solve this. I use request.form to get the value of my textbox.
Thanks for anyone that participating
Regards,
Julian
This is how I have done in my asp.net application.
Creating dynamic control
TextBox txtDate = new TextBox();
txtDate.EnableViewState = true;
txtDate.ID = "PreixValue" + 1;
txtDate.Text = "07 Feb 2014"
pnl.controls.add(txtdate);
To retrieve the value from that textbox
DateTime datefrom = DateTime.Now ;
for (int cnt = 0; cnt < Request.Form.Count; cnt++)
{
if (Request.Form.AllKeys[cnt].Contains("Prefixvalue"))
{
int ParamStartPoint = Request.Form.AllKeys[cnt].IndexOf("Prefix") + 4;
int ParamNameLength = Request.Form.AllKeys[cnt].Length - ParamStartPoint;
string[] ControlName = Request.Form.AllKeys[cnt].Substring(ParamStartPoint, ParamNameLength).Split('$');
if (ControlName[0] == "Date From")
{
datefrom = DateTime.Parse(Request.Form[cnt]);
//datefrom has value now
}
}
}
This is how I have done in my web application, but there may be other ways achieve this.
basically when you create Dynamic control in webform this will be available through Request.Form.
hope this helps you.
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
Dim anotherObj As TextBox = Me.Controls.Item("Textbox" & i)
a(i) =anotherObj.Text
Next
The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchld control. You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes.
Using your code, all you need to do is on postback determine if the textboxes were created and if so, recreate them.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Determine if the text boxes were created and if so, recreate them.
If CBool(ViewState("TextBoxesCreated")) Then
CreateTextBoxes()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
CreateTextBoxes()
ViewState("TextBoxesCreated") = True
End Sub
Private Sub CreateTextBoxes()
For i As Integer = 0 To 4
plchld.Controls.Add(New TextBox With {.ID = "TextBox" & i})
Next
End Sub
Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OkButton.Click
Dim a(4) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
I don't know the full extent of what you are doing but I would suggest not creating them dynamically if you don't need to. Just show or hide the textboxes instead.
Reference: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

ASP.NET Control added to Placeholder lost values right after adding

Working over 5 hours on the following problem:
Private Sub ModulEdit_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim modulid As Integer = 1
loadeditors(modulid)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub loadeditors(modulID As Integer)
PlaceHolder1.Controls.Clear()
Using dbContext As New EntitiesModel()
Dim mps As List(Of ef.Modulparameter) = dbContext.Modulparameters.Where(Function(c) c.ModulID = modulID).ToList
Dim mmid As Int16
If EditMode.Checked = True Then
mmid = RadComboBox3.SelectedValue
End If
Dim mp As ef.Modulparameter
For Each mp In mps
Dim lbl As New Label
lbl.Text = "<BR>" & mp.Name & "<BR>"
PlaceHolder1.Controls.Add(lbl)
Select Case mp.Editor.Name
Case "textbox1line"
Dim con As New TextBox
con.ID = mp.ID
If EditMode.Checked = True Then
Using dbContext2 As New EntitiesModel
Try
Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
con.Text = mpa.Valuestring
Catch ex As Exception
con.Text = "AAAA"
End Try
End Using
End If
PlaceHolder1.Controls.Add(con)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)
Case "radeditor"
Dim con As New RadEditor
con.ID = mp.ID
con.ToolsFile = "\admin\controls\ToolsFile.xml"
'con.CssFiles.Add("\Content\frenzy\css\frenzy-orange.css")
If EditMode.Checked = True Then
Using dbContext2 As New EntitiesModel
Try
Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
con.Content = mpa.Valuestring
Catch ex As Exception
con.Content = "BBBB"
End Try
End Using
End If
PlaceHolder1.Controls.Add(con)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)
End Select
Next
End Using
End Sub
I add the control dynamicly, calling the codepart above in pre_init (tryed in load and init too with same result)
The value (text) for the control is there until that line PlaceHolder1.Controls.Add(con)
After the con.text is empty.
The control is added after, but with no value.
Strange, that in the same proc i add another control (label), where the text value is on the page after.
Adding additional info:
the control value (text or content), when debugging the LoadEditors), is allways correctly set. But then on the page both (textbox and radeditor) are empty
The routing is called from pre init, as described in a lot of related posts.
You are calling loadeditors in ModulEdit_Init. Shouldn't this be LoadControls ?
I fixed it myself:
Adding "con.ViewStateMode = System.Web.UI.ViewStateMode.Disabled" before adding control to placeholder
Calling "loadeditors()" in RadComboBox3 too
much probably the problem was, that i loaded editors in page-load or init, which got the correct values, but then the RadComboBox3.SelectedIndexChanged event was called, which overwrote the values somehow
So my answer is not a real answer, but it works now (I hate such: it works, but i dont know why) ;)

VB.NET CheckboxList doubles when button is clicked. Why?

Every time I click the Delete Button, the check marks double in quantity:
Public Class About
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
For count As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(count).Selected Then
File.Delete("C:\Images\" & CheckBoxList1.Items(count).ToString)
End If
Next
End Sub
End Class
The checkboxlist isn't refreshed so that the checkbox that I deleted is removed from the checkboxlist. How do I accomplish that? Thanks!
On a button click, Page_Load is ran again, so the code that adds the checkboxes is ran a second time.
Add a check for Page.IsPostBack, and only add the checkboxes if it is not a postback.
If Not Page.IsPostBack Then
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If
(I hope syntax is right... Not used to VB)
the whole content of the Page_Load event handler has to be executed only once in your case, so rewrite it like this:
If Not IsPostBack Then
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If

iterate through NavigateUrls on Page_PreRender and change style

I'm not sure if this is at all possible (there may be a different way to acheive it) but is there a way to iterate though all hyperlinks on Page_PreRender and if the NavigateUrl matches the file name then I can add a class to the link to show this as the active page.
Or even better, iterate through all hyperlink NavigateUrls within a certain DIV.
I can do it individually but that would take too long as there are so many links and be too hard to manage:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim filePath As String = System.Web.HttpContext.Current.Request.Path
If filePath = "/" & hMembership.NavigateUrl Then
hMembership.CssClass = "active"
End If
End Sub
You can do something like this in the Page_PreRender:
Dim filePath As String = System.Web.HttpContext.Current.Request.Path
For Each Control As Control In Me.Form.Controls
If TypeOf (Control) Is HyperLink Then
With TryCast(Control, HyperLink)
If .NavigateUrl = filePath Then
.CssClass = "active"
End If
End With
End If
Next Control

Resources