954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with opening a window

Hey guys, I'm having trouble with an error message in my code, would you mind taking a look and helping me out?

private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "user" && passwordBox1.Password == "password")
            {
                Window2 W2;
                {
                    W2 = new Window2();
                    W2->Show;
                }
                Close();
            }
        }

The error is: The * or -> operator must be applied to a pointer
and: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
(The error occurs on the "W2->Show;" line)

I really appriciate your time!!
Thanks,
TSims11

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Add () and *.

Zjarek
Junior Poster in Training
79 posts since Oct 2009
Reputation Points: 20
Solved Threads: 18
 

managed c++ does not use new -- it uses gcnew. And you have to make W2 a pointer. The braces are unnecessary.

Window2^ W2;
               W2 = gcnew Window2;
               W2->Show;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I'm sorry, I didn't know about it and I think it's weird. Why microsoft would want to change syntax so much?

Zjarek
Junior Poster in Training
79 posts since Oct 2009
Reputation Points: 20
Solved Threads: 18
 

managed c++ does not use new -- it uses gcnew. And you have to make W2 a pointer. The braces are unnecessary.

Window2^ W2;
               W2 = gcnew Window2;
               W2->Show;


On the second Window2, it throws an error saying "; expected"...

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

The third line should be W2->Show(); -- needs the parentheses.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

That doesn't work either... It throws 5 more errors when I add the "()" to W2->Show;
I'm sorry I feel like a bit of a pest.

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

What are the errors? Copy and paste them instead of paraphrasing, please. Also, how is Window2 defined?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This is all of the code related to this action:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "user" && passwordBox1.Password == "password")
            {
                Window2 W2;
                W2 = gcnew Window2;
                W2->Show;
                Close();
            }
        }

(This includes the changes that AD suggested besdes adding () to the end of W2->Show;)
This is just a simple password prompt that is supposed to open the main body of the program when the correct user name (user) and password (password) are used. I'm not even sure if this is really the correct way to make a password prompt, but this is what I came up with.
Thanks again guys,
TSims11
PS: I'm an ameture programmer. I'm not in college. I'm a high school seinor. I was just asking for help, not for you guys to do it for me.

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

You did not make the changes I suggested. You forgot to add the ^ in the declaration of W2. Window2^ W2; Just like c++ and C W2 has to be a pointer. Pointers in CLI are designated with ^ instead of *.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Amazing. You failed to provide both pieces of information I requested.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>>PS: I'm an ameture programmer. I'm not in college. I'm a high school seinor. I was just asking for help, not for you guys to do it for me.

If you want to learn to program then you have to learn to read, comprehend, and follow instructions. Both Narue and I have offered to help you but you have so far failed to provide the information we need to do so.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I'm sorry. I was in a rush this morning... You guys don't have to help me... But based on my searches through the web daniweb seemed to be the best for help. I'm trying to learn... I started maybe a month ago.

If this helps at all I'm using Visual C++ in Visual Studio 2010. This is a WPF form. I made the changes you listed on the pervious page Ancient Dragon, and the following errors pop up.

Chores.Window2' is a 'type' but is used like a 'variable'
'Chores.Window2' is a 'type' but is used like a 'variable'
; expected
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
The name 'gcnew' does not exist in the current context
The name 'W2' does not exist in the current context
The name 'W2' does not exist in the current context
The name 'W2' does not exist in the current context

The last three refer to lines 5, 6, and 7.
Narue, I'm not sure what you mean by "how is Window2 defined."
I really appriciate your help and patience on this.
I'm sorry I forgot to list the info you two asked for.

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

If it helps, here is my new code.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "user" && passwordBox1.Password == "password")
            {
                Window2^ W2;
                W2 = gcnew Window2;
                W2->Show();
                Close();
            }
        }
TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Did you add #include "Window2.h" at the top of that file? I just compiled the code you posted and did not get any erors or warnings, and it ran just as I expected it to run.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ancient Dragon, you are my hero. I haven't included that into my program. However, when I put what you typed above (with an added ";" to it) above all of the using statements, several errors pop up. Is this the correct spot to put a file header? If I recall correctly in Visual Studio 2008 you could place it here. Thank you so much for your infinite patience.
My errors and complete code are listed bellow.

#include "Window2.h";
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Chores
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "user" && passwordBox1.Password == "password")
            {
                Window2^ W2;
                W2 = gcnew Window2;
                W2->Show();
                Close();
            }
        }
    }
}

Errors:
Preprocessor directive expected
Single-line comment or end-of-line expected

Thanks again,
TSims11

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I put it at the top of the *.h file

#pragma once
#include "Window2.h"
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

That worked perfectly. Thank you so much!! You are a life (and grey hair) savior!!!

TSims11
Newbie Poster
20 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: