WPF VB.NET : how to insert a notepad in listbox - wpf

I already used the code as below.
Dim openfile1 = New Microsoft.Win32.OpenFileDialog With {.Filter = "Text (*.Text)|*.txt"}
If (openfile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
For Each line As String In File.ReadAllLines(openfile1.FileName)
ListBox1.Items.Add(line)
Next
End If
This code serves on windows form, when I use in WPF there are no errors but can not display the contents of the notepad on the listbox. there is no other solution

File.IO.ReadAllLines will return an array of strings and not a collection. You're better off using a For loop.
Dim openfile1 = New Microsoft.Win32.OpenFileDialog With {.Filter = "Text (*.Text)|*.txt"}
If (openfile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
For i = 0 To Ubound(File.ReadAllLines(openfile1.FileName),1)
ListBox1.Items.Add(File.ReadAllLines(openfile1.FileName)(i))
Next
End If
Foreach statement = You need a collection object

Related

Code not setting RichTextBox to a StringCollection item value

And I would like to share this issue with you.
This piece of code:
Dim np As New notepad
np.RichTextBox1.Text = My.Settings.SDBodies.ToString(ListBox1.SelectedIndex)
Is doing nothing. It is supposed to do what it is supposed to do. It is susposed to set the RichTextBox text to the StringCollection Item Value.
Yes, notepad and RichTextBox1 is defined.
RichTextBox1 is the RichTextBox I am talking about.
And notepad is the form RichTextBox1 is in.
Please help!
New code:
If My.Settings.SDBodies Is Nothing Then
My.Settings.SDBodies = New System.Collections.Specialized.StringCollection
End If
Dim np As New notepad
ListBox1.Enabled = False
For i = 0 To My.Settings.SDBodies.Count - 1
If i = ListBox1.SelectedIndex Then
np.RichTextBox1.Text = My.Settings.SavedDocuments(i)
ListBox1.Enabled = True
Else
' Do nothing
End If
Next

WPF - Search for Incremental Numbered Controls using Visual Basic

First time using WPF coming from WinForm and have been adjusting fairly well however I am stumped on one procedure I used quite commonly. This was the code that I used in WinForm to autopopulate controls that were labeled like so; label1, label2, label3, etc..
Dim lbl As Label
Dim matcheslbl() As Control
For i As Integer = 1 To 24
matcheslbl = Me.Controls.Find("label" & i, True)
lbl = DirectCast(matcheslbl(0), Label)
If matcheslbl.Length > 0 AndAlso TypeOf matcheslbl(0) Is Label Then
lbl.Text = "Data Here"
End If
Next
How do I use the same procedure in WPF? My hierarchy layout in the form goes from WrapPanel > StackPanel > Canvas > Controls
After modifying the code to my knowledge I get hung up on the Me.Controls aspect and cant find anything after extensively searching, or im not fully understanding it. This is my modified code...
For i As Integer = 1 To 24
Dim lbl As Label
Dim matcheslbl() As Control
matcheslbl = Me.WrapPanel.FindName("lbl" & i)
lbl = DirectCast(matcheslbl(0), Label)
If matcheslbl.Length > 0 AndAlso TypeOf matcheslbl(0) Is Label Then
lbl.Content = "Data Here"
End If
Next
This hangs up here..
matcheslbl = Me.WrapPanel.FindName("lbl" & i)
Any help on how to accomplish my previous procedure in WPF and give a detailed description on how to achieve it since I am very new to WPF
If you look at the intellisense on Me.WrapPanel.FindName you will notice it does not return an array, just a single object. I fixed your code with the following
For i As Integer = 1 To 24
Dim o As Object = wpMain.FindName("Label" + i.ToString())
DirectCast(o, Label).Content = "Data Here"
Next

How to read Data from Dynamically added TextBoxes to a FlowLayoutPanel?

I have added textboxes dynamically in a FlowlayoutPanel from SQL table like this
string query = "SELECT* FROM tblA";
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=DummyData;Integrated Security=True"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Label objLbl = new Label();
TextBox objText = new TextBox();
objLbl.Text = reader["A_Name"].ToString();
objText.Name = "txt"+reader["ID"].ToString();
pnlFlow.Controls.Add(objLbl);
pnlFlow.Controls.Add(objText);
}
}
}
It's working fine. Now the problem that I'm facing is when user enters some data in these textboxes. How do I get that data for further processing?
There are a number of ways you could do this, depends on how and when you need to get the values.
If you need to read them all at once, something like:
foreach(Control c in pnlFlow.Controls)
{
if c.Name.StartsWith("txt")
// process the text box
// you might want to use a more distinct naming pattern to be safe
...
}
If you need to process them individually and at different times, you could reference them by name in the Controls collection:
string textBoxName = "txt12345";
string valueText = ((TextBox)pnlFlow.Controls[textBoxName]).Text;
Of course both snippets need better error handling, but I'll leave that to you.

