C# Add value to combo box with identifier number - c#

I am currently developing a program in C# that contains a combo box and a button to add a new item to the combo box when said button is pressed. I need it so that when the button is pressed and the item is added, it gradually builds up a list of items that have a number that increases by one for each item.
For example when the button has been pressed twice, the combo box will contain the following two items:
New Profile 1
New Profile 2
Etc.
I am making it so that the items are set to "New Profile" with the number by default so that the end-user isn't confused as to which profile is which but the user can change the items name later if they wish to, but I am struggling making this concept work in code.
I had the generic code to add a item to my combo box:
private void AddProfileButton_Click(object sender, EventArgs e)
{
ProfileList.Items.Add("New Profile");
}
So, I tried drafting some code to see if I could accomplish my ideas on my own. This is my code:
int a = 0;
var b = a + 1;
var NewProfileName = "New Profile " + b;
ProfileList.Items.Add(NewProfileName);
However; when I tried this when the form was running, I kept getting items with the same name of "New Profile 1" repeatedly. So it sort of works - just that it doesn't increase the integer how I want it to.
I think part of the problem is the:
int a = 0;
part of the code however my attempt(s) to fix this (pasting this line of code under the Form_Load event) have been a failure/inconclusive.
I would greatly appreciate someone's help and all suggestions are welcome.
Josh

Thanks to #LarsTech for the answer.
The code is:
ProfileList.Items.Add(string.Format("New Profile {0}", ProfileList.Items.Count + 1));

Related

How do I make an increment textbox loop show on different label with buttonclick

I'm a freshman new to c# programming.
I'm trying to increment integer in textbox and show it on different label with every button click
This should be a simple task,but I've been stucking on this question for weeks,so I'm here to seek help
Any advice and suggestion will be great help.
Thank you for your help in advance.
This is what the final output should be look like:
keyin the number you want
first button click
second button click
third button click...etc
Assumptions:
It looks like the top label holds the current and the bottom label holds the previous
We can transfer the current to previous and calculate a new current
The user will always type good input so we don't need to get too heavy into error checking
The following code implements the assumptions:
void incrementButton_Click(object sender, EventArgs args){
int current = int.Parse(topLabel.Text);
int increment = int.Parse(incrementTextbox.Text);
bottomLabel.Text = topLabel.Text;
topLabel.Text = (current + increment).ToString();
}
Because you didn't post any code, the code above should be treated as pseudocode that demonstrates a concept; you will probably have to heavily modify it to meet your needs

My method only works when called from one particular area of my application [duplicate]

I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}

"next song" button is adding onto selected index instead of just moving it, any ideas?

So this "next song" button code has been working for a while and it recently stopped working. It's highlighting the next song but keeping the current song highlighted so it just played from the beginning of the selected index. Here is the next song code:
private void nSong_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
{
listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
WMPPlayer.URL = filepaths[listBox1.SelectedIndex];
}
}
I'm thinking it has something to do with the "SelectedIndex + 1" part of it because it's making the selected index two songs instead of one. Strangely enough, if you click it again it keeps it at two selected instead of going to three or more. Are there any tips you guys have on where I'm going wrong?
Thanks in advance
It seems as though you have the selection mode of the listbox wrongly configured. To set it to single selection mode use:
new ListBox().SelectionMode = SelectionMode.One;
This makes it impossible to select multiple items.
However, if you still want it to be possible to select multiple items, you can clear the selection before:
new ListBox().ClearSelected();
If this still doesn't work the problem isn't located in the snippet you provided - therefore we'd need to see more code.
I hope I could help you.

Programmatically retrieving a drop down box selection

Using VS 2012, c#, asp.net web form
Trying to create a calculator with a drop down box that determines the operator(/*-+) for two text box values.
The drop down box is called DropDownList1. I novice with C, only done some java. My question is, how do i check what operator has been selected via the drop down.
I have tried the following
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1 = * //or whatever ( obviously won't work for obvious reasons {
Then add textbox1 and textbox2 values
}
if (-)//and so on
etc...
Although the textboxes would be strings so I have no idea how to actually add the two values after I crack the drop down box :S)
I also tried to grab it via a list index of some sort? But i wasn't sure of the syntax however I tried stuff like
DropDownList1.SelectedIndex = something? // Wasn't sure where I was going here either
Any help would be great.
PS I wanted to use a drop down box even though it's silly
You should be able to just do:
DropDownList1.SelectedItem.Value
To get the value of the selected dropdown and then you can just do a case statement on what to do with the selected operator.
switch(DropDownList1.SelectedItem.Value)
{
case "+":
// do +
break;
case "-"
// do -
break;
}
if (DropDownList1.SelectedValue.Equals("your item value here"))
or
if (DropDownList1.SelectedItem.Text.Equals("your item text here"))

Accessing dynamically created textboxes text

I have stumbled across a problem with my asp.net form.
Within my form the end user chooses a number of textboxes to be dynamically created, this all works fine with the following code:
protected void txtAmountSubmit_Click(object sender, EventArgs e)
{
int amountOfTasks;
int.TryParse(txtAmountOfTasks.Text, out amountOfTasks);
for (int i = 0; i < amountOfTasks; i++)
{
TextBox txtAddItem = new TextBox();
txtAddItem.ID = "txtAddItem" + i;
txtAddItem.TextMode = TextBoxMode.MultiLine;
questionNine.Controls.Add(txtAddItem);
txtList.Add(txtAddItem.ID);
}
}
However this has also caused a small problem for me, later on in my form on the submit button click, I send the results to the specified person it needs to go to (using smtp email). Again this part is fine, until I am trying to retrieve the text from these dynamically created textboxes.
What I have tried
I have tried using this msdn access server controls ID method however this was not working.
I tried to add these new textboxes to a list, however I was unsure on how to update these textboxes when they have text in them. Therefore my results were returning null because of this.
I have also looked at other questions on SO such as this however they are usually for WPF or winforms, rather than my problem with asp.net (this usually isn't an issue, but I don't need to get the text from every textbox control in my page, just the ones that were dynamically created).
I have also tried changing how I call the code that I hoped would have worked:
string textboxesText = string.Join("\n", txtList.Select(x => x).ToArray());
and then in my concatenated string (email body) I would call:
textboxesText
The problem
As they are dynamically created I am finding it difficult to call them by their id for example: txtExampleID.Text, also as I have to increment the ID's by one each time (so they don't override each other) it has made things a little bit more difficult for me.
I am not asking for a code solution, I would prefer pointers in the right direction as I am still learning.
So to sum it all up: I need to get the text from my dynamically created textboxes to add it to my email body.
The issue is these text boxes need recreated in the Load event of the page, every single time, so that both events and values can be hooked back up and retrieved.
I think the most straight forward approach, in your case, would be to extend idea #1 that you had already tried. Build a List of these controls with enough information to recreate them in Load, but you need to store that List in either ViewState or Session.
ViewState["DynamicControls"] = list;
or
Session["DynamicControls"] = list;
I would use ViewState if you can because it gets destroyed when the user leaves the page.

Categories

Resources