I have a multi-line text box in my WPF project, and I want to change the specified line text. I used to do this easily in Win form, but there is no Lines option in WPF Text box. For example, I used to do this in Windows Form:
Dim lines As String() = VTextBox1.Lines
lines(Specific Line Number) = "This is a test."
VTextBox1.Lines = lines
But this piece of cod, not working in WPF textbox.
Do I have any chance that I do this in the WPF textbox?
I have no idea how to do that.
Do it "manually" by manipulating the string yourself:
Dim lines As String() = VTextBox1.Text.Split(vbLf)
Dim lineNumber As Integer = 5
If lines.Length > lineNumber Then
lines(lineNumber) = "This is a test."
VTextBox1.Text = String.Join(vbLf, lines)
End If
Related
Lets say I have a form with 100 textboxes, comboboxes and other controls and I want to check if any of the controls is empty. When I click on 'OK' button, I want the messagebox to show a list of errors example Textbox1 is empty, Textbox30 is empty and such.
I can achieve this by doing the tedious method where I check textbox1 and messagebox is shown, check textbox2 and messagebox is shown again and so on.
I want the messagebox to show only once. How can I achieve this?
What I have did is that I set up an array and store all the error messages to be shown later by selecting (example Msgbox(errMessages(3) + Environment.newline + errMessages(30)) and I know this is not the right way to do it as well.
Please and thank you in advance.
Here is a direct answer to your question:
You can store the empty controls in a list, and at the end create the message like this:
Dim empty_controls = New List(Of Control)
If TextBox1.Text = String.Empty Then
empty_controls.Add(TextBox1)
End If
If TextBox2.Text = String.Empty Then
empty_controls.Add(TextBox2)
End If
Dim result As String = String.Join(
Environment.NewLine,
empty_controls.Select(Function(c As Control) c.Name + " is empty"))
MessageBox.Show(result)
Here is even a better way to detect which text boxes are empty:
Dim empty_controls = New List(Of Control)
//The following line will search through all text boxes on the form
empty_controls.AddRange(
Controls.OfType(Of TextBox).Where(Function(c As Control) c.Text = String.Empty))
//Here you can add other kinds of controls with their own way of determining if they are empty
Dim result As String = String.Join(
Environment.NewLine,
empty_controls.Select(Function(c As Control) c.Name + " is empty"))
MessageBox.Show(result)
I'm making a WPF app and I want to add strings in ingredientsList to a TextBlock. But from my code below, It seems like the Textblock regconizes only the last string in the list. How can a display all strings on my list to the text box? or any suggestion for using other control instead of a TextBlock?
TextBlock txbDisplayIngredients = new TextBlock ();
List<string> ingredientsList = new List<string>();
for (int t = 0; t < ingredientsList.Count(); t++)
{
txbDisplayIngredients.Text = ingredientsList[t] + "\n";
}
It's because you're basically changing it in each iteration, and the only one you see is the last one ...
assuming you have 3 string one,two,three, you're running and telling it: "Oy, make it one", then "make it two", then "make it three", so it'll do it, and you'll end up with the text box saying: "three"
You want to either have all your strings as one multi line string, or use an observable collection of strings that you'll bind that textblock to.
You could also append the lines to the Inlines property of the TextBlock: more on this msdn page
I just found out, it works if i use this:
txbDisplayIngredients.Inlines.Add(" + " + ingredientsList[t] + "\n");
Hey all i have created a few text boxes and also buttons to go along with them at run-time.
The code for the button is:
Dim updateButton As New Button
updateButton.Name = "button_" & ticketTheRowNum & "_" & ticketRowNum
updateButton.Content = "UPDATE!"
updateButton.Height = 26
Canvas.SetTop(updateButton, 24 * ticketTheRowNum)
Canvas.SetLeft(updateButton, 330 + lblNotes.Width)
updateButton.Width = lblNotes.Width / 2 - 10
updateButton.Background = New SolidColorBrush(Colors.Green)
Grid.SetRow(updateButton, 0)
Grid.SetColumn(updateButton, 0)
Grid.SetZIndex(updateButton, 2500)
cavTicket.Children.Add(updateButton)
And the code for the text box is this:
Dim txtBlock As New TextBox
txtBlock.Name = "txt_" & ticketTheRowNum & "_" & ticketRowNum
txtBlock.Text = theHeader
txtBlock.TextWrapping = TextWrapping.Wrap
txtBlock.Width = lblNotes.Width - 5
txtBlock.BorderThickness = New Thickness(0)
txtBlock.TextAlignment = TextAlignment.Left
txtBlock.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
tmpExpander.Content = txtBlock
Now the button and text box displays just fine on the WPF but I am unsure of how to hook the button up to have it know whats in the text box and save its content. Currently since I am creating these at run-time I don't have access like I would if its was already on the form before it ran (I would simply just call the buttons click event and then within that I would call the text box content and save it).
If both of your code blocks come from the same class then just add a private property txtBlock (by the way why block and not Box?). So when you will create your TextBox control it will then be visible to all the functions in your class including the button event handler.
But as NETscape said. WPF is all about XAML, binding, and WVVM. So if you want to avoid headaches use them ;)
i need to find out the Type of control on the form. is it a TreeView, GroupBox or a Label
i am using
for(int i=0;i!=Properties->Controls->Count;i++)
{
Control^ Current_Control=Properties->Controls->default[i];
}
sorry for my English
You can iterate through the controls on the form and use TypeOf and GetType:
Public Sub ClearAll(ByVal frm As Control)
For Each C As Control In frm.Controls
Dim Ctl As Control = C
If ControlList.Contains(Ctl.GetType) Then
ClearAll(Ctl)
ElseIf (TypeOf Ctl Is TextBox) Then
Ctl.Text = ""
End If
Next
End Sub
This searches for the TextBox and clears the text. If it hits groupbox or other container it clears all the textboxes within it.
I've created some windows in wpf that I need to print to one xps document. Each window opens, loads the relevant data and then immediately closes. Currently I use the below code to create the xps:
Using doc = New XpsDocument(TempLoc, FileAccess.Write)
Dim writer = XpsDocument.CreateXpsDocumentWriter(doc)
Dim collator = writer.CreateVisualsCollator()
Dim Window1 As Window1 = New Window1()
Window1.ShowDialog()
Dim Window2 As Window2 = New Window2()
Window2.ShowDialog()
Dim WindowX As WindowX = New WindowX()
WindowX.ShowDialog()
collator.BeginBatchWrite()
collator.Write(Window1)
collator.Write(Window2)
collator.Write(WindowX)
collator.EndBatchWrite()
End Using
Dim doc2 = New XpsDocument(TempLoc, FileAccess.Read)
Dim seq = doc2.GetFixedDocumentSequence()
Dim window = New Window()
window.Content = New DocumentViewer() With {.Document = seq}
window.ShowDialog()
doc2.Close()
However the trouble with this approach is that the area printed varies between machines - I assume this is due to the local screen size being used etc.
Is it possible to make the program print the full window independent of the computer its on by modifying this code? Alternativly is there a better way to approach this problem?
Thanks for any help
I print with FixedDocuments by either appending or adding placed UIElements. I posted the full source code to my helper class here. You may find that breaking it down by UIElements gives you much better control over your exact printed output, though yes, it requires you to separate printing code instead of just emitting your window.
I've used this helper class to create some very nicely formated multi-page reports that always come out the same on every computer.