I'm trying to add some extra text to the following code but it doesn't seem to work whatever I do. The information I want to show in a label comes from a DataBinding. But I can't manipulate it like a usual string
lblPower.DataBindings.Add("Text", BindingSourceMachineProfiles, nameof(MachineProfile.NominalPower));
At the moment the label only displays a number but I would like to add before the description and at the back the units.
So I would like to have e.g. "Speed" + code from above + "km/h"
How could I do this using the current code?
I have found a solution by using Binding.Format Below I have made an example of how I use it in my app.
var SpeedBinding = new Binding("Text", BindingSource);
SpeedBinding.Format += delegate (object sentFrom, ConvertEventArgs convertEventArgs)
{
convertEventArgs.Value = "Speed: " + convertEventArgs.Value + " km/h";
};
lblPower.DataBindings.Add(SpeedBinding);
This gives me nice results
Related
i created a windows forms-app with visual studio and im programming in c#.
I have one textbox, one richtextbox and one button. If im writing something in the textbox and press the button, the text from the textbox is shown in the richtextbox (just like a simple chat). My problem is, that if i already wrote something and add something new, the old text in the richtextbox gets replaced by the new one. I want that everything i wrote is among each other.
My source code:
string message = textbox1.Text;
richTextBox1.Text = "Name:" + message;
Of course; you set the Text of the richtextbox to a new text each time you access it
Consider using richTextBox1.AppendText(message) instead
Using the equals operator replaces the value in the .Text property. If you'd like to append text, use the += operator:
string message = textbox1.Text;
richTextBox1.Text += "Name:" + message;
Alternatively, you can skip the shorthand and say:
string message = textbox1.Text;
richTextBox1.Text = richTextBox1.Text + "Name:" + message;
Or, use AppendText():
richTextBox1.AppendText(message)
I have a winform that pull data from database into a DataGridView and Databind it to several text box and a PictureBox. I have a databinding and data loading method to reload and rebind when there is change in the data as below.
private void LoadData()
{
DataTable dtDS = new System.Data.DataTable();
dtDS = prodController.GetData();
dgvProduct.DataSource = dtDS;
}
private void DataBinding()
{
txtProductID.DataBindings.Clear();
txtProductID.DataBindings.Add("Text", dgvProduct.DataSource, "ProductID");
txtProductName.DataBindings.Clear();
txtProductName.DataBindings.Add("Text", dgvProduct.DataSource, "ProductName");
...
bxImage.DataBindings.Clear();
bxImage.DataBindings.Add("ImageLocation", dgvProduct.DataSource, "ImagePath");
}
At first I stored the absoluted path of the image in the database as string and it worked fine binding the data into the control. But after that I want to only store the image name in the database, the image themselves are moved to an Image folder in the Application startup folder so that I can load the Image like this:
bxImage.Image = Image.FromFile(Application.StartupPath + #"\Image\Student\" + imagePath);
I followed Databinding a label in C# with additional text? to add like below to my DataBinding method:
var binding = new Binding("ImageLocation", dgvProduct.DataSource, "ImagePath");
binding.Format += delegate (object sentFrom, ConvertEventArgs convertEventArgs)
{
convertEventArgs.Value = Application.StartupPath + #"\Image\" + convertEventArgs.Value;
};
bxImage.DataBindings.Add(binding);
It worked as first but after each time the LoadData and DataBinding are called the string keep adding onto each other making the path invalid. Even if I called the DataBinding clear and reset the format it still adding each time the methods is called. Is there a way to properly do this or should I use CellClicked method of DataGridView to get the image to load into the PictureBox?
I know this is long overdue but I should update the answer for anyone who stumble upon this problem the same as me. Thanks #TaW for the answer. In the binding.Format change the code to this:
String path = convertEventArgs.Value.ToString();
if (!path.StartsWith(Application.StartupPath + #"\Image\"))
{
convertEventArgs.Value = Application.StartupPath + #"\Image\" + convertEventArgs.Value;
}
This should prevent the string to be added each time the DataBinding is called and help bind the data to the control properly.
I have a car class with four attributes (colour, model, mileage, year) and I get these attributes from four text boxes. I then assign these attributes to a new object of the car class and assign this new object to an object array in the car class. I need to be able to highlight the mileage attribute but I am outputting in a string builder like this it like this:
private void button1_Click(object sender, EventArgs e)
{
carColourIN = colour.Text;
carModelIN = model.Text;
carMileageIN = Double.Parse(mileage.Text);
carYearIN = Int32.Parse(year.Text);
Car user = new Car(carColourIN, carModelIN, carMileageIN, carYearIN);
Car.vehicles[i] = user;
StringBuilder sb = new StringBuilder();
sb.Append("Colour: " + Car.vehicles[i].CarColour + "\nModel: " + Car.vehicles[i].CarModel + "\nMileage: " + Car.vehicles[i].CarMileage
+ "\nYear: " + Car.vehicles[i].CarYear + "\n\n");
displayLabel.Text = sb.ToString();
i++;
}
how would I highlight the mileage attribute in the appended string builder so that either the background of that particular section of text is a different colour or the actual text is.
I see a few different options. The answers to this question Multiple Colors in C# Label explain the first two options.
Option 1: Do not use your StringBuilder, and use a rich text box. Then loop through the properties and add them one at a time using the rtb_AppendText function in the provided link.
Option 2: Create a custom label control and override the OnPaint method. In the OnPaint method you can use MeasureText and DrawString to accomplish your goal.
Option 3: Create a user control with four different labels, this way you could control the color/font/appearance of each label, and in the long run gives you the most flexibility.
I want to get the data from my serial port and display this data in a textbox. but when i run mu code it displays just one line in the textbox and it gets replaced by the next. but I want the each part of the string under the next one.
private void button1_Click(object sender, EventArgs e)
{
SerialPort serP = new System.IO.Ports.SerialPort("COM3", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
while (true)
{
serP.Open();
serP.WriteLine("test");
string dataIn = serP.ReadLine();
textBox1.Text = dataIn;
serP.Close();
}
}
this is my code, I hope some one can help me out with this.
Rick
Just concatenate your text:
textBox1.Text += dataIn + Environment.NewLine;
And make sure your textbox is multiline (textBox1.Multiline = true for standart Windows.Forms.TextBox or somethimg similar if it's textbox from some controls library)
I would go with Andy Korneyev's answer. But if you want each part in a new line you could alter his code like below,
textBox1.Text += dataIn + Environment.NewLine;
Also if you want to set the textbox as MultiLine in code behind,
textBox1.TextMode = TextBoxMode.MultiLine;
Note:
I assume you are working with WinForms. If so, you can also use RichTextBox control. It doesn't need to be set has MultiLine.
I agree with Pavan. If you want to use more than one line i would prefer RichtTextBox, too.
Also if you only want to display something, you could use a simple label, too.
I think thats much nicer for a user than to have it in a TextBox, but only if you want to display something without using it in other things.
I have to add details of my file into a multiline textbox. But all the details are getting added in a single line in the text box and not in a vertical sequence. I used Environment.NewLine and also used "\r\n", but it's not of any help. I have ticked the multiline text box in a Windows Forms form and also set it to true but to no avail.
My line of code is like this:
m_Txt.Multiline = true;
m_Txt.Text = fileInfo.m_Title + "\r\n" +
fileInfo.m_Identifier + Environment.NewLine +
fileInfo.m_TotalTime;
A cleaner answer is:
Assuming txtStatus is a textbox:
txtStatus.Multiline = True;
txtStatus.Clear();
txtStatus.Text += "Line 1" + Environment.NewLine;
txtStatus.Text += "Line 2" + Environment.NewLine;
Using the built in enumeration means cleaner code.
Shift+Enter
In the Visual Studio resource editor, you can hit "Shift + Enter"
to create a new line, as doing something like "\r\n" will get escaped
out. You will also need to increase the cell height to see both
lines as it does not auto-size.
If you're doing it programatically, append the new line to m_Txt.Lines, which is a string[].
m_Txt.Lines = new string[]{ fileInfo.m_Title, fileInfo.m_Identifier, fileInfo.m_TotalTime};
Not sure why your code would not work unless something else is going on.
I just created a WinForms project using C#, added a textbox, set it multiline and added the following code - works a charm.
textBox1.Text = "a\r\nb";
I just wrote this code, seems to be working fine.
public void setComments(String comments)
{
String[] aux;
if(comments.Contains('\n')) //Multiple lines comments
{
aux = comments.Split('\n');
for (int i = 0; i < aux.Length; i++)
this.textBoxComments.Text += aux[i] + Environment.NewLine;
}
else //One line comments
this.textBoxComments.Text = comments;
}