|
-
Jul 10th, 2010, 07:49 AM
#1
Thread Starter
Member
[RESOLVED] form and class.cs
i have problem using C#.
on vb.net when i call on module
frmMain.txtIp.text
how about on C# how to do it?
how to call frmMain.txtIp.text.
-
Jul 10th, 2010, 08:30 AM
#2
Re: form and class.cs
Hello,
By default, controls added to a Form in C# are created with Private Modifiers, meaning they can only be accessed from code within that form.
If you need to access them from another form, you either need to change the Modifiers to be public, or better yet, add some Public properties to the Form which expose the Private Members which you are trying to access.
Gary
-
Jul 10th, 2010, 09:53 AM
#3
Re: form and class.cs
Note that C# also doesn't support default instances, which are a little bot of black magic cooked up by VB to hide the fact that forms are objects just like any other. You can't magically produce an instance of a form class out of the class name in C# like you can in VB. You have to treat forms like any other objects.
So, in C#, you pretty much have to do what you really should do in VB too, and most experienced developers will. From your code, it seems that you're trying to set the Text of a TextBox on the main form. In either language, the "proper" way to do that is to make the data available and let the main form get that data and update its own TextBox.
I'm guessing that you are displaying a dialogue and the user is entering an IP address and you want to transfer that back to the main form. If so, as Gary suggests, you should add a property to the dialogue form that exposes that data. The main form can then get that property value and update the TextBox itself. This is the "proper" way to do it in VB too.
Default instances in VB are one of those features that can let you achieve an end result faster but mainly because it means you don't have to have as complete an understanding of how things work and therefore don't necessarily encourage best practice.
-
Jul 10th, 2010, 01:03 PM
#4
Thread Starter
Member
Re: form and class.cs
 Originally Posted by gep13
