|
Hi all,
I am still very new to C#. I have created a Windows Application in VS2005, and added a new class (Class1) that I created. I've then added another Form to the Application.
My problem is that I want to declare the Class (Class1) so that I can use it in both Forms.
Where about's in the Application would I do this please?
Many Thanks
Tony
|
|
|
|
|
Nooie wrote: Application
You are using "Application" as a proper noun here. Is there any significance in that? Or should the initial "A" be lowercased back to a common noun?
Nooie wrote: My problem is that I want to declare the Class (Class1) so that I can use it in both Forms.
Where about's in the Application would I do this please?
It really depends on what you want to do with the class. Class1 is not a very good name, it doesn't decribe what the class represents.
You could create an instance of the class in the first form then pass it to the second form in its constructor (you'll have to modify the constructor of the second form to accomodate the object). Or you could do one of many other things.
What is the purpose of Class1 ?
|
|
|
|
|
Hi Sorry I was using simple names to try and no over complicate it.
Basically I created a new windows form project using VS2005.
I then created a new Class called NavConnection. This class contains details such as Servername, databasename etc...
Next I added application settings which match the props in the class NavConnection.
From the Main Form I added a new MenuStrip and a call to a new form I created. On the new form I have textboxes that mimic the Usersettings/Navconnection.
The idea being that when this form is run it will populate the textbox's with the data from the class.
Then when the app is closed the User Settings will be saved from the Class NavConnection.
So when I run the app I want to create an instance that it used through out the app.
Does that make more sense?
It may not be the best way to do this but I am inventing little projects to help learn C#
Cheers for your help.
Tony
|
|
|
|
|
You need to create an instance of your class as a member.
As in
NavConnection conn = new NavConnection();
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi Christian,
Thats kind of what I was asking where do I put it? I've tried a few places, but when I go to code on the second form using the instance conn, intellisense does not pick it up.
Tony
|
|
|
|
|
Hi,
You write "public static MyClass myClass1 = new MyClass () outside "Form1_Load()" or "Form2_Load()" to use in both forms.
Hope my help.
|
|
|
|
|
Public fields are generally a bad idea.
|
|
|
|
|
NavConnection sounds like it should be a Singleton (i.e. There exists only one instance of it in the application)
If that is correct, you can create the class as static (i.e. put static on its class definition and all fields, properties and methods). Then you don't need to store an instance of the class anywhere. You can reference a method on it like this:
NavConnection.ServerName;
NavConnection.SaveSettings();
The class might look something like this:
public static class NavConnection
{
private static string serverName;
public static string ServerName
{
get { return this.serverName; }
set { this.serverName = value; }
}
public static void SaveSettings()
{
}
}
|
|
|
|
|
i am using remoting and fetching some info from remote machine......
i am able to fetch it
but at time the system gets hanged up and sets a message saying "User-interface operation not allowed at this time. "
how can i avoid it...
why this happens
|
|
|
|
|
i'm planning to make a .net roadmap for my new comming employees.
i know it's too broad to start and this is why i need help.
i wanna make a roadmap that can be used for people are frosh in this industry.
i havn't started yet, but probably it will have to contain what courses to take, what catagories should be educated, what concept to understand,,,etc.
does any one has any kind of resources, documents, or references??
please let me know...it will be helpful.
thanks.
|
|
|
|
|
Road Map, I would suggest if you can come out with a proper training followed by good demo project. When you plan for training think about all the possible feature that you can use it. Also if this is in line for some upcoming project then you should always keep those requirements in mind while deciding the scope of project.
Regards,
Jaiprakash M Bankolli
jaiprakash.bankolli@gmail.com
|
|
|
|
|
I want to develop a date picker control which will have cool look-n-feel. My control will be derived from “System.Windows.Forms.Control”.On click event I am trying to show the form which has to “Monthcalendar” control. The problem is the form will not display at the correct position.
Help me to determine the position to display the form to get exact appearance of the Windows date picker.
M.Sendilkumar,Bangalore,India.
|
|
|
|
|
Well, need I say that if you're having trouble positioning the form, writing it may be an issue, too ?
You show the form relative to the button, work out how you want it to show relative to the button, and set it's position from the button location.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Thats what my problem is.I Set the button's left as form's left and button's top+buttons'height as form's top.
This is the logic.
protected override void OnClick(EventArgs e)
{
SetAutoLocation();
frmCal.Show();
Invalidate();
}
private void SetAutoLocation()
{
Rect rect;
GetWindowRect(this.Handle, out rect);
Point tergatePoint;
tergatePoint = new Point(rect.left, rect.top + this.Height);
tergatePoint = new Point(Left, Top + Height);
if (rect.left + this.Width - frmCal.Width < 0)
{
tergatePoint.X = 0;
}
else
{
tergatePoint.X = rect.left - frmCal.Width + this.Width;
}
if (tergatePoint.X + frmCal.Width > System.Windows.Forms.SystemInformation.WorkingArea.Right)
{
tergatePoint.X = System.Windows.Forms.SystemInformation.WorkingArea.Right - frmCal.Width;
}
else if (tergatePoint.X < 0)
tergatePoint.X = 0;
if (tergatePoint.Y + frmCal.Height > System.Windows.Forms.SystemInformation.WorkingArea.Bottom)
{
tergatePoint.Y = rect.top - frmCal.Height;
}
if (tergatePoint.Y < 0)
{
tergatePoint.Y = 0;
}
if (tergatePoint.X < 0)
{
tergatePoint.X = 0;
}
frmCal.Location = tergatePoint;
}
public struct Rect
{
internal int left, top, right, bottom;
}
First click form is not positioning well ,but from second click onwards it is working fine.
Can you just see that logic is correct or not?
M.Sendilkumar
|
|
|
|
|
hi,
I am using datagridview in my project and i have 4 columns...
in one of the column i want to validate the data entry.
like in MaskedTextBox i want the column to be like xxx.xxx.xxx.xxx
user only should enter the values....
like settting the Mask property in MaskedTextBox..
Pls help me
With Regards
prasad
|
|
|
|
|
Maybe DataGridView.EditingControl Property would help. Set it to a MaskedTextBox and make the mask you want.
Regards
|
|
|
|
|
Hello everyone,
I was wondering if it is possible to change the light blue strip on the left hand side of the ContextMenu into a customized image.
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
If you want an image to be shown on the blue margin of the ContextMenu , Choose one of the items -usually the middle one-, then set the Image property of that item to the image you want. If you wan the image only next to that item this ould do it. If you want the image to spread all over the margin, set the ImageScaling property to false .
Regards
|
|
|
|
|
I am trying to construct a record in my database that has a Guid as the key. But I cannot find anything on actually generating a GUID. I'm surprised the Create is completely left out of the framework.
Any help would be appreciated.
Thanks.
|
|
|
|
|
theRealCondor wrote: But I cannot find anything on actually generating a GUID
Did you try Guid.NewGuid() method?
Regards
|
|
|
|
|
Hi,
I am doing a converstion from byte[] to hex string
byte[] aGroup = new byte[1024];
aGroup[] = {0,1,1,0,0,0,0,0,0,0,1,0,0...}
hexString = ToString(aGroup);
This is my To string function
public static string ToString(byte[] bytes)
{
string hexString = "";
try
{
StringBuilder temp = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
temp.Append(bytes[i].ToString("X2"));
}
hexString = temp.ToString();
}
catch (Exception)
{
}
return hexString;
}
After convertion my hexstring looks like this
000101000000000000010...00
where as it should be
0x06020
Is it that i need to do the conversion to base 16 first? any samples appreciated.
|
|
|
|
|
As I have shown you before, when your byte array only contains the values zero and one,
then the string will contain only the characters 0 and 1.
To convert bits into a hex string you somewhere somehow must pack 8 bits into one byte
(or 4 bits into one hex char); one way to do this is by using index/8 and 1<<(index%8)
|
|
|
|
|
I did stick the index/8 and 1<<(index%8) in the right place.
Thanks alot for the response.
|
|
|
|
|
The conversion is taking place as it should. Here's where I think you're tripping up:
aGroup[] = {0,1,1,0,0,0,0,0,0,0,1,0,0...}
Do you mean for this array to represent a binary value?
When a value of 0 is converted to hex, it is 0. A value of 1 is converted to 1 in hex. So the results of converting the above byte array to hex is not going to look any different than simply printing out the individual values of the array.
What you probably want to do is pack the binary values into the bytes, then do a conversion to hex.
Also:
aGroup[] = {0,1,1,0,0,0,0,0,0,0,1,0,0...}
hexString = ToString(aGroup);
I'm not sure what you're trying to accomplish here, but doing a ToString on an array is just going to give you something like, "System.Byte[]".
|
|
|
|
|
BitArray bitArray = new BitArray(aGroup.Length);
for (int i = 0; i < aGroup.Length; i++)
{
bitArray[i] = Convert.ToBoolean(aGroup[(aGroup.Length - 1) - i]);
}
byte[] hexValue = new byte[aGroup.Length / 8];
bitArray.CopyTo(hexValue, 0);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("0x");
for (int i = 0; i < hexValue.Length; i++)
{
stringBuilder.Append(hexValue[(hexValue.Length - 1) - i].ToString("X2"));
}
Console.WriteLine(stringBuilder.ToString());
|
|
|
|