Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

C# Question.

Just make a variable that they both can access.
Like, when you create the two classes, you just send a reference or something along.
 
Really depends on the relationship between the two dialogs.

If it's information you want publicly visible to the entire project, and there's only going to be one set of information per application run, you can use static variables.

If the Main Form is creating the Login Form and you want the Main Form to be able to access the results of what happens within Login, you can declare instance (non-static) locals within Login, with an access modifier of internal instead of public or private. If you use Login as a dialog window, you can set the local values to the value of their controls before closing the dialog, the 'Login' or 'OK' button, whichever you use that accepts the dialog would have a DialogResult property set to DialogResult.OK, which tells you on the point where you called, something like, var dialogResult = loginInstance.ShowDialog();, whether the user clicked the login or cancel button would be noted by the dialogResult value. You'd set the 'Cancel' button's DialogResult to an appropriate value.

As for what you declare your variables as, and what you set them to, that really depends on your code, you didn't ask anything more than 'how to pass data'. If the Main Form used Login, then the point where it uses Login would be where you'd access the data, after determining through the dialog result that the data is accessible (if they canceled, the data isn't valid, since you didn't set it).

If you have any more questions, just ask.

Edit:
On another note, if you wanted to pass data to Login at the point where you create the dialog, you'd simply add parameters to the constructor of the class. Something like... public Login(type1 data1, type2 data2) { /*...*/ } And use the data within the constructor to do what you want, replacing type1 and type2 with the types of the data you want to pass.
 

Tdata

Sponsor

Sorry I was half asleep when I wrote it having spent 7 hours on a Login Window...

Structure
Login
If (Varify_User) {
new Main.ShowDialog();
Invasion3075_Login.hide();
}

Is basically what I am doing... All I want from Login is the UserID for the user, and give it to Main so I can load the data.
 
There is a couple of ways of doing that one is to create the MainForm instance before and pass it as a argument like:

f (Varify_User)
{
Main s = new Main(UserID);
s.ShowDialog();
Invasion3075_Login.hide();
}

other make it a variable and set it in main,

Maybe if you could explain better what you want we could help you better?
 
Well, first off you don't show a dialog by going 'New DialogClassName.ShowDialog()' that's a syntax error, since DialogClassName.ShowDialog() is a method, not a type (since you could have a sub-class on DialogClassName called ShowDialog, as wacky as that sounds).

Main is your... main window from the looks of it, it's not a dialog so it won't be returning a Dialog Result. Using just Main.Show() would suffice.