Conditional statements with sql query

I posted a question earlier but it is so complicated that I think that's the reason why nobody tries to answer it. So I decided to put this in the simplest way I can. So here is my code:
Private Sub frmcrc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = GetConnect()
Dim com As New SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
conn.Open()
com.CommandText = "Select * from tblCRClearance where crc_no = '" & frmComm.txtCRCNum1.Text & "'"
com.Connection = conn
dr = com.ExecuteReader
dr.Read()
txtCRCNo.Text = dr!crc_no
txtCLTAcctNo.Text = dr!crc_clientno
lblrootpayee.Text = dr!crc_rootpayee
txtPayeeName.Text = dr!crc_payeename
txtClientName.Text = dr!crc_clientname
txtProjName.Text = dr!crc_projectname
txtCommType.Text = dr!crc_commtype
txtGrossAmt.Text = dr!crc_grossamount
txtWTax.Text = dr!crc_withheld
txtCALType1.Text = dr!crc_caliqtype1
txtCALDate1.Text = dr!crc_caliqdate1
txtCALNo1.Text = dr!crc_caliqnum1
txtCALAmt1.Text = dr!crc_caliqamt1
txtCALDesc1.Text = dr!crc_caliqdesc1
txtCALType2.Text = dr!crc_caliqtype2
txtCALNo2.Text = dr!crc_caliqnum2
txtCALAmt2.Text = dr!crc_caliqnum2
txtCALDate2.Text = dr!crc_caliqdate2
txtCALDesc2.Text = dr!crc_caliqdesc2
txtCALType3.Text = dr!crc_caliqtype3
txtCALDate3.Text = dr!crc_caliqdate3
txtCALNo3.Text = dr!crc_caliqnum3
txtCALAmt3.Text = dr!crc_caliqamt3
txtCALDesc3.Text = dr!crc_caliqdesc3
txtCALIQ.Text = dr!crc_caliqtotal
txtNetAmt.Text = dr!crc_netamount
txtVoucherNum.Text = dr!crc_voucherno
txtCRLNum.Text = dr!crc_crlno
txtDate.Text = dr!crc_date
txtCPFNo.Text = dr!crc_cpfno
txtPreparedBy.Text = dr!crc_preparedby
txtCheckedBy.Text = dr!crc_checkedby
txtApprovedBy.Text = dr!crc_approvedby
txtGrossPrice.Text = dr!crc_grossprice
txtLess1.Text = dr!crc_lessprice1
txtDesc1.Text = dr!crc_lessdesc1
txtLess2.Text = dr!crc_lessprice2
txtDesc2.Text = dr!crc_lessdesc2
txtLess3.Text = dr!crc_lessprice3
txtDesc3.Text = dr!crc_lessdesc3
txtNetSellingP.Text = dr!crc_netsellingprice
txtRate.Text = dr!crc_rate
txtGrossRel.Text = dr!crc_grossrelease
txtDed1.Text = dr!crc_ded1
txtDDesc1.Text = dr!crc_deddesc1
txtDed2.Text = dr!crc_ded2
txtDDesc2.Text = dr!crc_deddesc2
txtDed3.Text = dr!crc_ded3
txtDDesc3.Text = dr!crc_deddesc3
txtDed4.Text = dr!crc_ded4
txtDDesc4.Text = dr!crc_deddesc4
txtDed5.Text = dr!crc_ded5
txtNetRel.Text = dr!crc_netrelease
txtCollectibles.Text = dr!crc_collectibles
conn.Close()
txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
txtCRLNum.Text = frmComm.txtCRLNo.Text
txtProjName.Text = frmComm.txtUnitCode1.Text
txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text
txtCommType.Text = frmComm.cmbParticulars1.Text
txtPayeeName.Text = frmComm.cmbPayee01.Text
Me.lblrootpayee.Text = frmComm.lblrootpayee.Text
End Sub
It works like this: the sql query gets the data with the crc number the same as in txtCRCNum1. It works just as how I want it to, but I have some issues about this. I have 30 textboxes just like txtCRCNum1, so there are going to be 30 different values of crc number, depending on which textbox will I get its value. I want to have a conditional statement wherein it checks if the textbox that should contain the crc number is empty. If it is empty, then the fields will display just do this:
txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
txtCRLNum.Text = frmComm.txtCRLNo.Text
txtProjName.Text = frmComm.txtUnitCode1.Text
txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text
txtCommType.Text = frmComm.cmbParticulars1.Text
txtPayeeName.Text = frmComm.cmbPayee01.Text
Me.lblrootpayee.Text = frmComm.lblrootpayee.Text
If not empty, then it will execute the sql query part.
How should I put up the conditional statement so that it will work as I have described?
You can add procedures and methods to forms just like any other class. In this case, since the CRCForm in all cases depends on a crc value, we will pass it in the constructor.
Public Class frmcrc
' a crc value property
Private _crcVal As String = "" ' I am guessing it is string
' the constructor
public Sub New(crcval As String)
' This call is required by the designer.
InitializeComponent()
_crcVal = crcval
End Sub
' etc
End Class
When the user clicks one of the 30 (?!) textbox/button pairs. This requires a form instance rather than the VB default instance (ie frmcrc.Show) which is bad anyway.
Dim f As New frmCrc(txtCRCNum1.Text)
f.Show
The form now has the value it needs to work with whether it is from TB1, Tb2 or TB22. The form need not know or care where it came from. The query would use _crcVal in the SQL.
The existing form load code then just needs to deal with cases where _crcVal is String.Empty. If it is not String.Empty then just post the frmComm parts (which probably ought to be a class) Else perform the query and display results.
You could also add a CrcValue property to the form to execute the query and display the results without creating a new form instance, just use the new crcvalue.
EDIT
A class for the "other" stuff - I cant tell what the data represents, I'll call it BaseAcct:
Public|Friend Class BaseAcct
Public Property AccountNumber As String
Public Property UnitCode As String
' etc
End Class
Newer versions support auto implemented properties where that is all you need to define a property. Older versions, you will need to code a Get and Set. You could also add procedures like FetchData and DisplayData to have the class get the data from one form and post it to another, specifying which form as an parameter. The ELSE you have to copy data from one Form to the other could then be:
Dim BAcct As New BaseAcct
' ... etc
If _crcVal <> String.Empty Then
' ...
Else
BAcct.Display(Me) ' let the class post the data to this form
End If
The crcVal can be one of the Properties in the class, but even if it is, I would require that it be passed to frmcrc in the constructor since that form seems to be meaningless without it. That is, your code should not be able to create a form instance without that value.
First thing, you should be aware of all the 30 textboxes ids'. I dont know where would you place the if statement. but i would suggest you to have a button_click event where you will check all the textboxes.
Another thing for all the different 30 textboxes, Do you have the same textboxes like txtCRCNo,txtCLTAcctNo,lblrootpayee,etc.,
If it so , then you can allow only one textbox at a time.
So make textbox_TextChanged event for all the textboxes,while typing into a textbox all other textboxes should be empty.
Note: put all the textboxes in a panel.
like,
Sub txtCRCNum1_TextChanged()
txtCRCNum2.Text=""
txtCRCNum2.Enabled=False
' and so on
End Sub
Now it is easy to make your solution on button_click
Sub button_click()
Dim ctrl As New Control
For Each ctrl In Panel1.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Text <> "" Then
'sql query with that textbox
End If
End If
Next

