• Help with basic c#
    59 replies, posted
so- currently i am testing out the language c#, and i need some help with figuring out buttons- (let there be a note this is my second c# application) i need to know how to make it so in an app- when you click a button, how can i make this lead to a new page- in the same app? thanks.
Are you making a 'web browser' by any chance?
no- just a simple application.
Do you mean: - Open a new form when a button is pressed? - Change the content of the existing form
[QUOTE=ShaRose;22478335]Do you mean: - Open a new form when a button is pressed? - Change the content of the existing form[/QUOTE] yes. first one.
[QUOTE=Garb;22478389]yes. first one.[/QUOTE] [code] private MySecondForm mysecondform; public void ShowForm() { if(mysecondform == null || mysecondform.IsDisposed) mysecondform = new MySecondForm(); mysecondform.Show(); }[/code] [editline]12:00AM[/editline] First, it checks if mysecondform is null (EG it wasn't created yet) or if it is disposed (This means that is was closed). If it was either of these, it will create it again. Then it shows it. The reason for the variable is so you don't pop up with multiple copies of the form in question. If that IS the behavior you want, just do [code]new MySecondForm().Show();[/code] [editline]12:01AM[/editline] And of course call 'showform' in the button's click event handler. Which is the code that runs when it's clicked.
well- a few things, lets say I want it to go to a new form with a picture on it- that i already made when i hit that button. what do i do? ( basically you start with a button that says hi- then you hit it, beings you to a picture of a giraffe) also- this may sound like a ridiculous question, but where exactly do i put the code? thanks.
[QUOTE=Garb;22478676]well- a few things, lets say I want it to go to a new form with a picture on it- that i already made when i hit that button. what do i do? ( basically you start with a button that says hi- then you hit it, beings you to a picture of a giraffe) also- this may sound like a ridiculous question, but where exactly do i put the code? thanks.[/QUOTE] Get the form, call show. Just like in my example. Usually, you can just double click on a button in the designer, and it will set up a block for you to code in. Example being: [code] private void button1_Click(object sender, EventArgs e) { }[/code]
You understand you have to make the form first?
Seriously, this is my first real attempt with buttons- all i keep getting are errors. [editline]11:31AM[/editline] how do i make a new form? [editline]11:34AM[/editline] Error 1 Ambiguity between 'WindowsFormsApplication1.Form1.mysecondform' and 'WindowsFormsApplication1.Form1.mysecondform'
Sorry but I have to ask. What's with the - hypens?
Hey, if you want you can hit me up on steam, googlemail, MSN or whatever and I'll give you help on this stuff if you want. I have lots of spare time on my hands now, how better to spend it than helping people?
[QUOTE=Chris220;22493720]Hey, if you want you can hit me up on steam, googlemail, MSN or whatever and I'll give you help on this stuff if you want. I have lots of spare time on my hands now, how better to spend it than helping people?[/QUOTE] working on solace
[QUOTE=Chris220;22493720]Hey, if you want you can hit me up on steam, googlemail, MSN or whatever and I'll give you help on this stuff if you want. I have lots of spare time on my hands now, how better to spend it than helping people?[/QUOTE] snip
If you still need help: On your buttons event, put something like this: [cpp] Form1 form1 = new Form1(); Form1.Show(); This.Hide(); [/cpp] That'll get you from one form to another. Just change the form name.
[QUOTE=Chad Mobile;22500648][cpp] Form1 form1 = new Form1(); Form1.Show(); This.Hide(); [/cpp][/QUOTE] On the second line, you want form1. C# is case sensitive, you're referring to the type there. On the third line, you want this. C# is case sensitive. How about cluing up yourself before attempting to help people?
[QUOTE=raccoon12;22496376]working on solace[/QUOTE] That too ;)
[QUOTE=turb__;22504391]On the second line, you want form1. C# is case sensitive, you're referring to the type there. On the third line, you want this. C# is case sensitive. How about cluing up yourself before attempting to help people?[/QUOTE] How about you stop being a jackass? It worked fine for me. I tested it just so you know.
[QUOTE=Chad Mobile;22523200]How about you stop being a jackass? It worked fine for me. I tested it just so you know.[/QUOTE] He's not being a jackass your code is blatantly wrong.
[QUOTE=Chad Mobile;22523200]How about you stop being a jackass? It worked fine for me. I tested it just so you know.[/QUOTE] What kind of compiler do you have that 'works' with obviously wrong code.
[QUOTE=Chad Mobile;22523200]It worked fine for me.[/QUOTE] I can't see how it did.
I'm using Visual C# 2008 Express Edition, and I tested it before posting.
Show() isn't a static method of a form class, how could it be possible to call it from a class and not an instance?
[QUOTE=Chad Mobile;22531659]I'm using Visual C# 2008 Express Edition, and I tested it before posting.[/QUOTE] Post a compiled program that uses that code. Unless you added a static Show method to Form1, and you defined This as this, IT WILL NOT WORK.
Here you go: [url]http://www.mediafire.com/?wywdzn5nmog[/url] Exact code: [cpp] private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); this.Hide(); } [/cpp]
[QUOTE=Chad Mobile;22500648] [cpp] Form1 form1 = new Form1(); Form1.Show(); This.Hide(); [/cpp][/QUOTE] Do you even understand what we were talking about? [QUOTE=turb__;22504391]On the second line, you want form1. C# is case sensitive, you're referring to the type there. On the third line, you want this. C# is case sensitive.[/QUOTE]
Did you even read the thread?
Yes, I read the whole thread but apparently you didn't. You posted code that doesn't work, turb pointed out the errors after which you kept claiming that the code you posted worked since you "tested" it. Now you come back with DIFFERENT code that doesn't have those errors mentioned before pretending like it's the same thing.
It is. Visual Studio does corrections or something, because I entered in that exact code that I posted earlier and saved it.
No, intellisense offers this when you type This, if the OP pasted the code you gave him it wouldn't automatically correct it. Also, when you type Form1 if won't even offer form1 as the first option.
Sorry, you need to Log In to post a reply to this thread.