Reading back ListBox values and setting default - c#

I'm new to this site, and basically I have created a Windows Form using C#.
On this form I have 3 values.
I have a button to access the values in my ListBox.
How could I make it so when the form loads, the first element in my list box is highlighted?
I was also wondering is there a way to start the indexing off for 1, if the first element in my list is selected? rather than 0?
I hope I have stated this as clearly as I should,

To select the first index, add this line to the OnLoad event of the form:
myListBox.SelectedIndex = 0;
As for your second question about changing the indexing range, the answer is no. You could use 1 to whatever, but then you would have to use -1 after. Like so:
myListbox.Items.Remove(Your_Value - 1);

Related

How to make a listbox in c# not duplicate items on button click?

Well, i'm using ASP.NET c# listbox. I also have a button, the idea is the following: whenever I click the button an item x has to be shown in the listbox. However, I click the button many times, and instead of replacing the x value in the list for the new value (pretend it's y), it shows the following:
x
y
The expected result would be:
y
I mean, i don't want values to be on top of eachother, just one at a time.
You should be having a code behind file where the selected items (selected by clicking the button) should be getting added to the listBox object.
When you run a loop to add items to this listBox object, use the Contains method to check if the listBox object already contains the current item. Add only if it does not contain.
At code behind :
Before that you need to create list and store all the listbox value. if listbox doesn't have value then keep list as null.
Every time you need to check whether the list contain the value or not if list contain value then you don't need to add, if not contain then you need to add.
in Jquery :
on button click first get all the listbox list item value and check that in that list whether that list contain the value or not which you want to add.
May be this is help full for you.

I need a multi-column control that only displays one column in c#

In my project I need a control that allows me to enter 2 columns.
First column is an Id Number
Second column is some Text.
Example...
row 1 Id = 1 Text = Day Shift
row 2 Id = 2 Text = Night Shift
But I only want to display the Text Values and then have the user select either Day Shift or Night Shift.
The program can then just lookup the corresponding value (1 or 2) for whatever text they chose.
- In Microsoft Access I would just have used a ComboBox and hidden the first column.
I cannot find anything in Visual Studio 2017 to put on my Windows Form that easily does this.
I want to set it all up at design time and the closest I have come so far is by using a LISTVIEW control using a display type of LIST (I don't want column headers either.
It seems to do what I want except that it always wants me to leave a blank space below my lines of text - presumably for a horizontal scrollbar even though I set it to False in the properties and it does not actually display a scroll bar.
If I resize the ListView control to just be big enough for my two rows of text it tries to display the 2nd row next to the 1st row and still leaves a blank space below the rows.
See the below images, assuming I uploaded them properly, I am totally new to asking questions here.
Is there a way I can achieve this - should I be using a different control?
The closest I came to what I need is the top image
[![enter image description here][1]][1]
Thanks and I hope it all makes sense.
In reply to Harry I added the below...
After adding the ListView control to my form I then clicked on the little arrow selector (in the top right of the ListView control).to bring up the collections list popup box.
It is there I set the view to be ‘List’ so I don’t have to have column header, then I clicked on EDIT ITEMS to get another pop-up screen…Click the [Add] button to add items in the rows.
Then I changed the Text to ‘Day Shift’ for the first member (and ‘Night Shift’ for the second member).
To add values in the rows for the 2nd column I then clicked the SUBITEMS (collection) box in properties to get the next popup…
I then clicked the Add button and created a new Text item (value 1)
and repeated this for the Night Shift member but gave it a value of 2.
These values are to be the actual ID values (in the 2nd column).
Note…
I did create column Headers but setting the View to List means I don’t get them displayed (and don’t want them displayed).
but I did notice that changing the View to List also removes the second column (Id) from the ListView display.
As I don’t need the Id values (1 & 2) displayed I am not going to worry about what happened to them disappearing in the display. All I did, though, was just changed the View from Details to List.
Sorry If I’m misunderstanding you but at this point there is no actual code I am creating, no doubt Visual Studio 2017 does that itself but I am not looking at that. Is that what you needed to see?
Hope this makes better sense though, I did try and include images but the system will not let me do that until my reputation points are higher, sorry.
Thanks

how to change starting position combobox C# mysql

I have a little trouble using C# combobox with MySql
So, to begin with... I have combobox and button on a form. After submitting a form, executes MySql query like :
"select * from db1.users where login='"+combobox.SelectedIndex+"')";
It works great, But.... if we submit the first element of combobox - MySql FOREIGN KEY problem occures (= NULL and so on).
Any Ideas how to give him know, that first element from combobox isn't NULL ?
Stupid idea but who knows:
Mysql counts from 1, combobox from 0 did you try:
(selectedIndex + 1)
I'll assume you're talking about just submiting the button without actually selecting a value on the combobox, right?
You should do a simple if before executing your command, like:
if (combobox.SelectedIndex != null)
{
codethis("select * from db1.users where login='"+combobox.SelectedIndex+"')");
}
else
{
//do something else
}
Are you sure what you want isn't combobox.SelectedItem?
This would be the case if "login" was a string in the SQL database.
If it's an integer, have you tried doing combobox.SelectedIndex+1? This could fix the problem if "login" starts at "1" in the SQL whilst SelectedIndex starts at 0 in C#.
In any case, check the answer below if your problem is the combobox having no selected item.
Optionally you can have the form OnLoad do combobox.SelectedItem=1;
You would want to stop using SelectedIndex, because it's not meant to represent your data. The problem may not even be in your current situation (mismatch between 1s and 0s indexing). Consider the following:
Another developer adds sorting to your combox. Now instead of a,b,c the values are c,b,a and the selected index of 0 will now point to c instead of a as it has used to do
Somebody removes users in the database, so in the database you now have b,c,d, think about what data you will have in your combobox?
To fix this problem you should use a value. Each combobox should have: displayed text, corresponding value (which is hidden from user) and selected index. Using selected index (or selected item) you get the item's value which should be your database_id. For example see this answer if the value is not available for your combobox type out of the box.
Bonus points:
use parameterised queries (prevents SQL injection attacks)
never store passwords, instead store password hashes and random salts (read more)

How to get values from dynamically created elements using jquery in code behind

Hi and thanks for taking the time to asnwer my question.
I have the following problem. I have a form and a button which says "add new activities".
Whenever the button is clicked I add a new set of elements, namely 2 drop down menus + text area. How can I get the values of these newly created elements in code behind since I cannot know their ids up front?
If there is something unclear about my question, please let me know.
Thanks again!
But you must be setting id's (more importantly - name attributes) of new elements using certain pattern. Use the same pattern in a loop in server-side code to get values from Request.Form. Provide a hidden input where you put the total count of items added for the server-side to know the upper bound of loop counter.
You should set the ids when you create the elements if you plan to access them again
So if you this is your text area:
var textarea = document.createElement('textarea');
You can set the id to like this:
textarea.id = "taId";

How to disable dropdown list value in the current form that was selected in the previous page

I am having 2 web formsand will have a drop down list on those web forms. If i select a value from drop down and click on ok i will get tranfer to next page. In that page i will have a drop down with the same values in the previous form . What i need is i would like to disable the selected value in the previos form and would like to display the remaining normal
I dont know your exact scenario but i would suggest that you look at whether you can maybe achieve your needs using a single form instead of two.
If that is not an option :-
If you are POSTING the form on click of OK, you can retrieve the value of the original page using PreviousPage. Refer eg below.
I am not quite sure what you mean by "disable the selected value in the previos form and would like to display the remaining normal" but once you retrieve this value, then you can manipulate the data in your current page any way you want.
DropDownList oldValue = (DropDownList)PreviousPage.FindControl("DropDownOldValue");
oldValue.SelectedValue - should then give you the value selected on the previous page
Got the answer
DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");
string str = oldvalue.SelectedValue.ToString();
ListItem i = DropDownList1.Items.FindByValue(str);
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");

Categories