remove bulletstyle on WPF richtextbox selection

I've managed to applying bulleted list formatting possible, but how to remove it again?
How to detect if the selection is/contains a List?
Did I overcomplicate things? Is there a straightword way to convert a selection to a bulleted list and back?
Private Sub bullet(o As Windows.Forms.ContextMenuStrip, e As Windows.Forms.ToolStripItemClickedEventArgs)
Dim lst As New Windows.Documents.List()
lst.MarkerStyle = bullets(e.ClickedItem.Text)
If rtf.Selection.IsEmpty Then
lst.ListItems.Add(New Windows.Documents.ListItem())
Else
Dim li As Windows.Documents.ListItem
Dim lines() As String = rtf.Selection.Text.Split(vbCrLf)
For Each s As String In lines
li = New Windows.Documents.ListItem()
li.Blocks.Add(New Windows.Documents.Paragraph(New Windows.Documents.Run(s.Trim())))
lst.ListItems.Add(li)
Next
rtf.Selection.Text = ""
End If
Dim curCaret = rtf.CaretPosition
Dim curBlock = rtf.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
rtf.Document.Blocks.InsertAfter(curBlock, lst)
Dim vMove As Windows.Documents.TextPointer = Nothing
vMove = curCaret.GetNextInsertionPosition(Windows.Documents.LogicalDirection.Forward)
If vMove IsNot Nothing Then rtf.CaretPosition = vMove
rtf.Focus()
End Sub
I've since come to drop this code from my project because it's unreliable in certain situations. Would a solution based on dynamic XAML insertion be more reliable? Many aspects of WPF seem to very poorly conceived....
If you could use the windows forms version of richtextbox, you could use the SelectionBullet property.
http://msdn.microsoft.com/en-us/library/ms742875.aspx
Try EditingCommands.ToggleBullets.Execute(null, richTextBox) to toggle bullet style in selected paragraphs.

Resources