WindowsForms
why can't i change from one form to another, it's as though Form3 doesn't exist, did the same with Form2
138 Replies
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/and it errors on Form3 line as it doesn't know it?
yes
but there is a form3
u probably renamed it then
try looking inside the file of form3 to see
look inside
at the top of the class
this?
are they in different namespace then?
or projects
no i added it to the project and its all in same namespace
its stressing me out
that says pc_spec_3
can u confirm Form1 is also in pc_spec_3?
pther says pc_spec_generator
yeah there we go 😉
ohhhhh
thank you
so i can just change it?
make sure they are all the same
Can you show a screenshot of your solution? just to make sure its not a different project or something?
if not then changing the namespace would be fine, u might need to change it on the Designer.cs file as well
Did u copy and paste Form3 from another project or something?
i have no idea how ive managed to do that
u might need to change it on the Designer.cs file as well
and then also rebuild
I mean I can only think of the namespace being different like that if u copied it from another solution or something
i pressed the add button from the project bit
without seeing your solution explorer its hard to say
but yeah after u change the namesapce on both files
and rebuild u should be ok
yeah u see you're in folder view
so you did not notice the mistake
u have 2 separated projects
i've only just checked that
what do i change
where is your solution file?
I dont even see a sln file
u might need to create one
what is a solution file
iirc you can't close the main form without killing the entire app in a winforms app
this is what it normally looks like
so thats worth keeping in mind too
it is using showdialog it wont close until he closes form3 thou
she*
good catch
that is so weird then because u had 2 csproj it only shows u 1 in the view
and its not even the one you're working on
that is why its creating new forms with the different namespaces
you're under pc_spec_3 project
ITS WORKING I THINK
so I would suggest u to change all the files u see in there to use the namespace of pc_spec_3
to avoid future issues like that
its not working, when i press the next button it just comes up with this
its not red anymore though
can you post the code on Form3.Designer.cs?
how would i see if i can do that
im very new to windows forms
click the arrow to collapse it on the menu there
then u will see the file
the pc_spec_generator file doesn't work, the form just comes up as an error so ill have to change it all to the pcspec3
its better imo because your project namespace is pc_spec_3 and it will potentially avoid future issues with it
but u will have to change it in all files
press CTRL + F to open the search function
then click the arrow on the right
top text box u type in pc_spec_generator bottom one u type pc_spec_3 then change the dropdown box from Current Document to Current project
it will find and change all occurences of it
oh okay i have changed that now
now see if it finds anything
no but its still not changing forms
that's fine
now click the + file u opened earlier
this
according to that code u should have a single button in it
i have done it
I mean if u dont post here I wont know what is inside...
:catlaugh:
ok looks fine
can you show me the Form3.cs as well
shall i copy and paste or is that okay?
thats fine as an image but usually better if u post code instead as we can copy paste modify and send back
thats gonna cause some issues later down the road thou
form1 -> form3 -> form1 all via new and showdialog
shall i just start again
yeah recursive form creation 😛
technically what you want to do is when you create Form3 you pass an instance of Form1 so you can summong it back and hide Form3 or close as needed
if all you've done is create two forms with buttons leading to eachother, it might not be a bad idea - sounds like your project got messed up by the namespace changes?
yes i think so
yeah there was 2 csproj initially but solution only have 1 of them very messy
if you describe to us how you want the "flow" of the forms to go, we might be able to suggest a way to structure the form "hierarchy"
because you cant really just create and open new forms all the time, that will explode 🙂
i had to email myself the entire file from a different computer because i cant use github
so maybe thats why
why not?
working on multiple computers without proper source control is very painful, yep
whats stopping you from using git/github?
i can't figure it out and my project is due in a month
I see, its very simple honestly but I wont delve into that go recreate your project
and we can talk about git github after
I can prob get you set up on github in ~10-15 minutes
^
okay ill go do it again
won't take too long im just lazy
I honestly dont know how u messed up your solution like that
thank you
🙂
i've done it
and its working!!
great 😉
be aware that you have an "issue" right now with the going back and forth between the forms - you are actually creating new forms all the time
so if you enter some data in form 1, then click next, and go back, the data will be lost
and you are also building up a lot of resource usage in the background
yes that's what i want
my project is getting data from a user and outputting data given on what they want
so going back will reset it which is good
well sure, but its still better to do that properly
by implementing a reset feature, rather than just allocating a whole new form
okay how do i do that
what he meant is that you're creating a loop of forms which is not good and potentially a memory leak
this is a huge learning cerve
as it was in your code
because you were opening form1, then creating form3 then creating form1, then creating form3
i just have the 2 now
yeah but each time you click a button, a new form is made.
so if you go back and forth 10 times, you have 20 forms
how do i make it not do that
yeah what u want to do is you open form1 then you pass a reference to form2, then when you're done with form2 u can show form1 and close it
now you're back to having 1 form u could even just hide form2 if u will use it multiple times
and resetting the data would be done via code
is that not what the hide bit is doing
yes and no
it is creating new forms and never going back to the original ones
the
new
keyword literally means "create a new..."
the old one still exists, just hiddenoh right
you never reach the Close() bit of the form
and ur always creating new ones
and because you are using
ShowDialog
, they are all awaiting the next form to "finish"oh so it will just overload my memory if i do it too many times
exactly
and if u were not using ShowDialog I imagine your app would crash because it would close the main thread
do you know code i could use to prevent this?
yes, keep your form references somewhere else and pass them around. Maybe even use a "view manager" or similar to keep track of them
it might be a bit too confusing but what we normally do in this scenario is something called Dependency Injection, where we basically inject form1 into form2 so form2 now can call methods from form1 to go back to it etc
Have you worked with C# or another OOP language at all before this?
or is winforms your first experience with C#?
no i learnt a bit of C# before this
like i know for loops and while etc
okay, so you have some idea of object references?
like global
or am i thinking of something else
?
I'm not sure what you are talking about 🙂
lol never mind then
I will just step aside and let pobiega take over so it doesn't get confusing... gl
oh didnt mean to hijack :p
if you want it, its yours
im surprised neither of you have given up yet
why would we? you are not insulting us, asking us to do your work for you or just left
:p
haha very true
i had to get this running first before adding a database to it
which is a whole nother level
Mhm, agreed.
There are a million ways to do this, with varying degrees of finesse.
okay
maybe easiest / more efficient?
its all tradeoffs, but I have a... somewhat simple idea in mind
Are you able to jump on a voice/screenshare call?
is there any change we can do it tomorrow, i have to go to work in a minute?
i'm free 2pm-5pm
timezones exist I'm afraid, so that tells me nothing 😛
Its... 15:32 currently
you're an hour in front of me
3-6pm
That might be tight, I usually get home from work around 16:30-17:00 my time, so would only have an hour
that's okay we can always do the day after as well if you're willing?
eh, or we keep it to text and write when we can
the idea itself isnt super complicated - instead of creating and showing forms via the buttons directly, you move that responsiblity somewhere else
my initial idea was some form of "view manager" class
okay, i will message you tomorrow
I fiddled with it a bit, here is what I got so far:
Form1 resets each time its shown, while form2 does not. Currently its up to each form if they want to do that or not
I also made it so if you close a form that wasnt form1, form1 is shown (so we dont end up with a floating process, an alternative would be closing form1 too).
this uses a "view manager" class that does the showing/hiding of forms on demand.
for example, here is the code for
Form1
how view manager is implemented I will not show, but I'll help you figure it out 🙂
@Eve. got it sorted?