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.
Printable View
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.
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
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.
Hey,
Have a look at this thread here:
http://www.vbforums.com/showthread.php?t=542283
Hope that helps!!
Gary
is there a image picture?
Please explain EXACTLY what it is that you're trying to do and then we can provide a code example.
Character.cs <= class
i cann't apply thisCode: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;
}
}
}
Characters.increasePicMale();
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: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.Code:frmRegister _frmRegister = new frmRegister();
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.
oh i see.
frmRegister _frmRegister = new frmRegister();
what i will change to connect to that form?
When you say "connect", do you mean to show the form?
i.e
GaryCode:frmRegister _frmRegister = new frmRegister();
_frmRegister.Show();
i don't know why no value of frmTxtusername.text.Quote:
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();
}
}
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
e.g in my form1.cs
have textbox1.text now i want to get the value of textbox1.text to 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
http://www.mediafire.com/?m1jfmmwqmoz
i upload my work. can you help me?
this is my first time to work on C#.
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:
Should look more like this:Code://Insert statement
public void InsertRegister()
{
...
}
And you call this from frmRegister like:Code://Insert statement
public void InsertRegister(string userName, string password, string sex, string email)
{
...
}
Does that make sense?Code:dbConnect.InsertRegister(this.txtUsername.Text, this.txtPassword.Text, this.cboGender.SelectedText, this.txtEmail.Text);
Gary
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.
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
thanks.
No probs!
Also, you might want to think about marking this thread as Resolved.
Thanks
Gary
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;
}
}
}
}
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
Actually solid2005 is using same OpenConnection() Function.that is why i did post here.sorry for mistake.Quote:
You really should have created your own thread for this question, rather than posting into someone elses Resolved thread.
i am receiving error the type or name Space Name 'oledbConnection' Could not be Found.KindlyQuote:
What is the error that you are receiving?
see the attached jpg.
Yes Path is Ok.Quote:
Are you sure that that is the right path to the Access Database?
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;
}
}
}
}
Hey,
Does you project have a reference to the System.Data assembly?
Also, you will need to add this:
At the top of your Code file.Code:using System.Data.OleDb;
Also, C# is case sensitive, so you will need to use this:
Notice the capital D.Code:OleDbConnection
Gary
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;
}
}
Hey,
The first error is because of this line:
The definition of conn is commented out. Remove the comment.Code:// OleDbConnection conn = new OleDbConnection();
The second error is because of this:
It is not needed.Code:break;
Gary
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;
}
}
}
}
You are getting the same error? Or a different error?
If different, what is the new error?
Gary