I think what you want is to specify a new Constructor on Main which has a single String Parameter named 'userName':
Code:
[font=courier new][color=#0000FF]public[/color] [color=#0000FF]void[/color] [color=#008080]Main[/color][color=#808080]([/color][color=#0000FF]string[/color] [color=#008080]userName[/color][color=#808080])[/color]

[color=#808080]{[/color]

    [color=#008000]//Do stuff here, like loading.

[/color]    [color=#0000FF]this[/color][color=#808080].[/color][color=#008080]InitializeComponent[/color][color=#808080]([/color][color=#808080])[/color][color=#808080];[/color] [color=#008000]//Use the windows forms designer's auto-generated method 'InitializeComponent'

[/color] [color=#808080]}[/color][/font]

Note the best place to put this, for your future sanity, is not inside Login, but rather the place that creates the login dialog. Using the method before of setting a dialog result property on your buttons, allows it to automatically know what kind of result the dialog yields, ie. Login/OK or Cancel. Only set the local values of Login when the button with a Dialog result of DialogResult.OK is clicked. This ensures that when you use the data from Login, it gives you something you can use.

Here's an example usage:
Code:
[font=courier new][color=#008080]Login[/color] [color=#008080]loginInstance[/color] [color=#808000]=[/color] [color=#0000FF]new[/color] [color=#008080]Login[/color][color=#808080]([/color][color=#808080])[/color][color=#808080];[/color]

[color=#008080]var[/color] [color=#008080]dialogResult[/color] [color=#808000]=[/color] [color=#008080]loginInstance[/color][color=#808080].[/color][color=#008080]ShowDialog[/color][color=#808080]([/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]dialogResult[/color] [color=#808000]==[/color] [color=#008080]DialogResult[/color][color=#808080].[/color][color=#008080]OK[/color][color=#808080])[/color]

[color=#808080]{[/color]

    [color=#008080]Main[/color] [color=#008080]mainDialog[/color] [color=#808000]=[/color] [color=#0000FF]new[/color] [color=#008080]Main[/color][color=#808080]([/color][color=#008080]loginInstance[/color][color=#808080].[/color][color=#008080]userName[/color][color=#808080])[/color][color=#808080];[/color]

    [color=#008080]Application[/color][color=#808080].[/color][color=#008080]Run[/color][color=#808080]([/color][color=#008080]mainDialog[/color][color=#808080])[/color][color=#808080];[/color]

[color=#808080]}[/color][/font]
More than likely the code above would be used within your 'Main' method. Which is why I'd recommend calling your 'Main' form 'MainWindow' so it's clear that you mean the Main Window versus the entry point of the application. Same applies to Login, I'd recommend refactoring it to 'LoginDialog', if you don't know what Refactoring is, go to the point where you declared Login, and press 'F2', it should bring up a window that asks you for the new name, alternatively you could click Refactor, Rename.

If you have further questions, let me know.

Edit: also, within the point where you say 'if (dialogResult == DialogResult.OK)' you might want to use: 'if (dialogResult == DialogResult.OK && loginInstance.VerifyLogin())' instead.
Edit (2): And in order to make it work, when you're storing the 'userName' local on LoginDialog, you'll also have to store the password or other credential information the user entered into a local so the VerifyLogin will be able to do its work, once the Form is hidden from view (any button with DialogResult auto-hides the dialog, if I remember correctly), the Form disposes the controls, so the text boxes and so forth won't (or at least shouldn't) have the data any more.
 

Tdata

Sponsor

Maybe this will make it easier to understand:
Login Form:
Code:
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using MySql.Data.MySqlClient;

using DBConnect;

 

namespace Invasion_3075

{

    public partial class Invasion3075_Login : Form

    {

        public Invasion3075_Login()

        {

            InitializeComponent();

            

        }

 

        private void Login_Button_Click(object sender, EventArgs e)

        {

            DbWrapper myWrapper = new DbWrapper("Server", "Database", "User", "Password");

            myWrapper.Connect();

 

            bool exists = myWrapper.VerifyUser(login_name.Text, Login_Pass.Text);

            if (exists == true)

            {

                Connection_Label.ForeColor = Color.Green;

                Connection_Label.Text = "Logged In.";

                this.Hide();

                new Main().Show();

            }

            else

            {

                Connection_Label.ForeColor = Color.Red;

                Connection_Label.Text = "Invalid Username/Password!";

            }

            myWrapper.Disconnect();

 

        }

 

        private void register_button_Click(object sender, EventArgs e)

        {

            Register.ShowDialog();

        }

 

    }

}

 

 
 
I think you Alexander explain it all before, just create a new Constructor for your main Form, and voila problem solve, if you need to pass alot of data just create a custom structure lik

struct User
{
string name;
int id ;
string pass;
/*add whatever more you need, dontr forget to add the atributes though or make the vars public public*/
}

Then in your main form just create a constructor like

public Main(User data)
{
// call base constructor
Main();
}

Something like that I guess.
 

Tdata

Sponsor

Thank you.

One more Question.

Using MySql.Data.MySqlClient, how to I retrieve data from the Database?

I don't think "ExecuteNonQuery();" returns a string.

Edit:

You know I just realized I'm going about this all wrong. The client shouldn't be making database queries directly. In fact, it should be asking the Server to provide the information... So, I need to build a Server somehow... *Sigh* I always do this, I never think ahead far enough... Know any guides to do that? I'm still going to need to know the answer to my question above, but not for the client...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top