C
C#•10mo ago
Eve.

WindowsForms

why can't i change from one form to another, it's as though Form3 doesn't exist, did the same with Form2
No description
138 Replies
leowest
leowest•10mo ago
$code
MODiX
MODiX•10mo ago
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/
Eve.
Eve.OP•10mo ago
private void Next_button_Click(object sender, EventArgs e)
{
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
this.Close();

}
private void Next_button_Click(object sender, EventArgs e)
{
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
this.Close();

}
leowest
leowest•10mo ago
and it errors on Form3 line as it doesn't know it?
Eve.
Eve.OP•10mo ago
yes but there is a form3
leowest
leowest•10mo ago
u probably renamed it then try looking inside the file of form3 to see
Eve.
Eve.OP•10mo ago
No description
leowest
leowest•10mo ago
look inside
Eve.
Eve.OP•10mo ago
No description
leowest
leowest•10mo ago
at the top of the class
Eve.
Eve.OP•10mo ago
this?
leowest
leowest•10mo ago
are they in different namespace then? or projects
Eve.
Eve.OP•10mo ago
no i added it to the project and its all in same namespace its stressing me out
Eric
Eric•10mo ago
that says pc_spec_3
leowest
leowest•10mo ago
can u confirm Form1 is also in pc_spec_3?
Eric
Eric•10mo ago
pther says pc_spec_generator
leowest
leowest•10mo ago
yeah there we go 😉
Eve.
Eve.OP•10mo ago
ohhhhh thank you so i can just change it?
Eric
Eric•10mo ago
make sure they are all the same
leowest
leowest•10mo ago
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?
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
i have no idea how ive managed to do that
leowest
leowest•10mo ago
u might need to change it on the Designer.cs file as well
Eric
Eric•10mo ago
and then also rebuild
leowest
leowest•10mo ago
I mean I can only think of the namespace being different like that if u copied it from another solution or something
Eve.
Eve.OP•10mo ago
i pressed the add button from the project bit
leowest
leowest•10mo ago
without seeing your solution explorer its hard to say
leowest
leowest•10mo ago
but yeah after u change the namesapce on both files
No description
leowest
leowest•10mo ago
and rebuild u should be ok
Eve.
Eve.OP•10mo ago
No description
leowest
leowest•10mo ago
yeah u see you're in folder view so you did not notice the mistake u have 2 separated projects
Eve.
Eve.OP•10mo ago
i've only just checked that what do i change
leowest
leowest•10mo ago
where is your solution file? I dont even see a sln file u might need to create one
Eve.
Eve.OP•10mo ago
what is a solution file
Pobiega
Pobiega•10mo ago
iirc you can't close the main form without killing the entire app in a winforms app
leowest
leowest•10mo ago
this is what it normally looks like
No description
Pobiega
Pobiega•10mo ago
so thats worth keeping in mind too
Eve.
Eve.OP•10mo ago
No description
leowest
leowest•10mo ago
it is using showdialog it wont close until he closes form3 thou
Eve.
Eve.OP•10mo ago
she*
Pobiega
Pobiega•10mo ago
good catch
leowest
leowest•10mo ago
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
Eve.
Eve.OP•10mo ago
ITS WORKING I THINK
leowest
leowest•10mo ago
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
Eve.
Eve.OP•10mo ago
its not working, when i press the next button it just comes up with this
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
its not red anymore though
leowest
leowest•10mo ago
can you post the code on Form3.Designer.cs?
Eve.
Eve.OP•10mo ago
how would i see if i can do that im very new to windows forms
leowest
leowest•10mo ago
click the arrow to collapse it on the menu there then u will see the file
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
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
leowest
leowest•10mo ago
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
Eve.
Eve.OP•10mo ago
No description
leowest
leowest•10mo ago
then there is no more files using that namespace u forgot to change from CUrrent DOcument to Current Project
leowest
leowest•10mo ago
No description
Eve.
Eve.OP•10mo ago
oh okay i have changed that now
leowest
leowest•10mo ago
now see if it finds anything
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
no but its still not changing forms
leowest
leowest•10mo ago
that's fine now click the + file u opened earlier this according to that code u should have a single button in it
leowest
leowest•10mo ago
No description
Eve.
Eve.OP•10mo ago
i have done it
leowest
leowest•10mo ago
I mean if u dont post here I wont know what is inside... :catlaugh:
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
No description
leowest
leowest•10mo ago
ok looks fine can you show me the Form3.cs as well
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
shall i copy and paste or is that okay?
leowest
leowest•10mo ago
thats fine as an image but usually better if u post code instead as we can copy paste modify and send back
Pobiega
Pobiega•10mo ago
thats gonna cause some issues later down the road thou form1 -> form3 -> form1 all via new and showdialog
Eve.
Eve.OP•10mo ago
shall i just start again
leowest
leowest•10mo ago
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
Pobiega
Pobiega•10mo ago
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?
Eve.
Eve.OP•10mo ago
yes i think so
leowest
leowest•10mo ago
yeah there was 2 csproj initially but solution only have 1 of them very messy
Pobiega
Pobiega•10mo ago
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 🙂
Eve.
Eve.OP•10mo ago
i had to email myself the entire file from a different computer because i cant use github so maybe thats why
leowest
leowest•10mo ago
why not?
Pobiega
Pobiega•10mo ago
working on multiple computers without proper source control is very painful, yep whats stopping you from using git/github?
Eve.
Eve.OP•10mo ago
i can't figure it out and my project is due in a month
leowest
leowest•10mo ago
I see, its very simple honestly but I wont delve into that go recreate your project and we can talk about git github after
Pobiega
Pobiega•10mo ago
I can prob get you set up on github in ~10-15 minutes ^
Eve.
Eve.OP•10mo ago
okay ill go do it again won't take too long im just lazy
leowest
leowest•10mo ago
I honestly dont know how u messed up your solution like that
Eve.
Eve.OP•10mo ago
thank you
Pobiega
Pobiega•10mo ago
🙂
Eve.
Eve.OP•10mo ago
i've done it and its working!!
leowest
leowest•10mo ago
great 😉
Pobiega
Pobiega•10mo ago
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
Eve.
Eve.OP•10mo ago
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
Pobiega
Pobiega•10mo ago
well sure, but its still better to do that properly by implementing a reset feature, rather than just allocating a whole new form
Eve.
Eve.OP•10mo ago
okay how do i do that
leowest
leowest•10mo ago
what he meant is that you're creating a loop of forms which is not good and potentially a memory leak
Eve.
Eve.OP•10mo ago
this is a huge learning cerve
leowest
leowest•10mo ago
as it was in your code because you were opening form1, then creating form3 then creating form1, then creating form3
Eve.
Eve.OP•10mo ago
i just have the 2 now
Pobiega
Pobiega•10mo ago
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
Eve.
Eve.OP•10mo ago
how do i make it not do that
leowest
leowest•10mo ago
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
Eve.
Eve.OP•10mo ago
No description
Eve.
Eve.OP•10mo ago
is that not what the hide bit is doing
leowest
leowest•10mo ago
yes and no it is creating new forms and never going back to the original ones
Pobiega
Pobiega•10mo ago
the new keyword literally means "create a new..." the old one still exists, just hidden
Eve.
Eve.OP•10mo ago
oh right
leowest
leowest•10mo ago
you never reach the Close() bit of the form and ur always creating new ones
Pobiega
Pobiega•10mo ago
and because you are using ShowDialog, they are all awaiting the next form to "finish"
Eve.
Eve.OP•10mo ago
oh so it will just overload my memory if i do it too many times
Pobiega
Pobiega•10mo ago
exactly
leowest
leowest•10mo ago
and if u were not using ShowDialog I imagine your app would crash because it would close the main thread
Eve.
Eve.OP•10mo ago
do you know code i could use to prevent this?
Pobiega
Pobiega•10mo ago
yes, keep your form references somewhere else and pass them around. Maybe even use a "view manager" or similar to keep track of them
leowest
leowest•10mo ago
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
Pobiega
Pobiega•10mo ago
Have you worked with C# or another OOP language at all before this? or is winforms your first experience with C#?
Eve.
Eve.OP•10mo ago
no i learnt a bit of C# before this like i know for loops and while etc
Pobiega
Pobiega•10mo ago
okay, so you have some idea of object references?
Eve.
Eve.OP•10mo ago
like global or am i thinking of something else
Pobiega
Pobiega•10mo ago
? I'm not sure what you are talking about 🙂
Eve.
Eve.OP•10mo ago
lol never mind then
leowest
leowest•10mo ago
I will just step aside and let pobiega take over so it doesn't get confusing... gl
Pobiega
Pobiega•10mo ago
oh didnt mean to hijack :p if you want it, its yours
Eve.
Eve.OP•10mo ago
im surprised neither of you have given up yet
Pobiega
Pobiega•10mo ago
why would we? you are not insulting us, asking us to do your work for you or just left :p
Eve.
Eve.OP•10mo ago
haha very true i had to get this running first before adding a database to it which is a whole nother level
Pobiega
Pobiega•10mo ago
Mhm, agreed. There are a million ways to do this, with varying degrees of finesse.
Eve.
Eve.OP•10mo ago
okay maybe easiest / more efficient?
Pobiega
Pobiega•10mo ago
its all tradeoffs, but I have a... somewhat simple idea in mind Are you able to jump on a voice/screenshare call?
Eve.
Eve.OP•10mo ago
is there any change we can do it tomorrow, i have to go to work in a minute? i'm free 2pm-5pm
Pobiega
Pobiega•10mo ago
timezones exist I'm afraid, so that tells me nothing 😛 Its... 15:32 currently
Eve.
Eve.OP•10mo ago
you're an hour in front of me 3-6pm
Pobiega
Pobiega•10mo ago
That might be tight, I usually get home from work around 16:30-17:00 my time, so would only have an hour
Eve.
Eve.OP•10mo ago
that's okay we can always do the day after as well if you're willing?
Pobiega
Pobiega•10mo ago
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
Eve.
Eve.OP•10mo ago
okay, i will message you tomorrow
Pobiega
Pobiega•10mo ago
I fiddled with it a bit, here is what I got so far:
Pobiega
Pobiega•10mo ago
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
public partial class MainForm : Form, IManagedForm
{
private readonly ViewManager _viewManager;

public MainForm(ViewManager viewManager)
{
_viewManager = viewManager;

InitializeComponent();
}

public void Reset()
{
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
}

public event EventHandler OnShow;

private void button1_Click(object sender, EventArgs e)
{
_viewManager.Goto(this, _viewManager.DetailsForm);
}

public void TriggerOnShow()
{
OnShow?.Invoke(this, EventArgs.Empty);
}
}
public partial class MainForm : Form, IManagedForm
{
private readonly ViewManager _viewManager;

public MainForm(ViewManager viewManager)
{
_viewManager = viewManager;

InitializeComponent();
}

public void Reset()
{
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
}

public event EventHandler OnShow;

private void button1_Click(object sender, EventArgs e)
{
_viewManager.Goto(this, _viewManager.DetailsForm);
}

public void TriggerOnShow()
{
OnShow?.Invoke(this, EventArgs.Empty);
}
}
how view manager is implemented I will not show, but I'll help you figure it out 🙂 @Eve. got it sorted?
Want results from more Discord servers?
Add your server