|
It is possible to have a static property in your main form, like
<br />
public class MainForm : Form<br />
{<br />
private static int data;<br />
<br />
public void FetchDataFromWebService()<br />
{<br />
data = webService.GetData();<br />
}<br />
<br />
public static int Data<br />
{<br />
get { return data; }<br />
}<br />
}<br />
Of course, you can make the data member public, but exposing it via a property is recommended. Or, if your main form is the one that creates all other forms, how about passing it along to others instead of maintaining it in the main form?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Does anyone know how to do The Critter Program from Andy Harris book's on the the challenge questions page 128.
Please help me for the solution...Thanx a million!
|
|
|
|
|
|
The Critter Program
using System;
namespace Critter {
///
/// Critter
/// Demonstrates basic OOP principles including properties and methods
/// Andy Harris, 12/13/01
///
class Menu {
static void Main(string[] args) {
bool keepGoing = true;
int choice;
Critter myCritter = new Critter();
myCritter.name = "George";
while (keepGoing){
myCritter.age();
choice = showMenu();
switch (choice){
case 0:
keepGoing = false;
break;
case 1:
Console.WriteLine(myCritter.talk());
break;
case 2:
myCritter.eat();
Console.WriteLine ("You have fed the critter");
break;
case 3:
myCritter.play();
Console.WriteLine("You have played with the critter");
break;
case 4:
Console.WriteLine("Current name: {0}", myCritter.name);
Console.Write("Change name to: ");
myCritter.name = Console.ReadLine();
break;
default:
Console.WriteLine("That was not a valid input");
break;
} // end switch
} // end while loop
} // end main
static int showMenu(){
int input = 1;
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("0) Exit");
Console.WriteLine("1) Listen to Critter");
Console.WriteLine("2) Feed Critter");
Console.WriteLine("3) Play with Critter");
Console.WriteLine("4) Rename Critter");
try {
input = Convert.ToInt32(Console.ReadLine());
} catch (FormatException) {
Console.WriteLine("Incorrect input");
} // end try
return input;
} // end showMenu
} // end class
class Critter {
private string pName;
private int pFull = 10;
private int pHappy = 10;
private int pAge = 0;
public string name {
get {
return pName;
} // end get
set{
if (value.Length > 8){
Console.WriteLine("The name can't be more than 8 characters");
pName = value;
pName = pName.Substring(0,8);
Console.WriteLine("Changing name to {0}", pName);
} else {
pName = value;
} // end if
} // end set
} // end string property
public string talk(){
string message;
message = "The critter says: \n";
if (pHappy > 5) {
message += " Hi! My name is " + name + "\n";
message += " I feel happy today! \n";
} else if (pHappy > 2) {
message += " " + name + " doesn't feel so good...";
} else if (pHappy > 0) {
message += " " + name + " is MAD...";
} else {
message += " ...nothing at all, but lays in a heap.";
} // end if
return message;
} // end talk
public void age(){
//handles aging the critter
pAge++;
pFull--;
pHappy--;
if (pFull < 3) {
//if hungry, accellerate unhappiness
pHappy--;
} // end if
} // end age
public void play(){
pHappy += 3;
} // play
public void eat(){
pFull += 4;
} // end eat
} // end class
} // end namespace
I dunno how to attach from here, just copy n paste what i have done so far (i've done 3 out of )...but theres more questions:
-modify the program so that is shows the critter's age. special reward to the owner who can keep a critter happy.
-add another method to the critter, such as sleep()
-add cas variable, when you fed the critter the money will be drecrease
Thanks a million
|
|
|
|
|
Yeah, there may be 1.8 million users of CodeProject, but 99.9% of them don't check up on the board every single day. So the chances of finding a person who has that book are very slim to none.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi,
Is it possible to change form title bar or caption bar color in c# without changing the windows setting?
How do I do that to a specific form?
Please guide.
Thanks,
Nitu
|
|
|
|
|
This can only happen if you override the form's WndProc and handle the WM_NCPAINT message. You might want to take a look at this[^] for a little more information on overriding WndProc.
[EDIT]
I should have said, "handle the WM_NCPAINT message and draw the non-client area of the form yourself."
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi Dave,
Thanks for your prompt reply . I'll check that out.
Regards,
Nitu
|
|
|
|
|
hello.im doin a projecton fingerprint recognition using C#.i have very little knowledge about image processing. could i get some help on this.
thank you.
ash
|
|
|
|
|
|
|
i have to transpose the row and column in any dataTable dynamically. i.e. if we add any row in the table it should display as the new column.
Thanks and Regards
Vinay Kumar
Perot Systems,
Bangalore, India.
|
|
|
|
|
hi
i have a directed graph.
i want to know when delete vertex_A from graph , should i join all childs of vertex_A to each parent of vertex_A or not?
thanks.
|
|
|
|
|
Erm, it depends on what behaviour you want from your graph. 'Normal' behavior would be to leave it as it is.
Ryan
Ryan.
|
|
|
|
|
Hi csharper's,
I am using automation to control MS Word with C#.
Does someone know if Word (Office) have a busy flag to test for to prevent a close command before work (printing) is done ?
Thank you
Frank
fracalifa
|
|
|
|
|
The problem is weird. I make a call to the method that does the binding. The method executes once, but it terminates after the databinding statement (not the close statements) then returns back to the Page_load method. This causes a problem because the connection is still open from the first time through. The html page with the dropdown list never appears.
If you can help great!
public class SA_ArptSelNav : System.Web.UI.Page<br />
{<br />
protected System.Web.UI.WebControls.ImageButton ZoomInButton1;<br />
protected System.Web.UI.WebControls.ImageButton ZoomOutButton1;<br />
protected System.Web.UI.WebControls.ImageButton ZoomPrevButton1;<br />
protected System.Web.UI.WebControls.ImageButton ZoomExtButton1;<br />
protected System.Web.UI.WebControls.ImageButton PanButton1;<br />
protected System.Web.UI.WebControls.ImageButton PrintButton1;<br />
protected System.Web.UI.WebControls.DropDownList DropDownList1;<br />
protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;<br />
protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;<br />
protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1;<br />
protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;<br />
protected System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;<br />
protected System.Data.OleDb.OleDbConnection oleDbConnection1;<br />
protected dbListOfAirports dbListOfAirports1;<br />
protected System.Web.UI.WebControls.ImageButton CopyButton1;<br />
<br />
private void Page_Load(object sender, System.EventArgs e)<br />
{<br />
if (!Page.IsPostBack) <br />
{<br />
BindListToDropDown();<br />
}<br />
}<br />
<br />
#region Web Form Designer generated code<br />
override protected void OnInit(EventArgs e)<br />
{<br />
InitializeComponent();<br />
base.OnInit(e);<br />
}<br />
<br />
private void InitializeComponent()<br />
{ <br />
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();<br />
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();<br />
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();<br />
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();<br />
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();<br />
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();<br />
this.dbListOfAirports1 = new dbListOfAirports();<br />
((System.ComponentModel.ISupportInitialize)(this.dbListOfAirports1)).BeginInit();<br />
this.DropDownList1.DataBinding += new System.EventHandler(this.Page_Load);<br />
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;<br />
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;<br />
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;<br />
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {<br />
new System.Data.Common.DataTableMapping("Table", "tblAirport", new System.Data.Common.DataColumnMapping[] {<br />
new System.Data.Common.DataColumnMapping("AirportCode", "AirportCode"),<br />
new System.Data.Common.DataColumnMapping("AirportID", "AirportID"),<br />
new System.Data.Common.DataColumnMapping("AirportName", "AirportName"),<br />
new System.Data.Common.DataColumnMapping("City", "City"),<br />
new System.Data.Common.DataColumnMapping("Latitude", "Latitude"),<br />
new System.Data.Common.DataColumnMapping("Longitude", "Longitude"),<br />
new System.Data.Common.DataColumnMapping("State", "State")})});<br />
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;<br />
this.oleDbDeleteCommand1.CommandText = "DELETE FROM tblAirport WHERE (AirportID = ?)";<br />
this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;<br />
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_AirportID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "AirportID", System.Data.DataRowVersion.Original, null));<br />
this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=0;Data Source=""D:\MGDevelopment\SIMDAC Viewer\DB\simdac_be.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";<br />
this.oleDbInsertCommand1.CommandText = "INSERT INTO tblAirport(AirportCode, AirportName, City, Latitude, Longitude, State" +<br />
") VALUES (?, ?, ?, ?, ?, ?)";<br />
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;<br />
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("AirportCode", System.Data.OleDb.OleDbType.VarWChar, 4, "AirportCode"));<br />
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("AirportName", System.Data.OleDb.OleDbType.VarWChar, 50, "AirportName"));<br />
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("City", System.Data.OleDb.OleDbType.VarWChar, 20, "City"));<br />
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Latitude", System.Data.OleDb.OleDbType.VarWChar, 50, "Latitude"));<br />
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Longitude", System.Data.OleDb.OleDbType.VarWChar, 50, "Longitude"));<br />
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("State", System.Data.OleDb.OleDbType.VarWChar, 2, "State"));<br />
this.oleDbSelectCommand1.CommandText = "SELECT AirportCode, AirportID, AirportName, City, Latitude, Longitude, State FROM" +<br />
" tblAirport";<br />
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;<br />
this.oleDbUpdateCommand1.CommandText = "UPDATE tblAirport SET AirportCode = ?, AirportName = ?, City = ?, Latitude = ?, L" +<br />
"ongitude = ?, State = ? WHERE (AirportID = ?)";<br />
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("AirportCode", System.Data.OleDb.OleDbType.VarWChar, 4, "AirportCode"));<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("AirportName", System.Data.OleDb.OleDbType.VarWChar, 50, "AirportName"));<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("City", System.Data.OleDb.OleDbType.VarWChar, 20, "City"));<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Latitude", System.Data.OleDb.OleDbType.VarWChar, 50, "Latitude"));<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Longitude", System.Data.OleDb.OleDbType.VarWChar, 50, "Longitude"));<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("State", System.Data.OleDb.OleDbType.VarWChar, 2, "State"));<br />
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_AirportID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "AirportID", System.Data.DataRowVersion.Original, null));<br />
this.dbListOfAirports1.DataSetName = "dbListOfAirports";<br />
this.dbListOfAirports1.Locale = new System.Globalization.CultureInfo("en-US");<br />
((System.ComponentModel.ISupportInitialize)(this.dbListOfAirports1)).EndInit();<br />
<br />
}<br />
#endregion<br />
<br />
private void BindListToDropDown()<br />
{<br />
oleDbConnection1.Open();<br />
OleDbDataReader OleDbReader = this.oleDbSelectCommand1.ExecuteReader(CommandBehavior.CloseConnection);<br />
DropDownList1.SelectedIndex=0;<br />
DropDownList1.DataSource=OleDbReader;<br />
DropDownList1.DataBind();<br />
<br />
OleDbReader.Close();<br />
oleDbConnection1.Close();<br />
}<br />
}
|
|
|
|
|
Hi Everyone!
I make a Webform1.aspx which contains an web user control. In Microsoft IE it can viewed perfectly, but in Mozilla for example, some property can not run properly. For example, when i hide a some control on my web form when it run on Mozilla, all control is visible. I don't know reason why and how can fix it ?
Đào Ngọc Hào! Hi How Are You?
|
|
|
|
|
Hi,
I 've a function which takes 2 parameters, a dataset , path to XSD file. Based on contents of the xsd file i need to modify data in daatset to generate appropriate xml.How do I do this?
Thanks in advance
Priya
|
|
|
|
|
Hi Dpriya,
Guess the following code will give you an idea to solve the above problem.
The following code will take a XML file as the datasource(abc.xml) and will generate another xml file (abc234.xml) based on the schema (Test.xsd).
DataSet ds;
XmlDataDocument ddc = new XmlDataDocument();
ds = ddc.DataSet;
ds.ReadXmlSchema(@"Test.xsd");
ddc.Load(@"abc.xml");
ds.WriteXml(@"abc234.xml");
Hope this will help you.
Thanks and Regards
SGS
|
|
|
|
|
Hi
I extended a textbox control and created a property i.e
public enum myVarEnum {Apple,Mango,Peach};
protected myVarEnum myvar1;
public myVar MyProperty
{
get { return myvar1; }
set { myvar1 = value; }
}
Now when i use this extended control i am getting the property as a dropdown and i am able to select, the problem is the value i select doesnot persist..for example if i select "Mango" it goes back to the first one "Apple" after i build the solution. i guess its a small mistake plz help me overcome this...
Regards
Deepak.S
|
|
|
|
|
Apparently, the problem may be due to the fact that Visual C# does not allow you to create parameterized properties except for the indexer. The indexer is a specialized property that allows one to expose a group of values or objects on the name of the object. The name used in the indexer is required to be "this", i.e., the name of the object itself would be required to be used to access the property. An example would be like this:
private int[] myArray;
public int this[int index]
{
get
{
return myArray[index];
}
set
{
myArray[index] = value;
}
}
Try the above method and see whether it works.
|
|
|
|
|
Indexers allows to expose a group of values but how can i show those values in the designer so that the user can select one. I have to set other properties based on the selection made by the user. i guess indexers are availabe only in code on a object of that class.
Any other suggestions....
|
|
|
|
|
This works for objects:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public myVar MyProperty
{
get { return myvar1; }
set { myvar1 = value; }
}
don't know about enums though - maybe this will work
[DefaultValue(false)]
public myVar MyProperty
{
get { return myvar1; }
set { myvar1 = value; }
}
-spif2001
|
|
|
|
|
In fact, in the limited feature desired by you, I tried the method being used by you, and good news for you, it works!
The only change I made in your code (as shown above) is that I have used "public myVarEnum MyProperty" instead of "public myVar MyProperty" as used in your code (which appears to be a typing mistake), while defining the property.
In the sample small project prepared by me, the values "Mango" and "Peach" also persist if set at design time from the Property window from the dropdown. If you wish, I can separately send you the sample project files. Otherwise, I am including the source code of the MyButton.cs file (the inherited control) and the Form1.cs hereinbelow for your information:
using System;
namespace TestControl2
{
public class MyTextBox : System.Windows.Forms.TextBox
{
public enum myVarEnum {Apple,Mango,Peach};
protected myVarEnum myvar1;
public myVarEnum MyProperty
{
get { return myvar1; }
set { myvar1 = value; }
}
public MyTextBox()
{
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestControl2
{
public class Form1 : System.Windows.Forms.Form
{
private TestControl2.MyTextBox myTextBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.myTextBox1 = new TestControl2.MyTextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.myTextBox1.Location = new System.Drawing.Point(184, 48);
this.myTextBox1.MyProperty = TestControl2.MyTextBox.myVarEnum.Peach;
this.myTextBox1.Name = "myTextBox1";
this.myTextBox1.TabIndex = 0;
this.myTextBox1.Text = "myTextBox1";
this.button1.Location = new System.Drawing.Point(88, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(88, 176);
this.label1.Name = "label1";
this.label1.TabIndex = 2;
this.label1.Text = "label1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.myTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
label1.Text = myTextBox1.MyProperty.ToString();
}
}
}
The last method button1_Click, puts the value "Mango" etc. in the label1 on Form1. Hope it solves your problem.
|
|
|
|
|
I want to add a delay at the end of my while loop, so that it executes once every 10 minutes but am unsure how that works in C#.
I've tried Thread.Sleep, but it doesnt delay the loop.
Any suggestions?
|
|
|
|
|