ListBox get value from index [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a program where you insert some numbers in a listbox and then a button click should get the values from the listbox and check whether they are positive or negative and display each other's count in a textbox.
I tried getting the value by: string x = listBox1.Items[index].Value; but it doesn't seem to work.

If you add the items to the list box as such:
listBox1.Items.Add(textBox1.Text);
Then you can retrieve the item from given index as follows:
string x = listBox1.Items[index];
The indexer is returning the value, which in that case is a string. Probably you might need to cast it to string, because the indexer actually returns object - see here: ListBox.ObjectCollection.Item Property :
string x = (string)listBox1.Items[index];

You can also try
string s = (string)listbox1.Items.GetItemAt(index);
You have to cast it to string because it's returning an object

Related

Is there a way to compare the exact value of a string in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
So I made a simple c# file reader because I was bored and added an if statement to filter the results. But when I ran it, it gave me more results than I wanted. I was supposed to get
276, 2, and there was only one line inside the file with that value, but I got multiple. I checked the file and it had lines ending with the same value. I tried string.Equals(line, "276, 2") but it gave me the same results. I doubt there isn't something in c# that doesn't solve this issue.
You can use Regex, as it's mentioned in this issue
bool result = Regex.IsMatch(line, "\\b276, 2\\b");

SelectedRows and DataBoundItem cannot be used like a method c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How Do I convert this line of code to c# properly as the SelectedRows and DataBoundItem cannot be used like a method.
Dim RecordValue As Int32 = grdList.SelectedRows(0).DataBoundItem.Item(_ValueMember)
The DataBoundItem is typed as Object, you cannot apply an indexing to this property. Instead if you have binded a DataTable/DataView then you can cast the DataBoundItem to the appropriate object and then, if that class offers indexing you can use indexing
DataRowView rowView = grdList.SelectedRows[0].DataBoundItem as DataRowView;
int recordValue = Convert.ToInt32(rowView[_ValueMember]);

String to decimal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The application that I've made I've got a text box called service cost. This allows the user to enter the cost of the service they have provided and this is decimal. I'm trying to get this service cost displayed in a DGV, I've got everything else working apart from this.
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost);
The above is the code that I currently have, is there something wrong that I've done here?
From the question it is clear that txtServiceCost is the TextBox, and Convert.ToDecimal() expects a string as input so you should use txtServiceCost.Text instead for txtServiceCost. Since txtServiceCost is a control where as txtServiceCost.Text is a string
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost.Text);
But i would like to suggest you to use decimal.TryParse
decimal userInput;
if (!decimal.TryParse(txtServiceCost.Text, out userInput))
{
// Throw some warning here that invalid input
}
currentComputer.ServiceCost = userInput;

Save price as decimal or string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an ASP.NET web project. I have a "price" field in my database, which is defined as decimal.
When the user inserts price in a textbox, the string from textbox is saved in the "price" field (I force the user to input only digits).
All data is presented in a ListView. The issue is when the user doesn't provide any value (empty textbox), I get an exception.
I can understand why this happens: because it can't cast an empty string to the decimal type. If the user didn't provide any value, I want to present an empty field in the ListView. How can I achieve that?
I suggest to have a default value 0.00 than an empty or null value. If user did not provide any value, validate it and convert the value of the textbox to 0.00.
If you don't want this suggestion check your DB then allow null the decimal field.
This is your own logical validation. However you can set your price field to nullable or check your textbox if has no value then set your price to 0.00

What does this C# code mean? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
FunctionThatReturnsAList(cmd)[0]
It is short-hand for:
List<Whatever> list = FunctionThatReturnsAList(cmd);
Whatever whatever = list[0];
The return type of FunctionThatReturnsAList is an object, like a List or an array that can be accessed via an indexer. The code is calling the function, which is then returning the List or array and then using the indexer to reference the first element in the collection.
An example would be:
var cmd = "123";
var returnedObj = FunctionThatReturnsAList(cmd)[0];
private List<string> FunctionThatReturnsAList(cmd)
{
return new List<string> {cmd};
}
The function returns a list, and you just access element 0 in the returned list.
Seems like cmd is an SQL command which returns may be array of some kind like DataTable[] and this function gets only first element(DataTable) from the array.
This statement can be used for all methods whose return type has a numeric indexer (e.g. lists or arrays).

Categories

Resources