C
C#2y ago
_Kai_

Stuck on some things involving listbox and textbox.

I have a simple application where the user inputs information into textboxes, and when they press a “save” button that information displays in a listbox. The idea is to create a record of customers for a store. The attached photo is my example. Right now Im trying to create the functionality for the “first” and “last” buttons, which are supposed to display the information from the list box in the text boxes. I don’t know how to go about doing that, though, and after about 3 hours of looking on youtube and google without getting any less stuck im coming here. Another thing i am stuck on is the counter next to customer # at the top and record # on the left in the bottom list box. What ive done to try and get it to count like it should is create a label where the counter will go and put this code under the save button: label6.Text += listBox1.Items.Count; it sort of works but instead of counting like 1, then turning into 2, and so on it just keeps adding numbers like 1, then 12, then 123. The functionality behind the first and last buttons are more important right now but i immensely appreciate any help or advice on any of this.
79 Replies
cap5lut
cap5lut2y ago
the label6.Text += listBox1.Items.Count; issue is a simple one: label6.Text is a string, string + anything will also result in a string, basically it calls string + anything.ToString(), eg "1" + 2 -> "1" + 2.ToString() -> "1" + "2" -> "12" u basically have 2 ways of doing that: either store a counter variable somewhere public int nextId = 1; and then do label6.Text = (nextId++).ToString(); (would prefer this), or convert the label's text to an int first: label6.Text = int.Parse(label6.Text) + 1; to the other part i come in ~30min got an important phone call rn
Pobiega
Pobiega2y ago
No, the counter issue you fix by using string Interpolation `Text = $"Customer #{listbox.Itema.Count}";
cap5lut
cap5lut2y ago
regarding the last/first functionality. usually u store the data in its own type and throw it in a list or something like public List<Customer> customers = new List<Customer>(); then i would write a method that takes a Customer object and updates the gui respectively public void UpdateGUi(Customer customer) then on first/last u simply get the respective customer object from the list (dont forget to check if the list is empty) and call UpdateGui() from there that wouldnt work, lets take the data from the screen shot as example, if they delete one entry thats not the last and they create a new entry, they end up with #4 twice with the counter and list approach u can also load data stored in a file so u can "continue" after the program was restarted
_Kai_
_Kai_OP2y ago
first of all thank you both for replying, and i tried all the suggestions for the counter and label6.Text = int.Parse(label6.Text) + 1; seems to work the best except i got one error message “cannot explicitly convert type int to string” so i think im missing a step
cap5lut
cap5lut2y ago
oh, right, u would have to wrap it in parenthesis and call ToString() on it (int.Parse(label6.Text) + 1).ToString() also i would recommend giving ur components more self-explanatory names, eg. customerNumberLabel or alike. that way u dont have to memorize which numbered label is for what also usually u should also mention which GUI frame work u r using (winforms, wpf, mau, avalonia, etc) when asking GUI questions - tho here it didnt matter
_Kai_
_Kai_OP2y ago
that got rid of the error and i was able to run it but i ran into an exception i think its called, “imput string was not in a correct format” oh thank you, i’ll do that next time
cap5lut
cap5lut2y ago
can u show me the code line where the exception was thrown?
_Kai_
_Kai_OP2y ago
Pobiega
Pobiega2y ago
It means you tries to parse something that wasn't a number into a number So label6.Text contains something that isn't a number
cap5lut
cap5lut2y ago
aaah, so Customer # 5 from the screenshot is the whole label6? then that variant doesnt work (thats why i recommended the counter approach) in that case i would recommend the counter + Pobiega's approach: label6.Text = $"Customer # {nextId++}";
Pobiega
Pobiega2y ago
It's usually a good idea to separate your program state from your presentation when possible, so the counter idea is good Means the label only shows values, it is never used as a "source of truth"
_Kai_
_Kai_OP2y ago
“nextID does not exist in the current context”
cap5lut
cap5lut2y ago
u will have to create the member somewhere public int nextId = 1;
_Kai_
_Kai_OP2y ago
should i put it under the “save” event too?
cap5lut
cap5lut2y ago
can u elaborate?
Pobiega
Pobiega2y ago
No, it will need to go at a higher scope so it remembers the value "save" is temporary $scope
MODiX
MODiX2y ago
you probably want $scopes
Pobiega
Pobiega2y ago
$scopes
MODiX
MODiX2y ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Pobiega
Pobiega2y ago
The thing inside B will go away when B ends Scope A here is most likely your Form class
_Kai_
_Kai_OP2y ago
i dont think this is the correct place either
cap5lut
cap5lut2y ago
it more or less is, u can use it from any method inside that class by nextID
Pobiega
Pobiega2y ago
You'd normally place that at the top of the class, and it should be private But what you have is legal 🙂
_Kai_
_Kai_OP2y ago
it still says that nextID doesnt exist in the current context :/
Pobiega
Pobiega2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
_Kai_
_Kai_OP2y ago
Pobiega
Pobiega2y ago
I said top of class, not on top of your class 🙂 It goes inside the class
_Kai_
_Kai_OP2y ago
ohhhh i see do i put it in brackets? putting it in class seems to throw the whole thing off because of these {}
Pobiega
Pobiega2y ago
The brackets are very important They define the class scope So yes, it must go inside the brackets There should never be anything I'm between You should take some time to practice the fundamentals of C# when possible
_Kai_
_Kai_OP2y ago
i am taking more classes in the following months, thank you so much for all of your patience
Pobiega
Pobiega2y ago
Don't assume that class-time will be enough You will need to put in some extra time and effort You are currently learning both C# and how to think like a programmer That's quite a bit of cognitive load
_Kai_
_Kai_OP2y ago
well it still says that the name doesn’t exist, but there are no bracket errors now
Pobiega
Pobiega2y ago
Show the code.
_Kai_
_Kai_OP2y ago
Pobiega
Pobiega2y ago
Yep, that looks fine. Show where the error is? Oh and remove those blank lines haha They are just confusing
_Kai_
_Kai_OP2y ago
Pobiega
Pobiega2y ago
That doesn't make sense. Can you copy paste the entire file to $paste please?
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Pobiega
Pobiega2y ago
Need to see the full thing, these snippets obscure a lot of details
cap5lut
cap5lut2y ago
u called it nextID and use it as nextId
Pobiega
Pobiega2y ago
Ah, yup
cap5lut
cap5lut2y ago
nextID is a completely different name than nextId
Pobiega
Pobiega2y ago
Code is case sensitive
_Kai_
_Kai_OP2y ago
okay i was able to run the app, but i ran into another exception
cap5lut
cap5lut2y ago
that line is obsolete now, just remove it its complaining because "Customer # <some number>" isnt a number ;p thats why we use the counter and label6.Text = $"..."; instead
_Kai_
_Kai_OP2y ago
oh right, sorry im running into mistakes like that bc i was really stressed but im calming down now
_Kai_
_Kai_OP2y ago
cap5lut
cap5lut2y ago
SelectedIndex of -1 means there is no item selected, thus u need to handle this edge case:
if (listBox2.SelectedIndex >= 0)
{
// do ur stuff
}
else
{
// do what ever u want to do if nothing was selected
}
if (listBox2.SelectedIndex >= 0)
{
// do ur stuff
}
else
{
// do what ever u want to do if nothing was selected
}
Pobiega
Pobiega2y ago
I think it might be time to start talking about the elephant in the code here, the fact that your listbox entries are giant strings 🙂 when you add a new entry, you build a big string from a bunch of separate values. This works, as you can see, but now each individual value no longer exists - its all one big string. so for the "put data from listbox into textboxes again" thing, this is now... really hard, because of the above fact
cap5lut
cap5lut2y ago
there comes that into account again ;p
Pobiega
Pobiega2y ago
^
_Kai_
_Kai_OP2y ago
ohhh i think i understand. can i still make it work or would i have to redo the listbox entries?
cap5lut
cap5lut2y ago
btw u can get rid of all but the last .Focus() call
Pobiega
Pobiega2y ago
you can easily fix this
cap5lut
cap5lut2y ago
u would have to adjust some stuff once u have something like a Customer class, but thats easily done
Pobiega
Pobiega2y ago
You need to make your own type to represent a customer and if that type were to override the ToString method, that would be good 😛 full separation of state and presentation is the dream, but it will be harder for a beginner
_Kai_
_Kai_OP2y ago
im just trying to get it to run without running into exceptions, even if it doesn’t totally work (at least for tonight, i have to be asleep in a couple hours)
Pobiega
Pobiega2y ago
You should have all you need for that, if you put the first/last buttons and editing rows on hold for now
_Kai_
_Kai_OP2y ago
i put everything under the “save” button into the “if” part of this, and put a messagebox.show(“input info in all fields”); in the “else” part. where would i put the customer class
Pobiega
Pobiega2y ago
I thought you said you wanted to wait with that?
_Kai_
_Kai_OP2y ago
im still trying to get rid of the exception
Pobiega
Pobiega2y ago
what exception?
_Kai_
_Kai_OP2y ago
cap5lut
cap5lut2y ago
u check listBox1.SelectedIndex, instead of listBox2's
_Kai_
_Kai_OP2y ago
it still says the same thing
Pobiega
Pobiega2y ago
show. code. we have no idea what you changed or how also, consider just using discord on the computer so you can share code by copypaste instead of camera :d
_Kai_
_Kai_OP2y ago
ill switch rn
//namespace CSI_122_Final
{
public partial class Form1 : Form



{
private int nextID = 1;

public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
//This is the save button

if (listBox1.SelectedIndex >= 0)
{
listBox1.Items.Add("Record: #" + label6.Text + 1 + " " + textBox1.Text + " " + textBox2.Text
+ " " + textBox3.Text + " " + textBox4.Text + " Type of items purchased: " + listBox1.Items[listBox2.SelectedIndex]);


label6.Text = $" {nextID++}";

textBox1.Clear();
textBox1.Focus();
textBox2.Clear();
textBox2.Focus();
textBox3.Clear();
textBox3.Focus();
textBox4.Clear();
textBox4.Focus();

listBox2.ClearSelected();
}
else
{
MessageBox.Show("Input information in all fields");
}


}

private void button4_Click(object sender, EventArgs e)
{
//This is the delete button

if (listBox1.SelectedIndex >= -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}

private void button1_Click(object sender, EventArgs e)
{
//this is the first button

listBox1.SelectedItem = listBox1.Items[0];
}

private void button3_Click(object sender, EventArgs e)
{
//this is the last button

listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count-1];
}
}
}
//namespace CSI_122_Final
{
public partial class Form1 : Form



{
private int nextID = 1;

public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
//This is the save button

if (listBox1.SelectedIndex >= 0)
{
listBox1.Items.Add("Record: #" + label6.Text + 1 + " " + textBox1.Text + " " + textBox2.Text
+ " " + textBox3.Text + " " + textBox4.Text + " Type of items purchased: " + listBox1.Items[listBox2.SelectedIndex]);


label6.Text = $" {nextID++}";

textBox1.Clear();
textBox1.Focus();
textBox2.Clear();
textBox2.Focus();
textBox3.Clear();
textBox3.Focus();
textBox4.Clear();
textBox4.Focus();

listBox2.ClearSelected();
}
else
{
MessageBox.Show("Input information in all fields");
}


}

private void button4_Click(object sender, EventArgs e)
{
//This is the delete button

if (listBox1.SelectedIndex >= -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}

private void button1_Click(object sender, EventArgs e)
{
//this is the first button

listBox1.SelectedItem = listBox1.Items[0];
}

private void button3_Click(object sender, EventArgs e)
{
//this is the last button

listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count-1];
}
}
}
okay this is the whole thing
Pobiega
Pobiega2y ago
if (listBox1.SelectedIndex >= 0)
{
listBox1.Items.Add("Record: #" + label6.Text + 1 + " " + textBox1.Text + " " + textBox2.Text
+ " " + textBox3.Text + " " + textBox4.Text + " Type of items purchased: " + listBox1.Items[listBox2.SelectedIndex]);
if (listBox1.SelectedIndex >= 0)
{
listBox1.Items.Add("Record: #" + label6.Text + 1 + " " + textBox1.Text + " " + textBox2.Text
+ " " + textBox3.Text + " " + textBox4.Text + " Type of items purchased: " + listBox1.Items[listBox2.SelectedIndex]);
so if listbox1.SelectedIndex is valid, use listbox2.SelectedIndex to access the value... from listbox1? does that make sense? Sorry to say but this looks like just randomly throwing things at the screen to see what sticks :/
_Kai_
_Kai_OP2y ago
it was rushed but i dont know what to change, the listbox2.select index is supposed to access whatever was selected from the "electronics, household, jewelry, etc" list
Pobiega
Pobiega2y ago
Think about it What are you trying to access? You want to get the currently selected item from listbox2. How do we do that? What values must be valid for that to make sense?
_Kai_
_Kai_OP2y ago
Well it says the value of -1 isnt valid, so it has something to do with the way it is processing the values within the lists?
Pobiega
Pobiega2y ago
Read my last 3 messages again.
cap5lut
cap5lut2y ago
think about what listBox1.Items[listBox2.SelectedIndex] does, it takes the index of the selected item of listBox2 and then u try to get an item of listBox1 at the given index is that what should happen?
Pobiega
Pobiega2y ago
and tbh, if you just want the currently selected item, use the SelectedItem property :p "if something is selected, get the currently selected item" we already know SelectedIndex returns -1 if nothing is selected
cap5lut
cap5lut2y ago
totally forgot about that xD
_Kai_
_Kai_OP2y ago
Hey I wanted to come back for a sec and let you guys know I'm done with the app, I got it to where it'd do most of what its supposed to and I also found someone I know irl that happens to know C# too, so I can reach out to him for help as well. More importantly I wanted to thank you both so much for how much time you took helping me out and being patient with me and genuinely explaining things. Your help made a huge difference for me. I immensely appreciate it beyond what I can express right now so again a huge huge thank you!! Have a good rest of your day/night 🙂
cap5lut
cap5lut2y ago
glad i could help, also if u r done here please use /close to mark the thread as answered
Pobiega
Pobiega2y ago
you're welcome. please /close the thread if you dont want to ask more related questions 🙂
Want results from more Discord servers?
Add your server