Hello,
By default, controls added to a Form in C# are created with Private Modifiers, meaning they can only be accessed from code within that form.
If you need to access them from another form, you either need to change the Modifiers to be public, or better yet, add some Public properties to the Form which expose the Private Members which you are trying to access.
Gary
can you give some example? this is my first time to use C#.
-
Jul 10th, 2010, 04:08 PM
#5
Re: form and class.cs
Hey,
Have a look at this thread here:
http://www.vbforums.com/showthread.php?t=542283
Hope that helps!!
Gary
-
Jul 10th, 2010, 08:16 PM
#6
Thread Starter
Member
Re: form and class.cs
is there a image picture?
-
Jul 10th, 2010, 08:49 PM
#7
Re: form and class.cs
Please explain EXACTLY what it is that you're trying to do and then we can provide a code example.
-
Jul 10th, 2010, 09:03 PM
#8
Thread Starter
Member
Re: form and class.cs
Character.cs <= class
Code:
class Characters
{
frmRegister _frmRegister = new frmRegister();
public void increasePicMale(int count)
{
count++;
switch (count)
{
case 1:
_frmRegister.picCharacter.Image = System.Drawing.Image.FromFile("./Data/Male/01.png");
break;
case 2:
_frmRegister.picCharacter.Image = System.Drawing.Image.FromFile("./Data/Male/02.png");
break;
case 3:
_frmRegister.picCharacter.Image = System.Drawing.Image.FromFile("./Data/Male/03.png");
break;
}
}
public void decreasePicMale(int count)
{
count--;
switch (count)
{
case 1:
_frmRegister.picCharacter.Image = System.Drawing.Image.FromFile("./Data/Male/01.png");
break;
case 2:
_frmRegister.picCharacter.Image = System.Drawing.Image.FromFile("./Data/Male/02.png");
break;
case 3:
_frmRegister.picCharacter.Image = System.Drawing.Image.FromFile("./Data/Male/03.png");
break;
}
}
}
i cann't apply this
Characters.increasePicMale();
-
Jul 10th, 2010, 09:18 PM
#9
Re: form and class.cs
Post #8 seems to have nothing to do with what you posted in post #1. Please keep each thread to a single topic.
The issue in post #8 is the fact that class Characters creates a new instance of frmRegister:
Code:
frmRegister _frmRegister = new frmRegister();
Making changes to this NEW instance doesn't help you much because that NEW instance is NOT the instance that you have displayed in the first place. If you had a notebook and then you went and bought a NEW notebook and wrote in it, would you expect writing to appear in the original notebook? Of course not. They are two different objects and what affects one doesn't affect the other. The same goes here. You are creating two different instances of frmRegister and what affects one doesn't affect the other.
If you're going to use this Characters class then you need to pass the existing instance of frmRegister to it, either via a constructor, a property or a parameter of the methods.
On a different note, you've got way more code there than you need. Your 'switch' statements are exactly the same so you could take that out of both methods and put it in its own method. Also, you only need one line to set the Image because you can just use 'count' to specify the path of the image file.
-
Jul 10th, 2010, 11:20 PM
#10
Thread Starter
Member
Re: form and class.cs
oh i see.
frmRegister _frmRegister = new frmRegister();
what i will change to connect to that form?
Last edited by solid2005; Jul 11th, 2010 at 01:39 AM.
-
Jul 11th, 2010, 03:34 AM
#11
Re: form and class.cs
When you say "connect", do you mean to show the form?
i.e
Code:
frmRegister _frmRegister = new frmRegister();
_frmRegister.Show();
Gary
-
Jul 11th, 2010, 04:17 AM
#12
Thread Starter
Member
Re: form and class.cs
This is my class.cs
public void InsertRegister()
{
frmRegister _frmRegister = new frmRegister();
string x = _frmRegister._userName;
string query = "INSERT INTO login (userid, user_pass, sex, email) VALUES('" + x + "', '" + Convert.ToString(_frmRegister.txtPassword.Text) + "', '" + Convert.ToString(_frmRegister.cboGender.Text) + "', '" + Convert.ToString(_frmRegister.txtEmail.Text) + "')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
MessageBox.Show(x);
//close connection
this.CloseConnection();
}
}
i don't know why no value of frmTxtusername.text.
-
Jul 11th, 2010, 07:03 AM
#13
Re: form and class.cs
Hello,
I am confused?!?
I don't see frmTxtusername defined anywhere. Also, what type is frmTxtusername. From the name, it would suggest that it is a Form, but you are trying to access the Text member, which would suggest it is a TextBox.
Can you perhaps show more of the code that you are trying to use, or at least explain exactly what you are trying to do?
On a side note, you shouldn't concatenate your SQL Query String like that, you should use Parameters. Take a look at the link in my signature, it goes into more detail about why you should use Parameters.
Gary
-
Jul 11th, 2010, 10:00 AM
#14
Thread Starter
Member
Re: form and class.cs
e.g in my form1.cs
have textbox1.text now i want to get the value of textbox1.text to class.cs.
-
Jul 11th, 2010, 10:17 AM
#15
Re: form and class.cs
solid2005,
You seem to be jumping around quite a lot.
Your last two posts have no relation to each other!!
Take a step back, describe exactly what you are trying to achieve.
Gary
-
Jul 11th, 2010, 10:30 AM
#16
Thread Starter
Member
Re: form and class.cs
http://www.mediafire.com/?m1jfmmwqmoz
i upload my work. can you help me?
this is my first time to work on C#.
-
Jul 11th, 2010, 10:37 AM
#17
Re: form and class.cs
Okay,
But you still aren't telling me exactly what you want to do.
From looking at your code, I have the following:
1) You want to click the Register ToolStripMenuItem on frmMain
2) This opens up an instance of frmRegister
3) User fills this out and clicks the Register button
4) This makes a call into DBConnect.InsertRegister
Am I right so far?
I think what you are missing is that this:
Code:
//Insert statement
public void InsertRegister()
{
...
}
Should look more like this:
Code:
//Insert statement
public void InsertRegister(string userName, string password, string sex, string email)
{
...
}
And you call this from frmRegister like:
Code:
dbConnect.InsertRegister(this.txtUsername.Text, this.txtPassword.Text, this.cboGender.SelectedText, this.txtEmail.Text);
Does that make sense?
Gary
-
Jul 11th, 2010, 10:58 AM
#18
Thread Starter
Member
Re: form and class.cs
Thanks it work.
by the way do you have reference for newbie with server, 2d games?
i have a project on school. this is my first time to make a 2d games on programming languages.
-
Jul 11th, 2010, 11:02 AM
#19
Re: form and class.cs
Hey,
You might want to think about posting this question in the Games and Graphics Programming Forum:
http://www.vbforums.com/forumdisplay.php?f=2
The people there should be able to point you in the right direction.
Gary
-
Jul 11th, 2010, 11:19 AM
#20
Thread Starter
Member
-
Jul 11th, 2010, 11:21 AM
#21
Re: form and class.cs
No probs!
Also, you might want to think about marking this thread as Resolved.
Thanks
Gary
-
Jul 13th, 2010, 11:44 PM
#22
Frenzied Member
Re: [RESOLVED] form and class.cs
Hi Friends .Can you see the following code.why OpenConnection is Not Working.why getting error at this line.private bool OpenConnection(ref OledbConnection conn).Kindly let me know Please.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MakeConnection
{
public partial class Form1 : Form
{
private string conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.SaddleBrown;
}
private bool OpenConnection(ref OledbConnection conn)
{
try
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\GetConnectionString\product_Table.mdb");
conn.Open();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
}
-
Jul 14th, 2010, 12:56 AM
#23
Re: [RESOLVED] form and class.cs
firoz.raj,
You really should have created your own thread for this question, rather than posting into someone elses Resolved thread.
What is the error that you are receiving?
Are you sure that that is the right path to the Access Database?
Gary
-
Jul 14th, 2010, 03:41 AM
#24
Frenzied Member
Re: [RESOLVED] form and class.cs
You really should have created your own thread for this question, rather than posting into someone elses Resolved thread.
Actually solid2005 is using same OpenConnection() Function.that is why i did post here.sorry for mistake.
What is the error that you are receiving?
i am receiving error the type or name Space Name 'oledbConnection' Could not be Found.Kindly
see the attached jpg.
Are you sure that that is the right path to the Access Database?
Yes Path is Ok.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MakeConnection
{
public partial class Form1 : Form
{
private string conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.SaddleBrown;
}
private bool OpenConnection(ref OledbConnection conn)
{
try
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
conn.Open();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:09 PM.
-
Jul 14th, 2010, 03:53 AM
#25
Re: [RESOLVED] form and class.cs
Hey,
Does you project have a reference to the System.Data assembly?
Also, you will need to add this:
Code:
using System.Data.OleDb;
At the top of your Code file.
Also, C# is case sensitive, so you will need to use this:
Notice the capital D.
Gary
-
Jul 14th, 2010, 04:57 AM
#26
Frenzied Member
Re: [RESOLVED] form and class.cs
Still Not Working .here is the following Code What i have written.Kindly see the attached jpg also.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb ;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MakeConnection
{
public partial class Form1 : Form
{
// OleDbConnection conn = new OleDbConnection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if ( ! OpenConnection(conn)== true)
{
MessageBox.Show("Connection is Not Open");
break;
}
}
private bool OpenConnection(ref OleDbConnection conn)
{
try
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
conn.Open();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 14th, 2010, 06:38 AM
#27
Re: [RESOLVED] form and class.cs
Hey,
The first error is because of this line:
Code:
// OleDbConnection conn = new OleDbConnection();
The definition of conn is commented out. Remove the comment.
The second error is because of this:
It is not needed.
Gary
-
Jul 14th, 2010, 07:34 AM
#28
Frenzied Member
Re: [RESOLVED] form and class.cs
Still getting error .here is the following code .what i have written.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb ;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MakeConnection
{
public partial class Form1 : Form
{
OleDbConnection conn = new OleDbConnection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!OpenConnection(conn) == true)
{
MessageBox.Show("Connection is Not Open");
// break; //i simple want if connection is not open then giving error message and after that going on }
}
}
private bool OpenConnection(ref OleDbConnection conn)
{
try
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\product_Table.mdb");
conn.Open();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
}
-
Jul 14th, 2010, 07:36 AM
#29
Re: [RESOLVED] form and class.cs
You are getting the same error? Or a different error?
If different, what is the new error?
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|