7 Replies - 1508 Views - Last Post: 29 March 2009 - 02:55 PM Rate Topic: -----

#1 cheese_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 09

C# Windows Application problem

Post icon  Posted 29 March 2009 - 08:41 AM

Hi,

I'm new with C# and I have a little problem.

I'm trying to make button and when the user clicks the button,
textbox shows message "This works!"

However, nothing happens when the button is pressed down.

Could you please help me?

The code is listed below.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button NaytaTeksti;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

private void NaytaTeksti_Click(object sender, EventArgs e)
{
string text = "This works!";
textBox1.Text = text;
}




/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.NaytaTeksti = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
// 
// NaytaTeksti
// 
this.NaytaTeksti.Location = new System.Drawing.Point(80, 88);
this.NaytaTeksti.Name = "NaytaTeksti";
this.NaytaTeksti.Size = new System.Drawing.Size(128, 23);
this.NaytaTeksti.TabIndex = 0;
this.NaytaTeksti.Text = "Nayta";
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(96, 120);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.NaytaTeksti);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
}
}



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: C# Windows Application problem

#2 wotsitMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-March 09

Re: C# Windows Application problem

Posted 29 March 2009 - 09:06 AM

You will need to add something like:
this.NaytaTeksti.Click += new System.EventHandler(this.NaytaTeksti_Click);

To the section private void InitializeComponent() under the section NaytaTeksti. Hope that makes sense/works.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# Windows Application problem

Posted 29 March 2009 - 09:37 AM

That's because you have no click event for that button. Take a look at the modifications I made in your code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button NaytaTeksti;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

private void NaytaTeksti_Click(object sender, EventArgs e)
{
string text = "This works!";
textBox1.Text = text;
}




/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.NaytaTeksti = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// NaytaTeksti
//
this.NaytaTeksti.Location = new System.Drawing.Point(80, 88);
this.NaytaTeksti.Name = "NaytaTeksti";
this.NaytaTeksti.Size = new System.Drawing.Size(128, 23);
this.NaytaTeksti.TabIndex = 0;
this.NaytaTeksti.Text = "Nayta";

/****************************************
		 MODIFICATIONS HERE
****************************************/
//Now we need a click event for the button
this.NayaTeski.Click += new System.EventHandler(this.NaytaTeksti_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 120);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.NaytaTeksti);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}




Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 cheese_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 09

Re: C# Windows Application problem

Posted 29 March 2009 - 10:16 AM

Many thanks, I got it work!

However, I have now second practise and the same problem.

I added Event Handler manually, but this time it's not working.

I got an error:
Error 1 Cannot assign to 'CreateDataSetButton_Click' because it is a 'method group'

Can you please tell me what this mean?

Best Regards,
Cheese
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# Windows Application problem

Posted 29 March 2009 - 10:24 AM

Snow us the code you're using in code tags, like so => :code:
Was This Post Helpful? 0
  • +
  • -

#6 cheese_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 09

Re: C# Windows Application problem

Posted 29 March 2009 - 10:33 AM

Hi again.

Here's the code (form1.cs):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XMLjaDataSer
{
	 public partial class Form1 : Form
	 {
		 private DataSet _CustomersDS = new DataSet("Customers");
		 public Form1()
		 {
			 InitializeComponent();
		 }

		 private void CreateDataSetButton_Click(object sender, EventArgs e)
		 {
			 DataTable infoDataTable = new DataTable("infoDataTable"); // create a datatable for our dataset

			 DataColumn newCol = new DataColumn(); // Create a DataColumn for our DataTabe
			 newCol.DataType = System.Type.GetType("System.String"); // Make the Column type a string

			 newCol.ColumnName = "Names"; // Give the Column a name

			 infoDataTable.Columns.Add(newCol); // Add the new Column to the DataTable

			 _CustomersDS.Tables.Add(infoDataTable); // Add the DataTable to the DataSet

			 MessageBox.Show("Done");
		 }

		 private void FillDataSetButton_Click(object sender, EventArgs e)
		 {
			 DataRow newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); //Create a Datarow
			 newDataRow["Names"] = "Donald Duck";// Add a name to the Column  
			_CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet

			 newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); //Create a Datarow
			 newDataRow["Names"] = "Homer Simpson"; // Add a name to the Column
			 _CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet

			 newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); //Create a Datarow
			 newDataRow["Names"] = "Bart Simpson"; // Add a name to the Column
			 _CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet

			 MessageBox.Show("Done");
		 }

		 private void ShowContentsButton_Click(object sender, EventArgs e)
		 {
			 // loop through each row and display each item in the Name Column
			 foreach (DataRow currDR in _CustomersDS.Tables["infoDataTable"].Rows)
			 {
				 MessageBox.Show(currDR["Names"].ToString());
			 }
		 }

		 private void SaveDataSetButton_Click(object sender, EventArgs e)
		 {

			 _CustomersDS.WriteXml("Comics.xml"); // this will just save the data see below to see more info

			 MessageBox.Show("DataSet Saved");

			 System.IO.FileStream myFileStream = new System.IO.FileStream("Comics.xml", System.IO.FileMode.Create);
			 System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
			 _CustomersDS.WriteXml(myXmlWriter, XmlWriteMode.WriteSchema);
			 myXmlWriter.Close();

		 }

		 private void AddDataButton_Click(object sender, EventArgs e)
		 {
			 if (_CustomersDS.Tables["infoDataTable"] != null)
			 {
				 DataRow newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); // Create a datarow
				 newDataRow["Names"] = AddDataTextBox.Text; // Add a name to the column from the test box
				 _CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet
				 _CustomersDS.AcceptChanges();
				 MessageBox.Show("Done");
			 }
		 }

		 private void BindButton_Click(object sender, EventArgs e)
		 {
			 DataTable infoDataTable = _CustomersDS.Tables["infoDataTable"];
			 DisplayListBox.DataSource = infoDataTable; // Bind to the list box
			 DisplayListBox.DisplayMember = "Names"; // Set the column to bind to.
		 }


	 }
}





and here's the code: (form1.designer.cs)

namespace XMLjaDataSer

{

partial class Form1

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;

private System.Windows.Forms.MainMenu mainMenu1;

/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.mainMenu1 = new System.Windows.Forms.MainMenu();

this.BindButton = new System.Windows.Forms.Button();

this.CreateDataSetButton = new System.Windows.Forms.Button();

this.FillDataSetButton = new System.Windows.Forms.Button();

this.ShowContentsButton = new System.Windows.Forms.Button();

this.DisplayListBox = new System.Windows.Forms.ListBox();

this.SaveDatasetButton = new System.Windows.Forms.Button();

this.AddDataButton = new System.Windows.Forms.Button();

this.AddDataTextBox = new System.Windows.Forms.TextBox();

this.SuspendLayout();

// 

// BindButton

// 

this.BindButton.Location = new System.Drawing.Point(144, 18);

this.BindButton.Name = "BindButton";

this.BindButton.Size = new System.Drawing.Size(72, 20);

this.BindButton.TabIndex = 0;

this.BindButton.Text = "Bind";

// 

// CreateDataSetButton

// 

this.CreateDataSetButton.Location = new System.Drawing.Point(77, 140);

this.CreateDataSetButton.Name = "CreateDataSetButton";

this.CreateDataSetButton.Size = new System.Drawing.Size(119, 20);

this.CreateDataSetButton.TabIndex = 1;

this.CreateDataSetButton.Text = "Create DataSet";


// 

// FillDataSetButton

// 

this.FillDataSetButton.Location = new System.Drawing.Point(77, 166);

this.FillDataSetButton.Name = "FillDataSetButton";

this.FillDataSetButton.Size = new System.Drawing.Size(119, 20);

this.FillDataSetButton.TabIndex = 2;

this.FillDataSetButton.Text = "FillDataSet";

// 

// ShowContentsButton

// 

this.ShowContentsButton.Location = new System.Drawing.Point(77, 192);

this.ShowContentsButton.Name = "ShowContentsButton";

this.ShowContentsButton.Size = new System.Drawing.Size(119, 20);

this.ShowContentsButton.TabIndex = 3;

this.ShowContentsButton.Text = "Show Contents";

// 

// DisplayListBox

// 

this.DisplayListBox.BackColor = System.Drawing.SystemColors.ActiveCaptionText;

this.DisplayListBox.Location = new System.Drawing.Point(3, 18);

this.DisplayListBox.Name = "DisplayListBox";

this.DisplayListBox.Size = new System.Drawing.Size(100, 100);

this.DisplayListBox.TabIndex = 4;

// 

// SaveDatasetButton

// 

this.SaveDatasetButton.Location = new System.Drawing.Point(77, 245);

this.SaveDatasetButton.Name = "SaveDatasetButton";

this.SaveDatasetButton.Size = new System.Drawing.Size(119, 20);

this.SaveDatasetButton.TabIndex = 6;

this.SaveDatasetButton.Text = "Save Dataset";

// 

// AddDataButton

// 

this.AddDataButton.Location = new System.Drawing.Point(144, 219);

this.AddDataButton.Name = "AddDataButton";

this.AddDataButton.Size = new System.Drawing.Size(72, 20);

this.AddDataButton.TabIndex = 7;

this.AddDataButton.Text = "Add Data";

// 

// AddDataTextBox

// 

this.AddDataTextBox.Location = new System.Drawing.Point(3, 219);

this.AddDataTextBox.Name = "AddDataTextBox";

this.AddDataTextBox.Size = new System.Drawing.Size(100, 21);

this.AddDataTextBox.TabIndex = 8;

// 

// Form1

// 

this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;

this.AutoScroll = true;

this.ClientSize = new System.Drawing.Size(240, 268);

this.Controls.Add(this.AddDataTextBox);

this.Controls.Add(this.AddDataButton);

this.Controls.Add(this.SaveDatasetButton);

this.Controls.Add(this.DisplayListBox);

this.Controls.Add(this.ShowContentsButton);

this.Controls.Add(this.FillDataSetButton);

this.Controls.Add(this.CreateDataSetButton);

this.Controls.Add(this.BindButton);

this.Menu = this.mainMenu1;

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button BindButton;

private System.Windows.Forms.Button CreateDataSetButton;

private System.Windows.Forms.Button FillDataSetButton;

private System.Windows.Forms.Button ShowContentsButton;

private System.Windows.Forms.ListBox DisplayListBox;

private System.Windows.Forms.Button SaveDatasetButton;

private System.Windows.Forms.Button AddDataButton;

private System.Windows.Forms.TextBox AddDataTextBox;

}

}





Best Regards,
Cheese

This post has been edited by cheese_: 29 March 2009 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 cheese_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 09

Re: C# Windows Application problem

Posted 29 March 2009 - 12:18 PM

I got it work!

Except the SaveDatasetButton,
it's not saving any XML file.

Have you any ideas?

Best Regards,
Cheese
Was This Post Helpful? 0
  • +
  • -

#8 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: C# Windows Application problem

Posted 29 March 2009 - 02:55 PM

View Postcheese_, on 29 Mar, 2009 - 11:18 AM, said:

I got it work!

Except the SaveDatasetButton,
it's not saving any XML file.

Have you any ideas?

Best Regards,
Cheese



use xmlreader class to read from a file and xmlwriter class to write to a file here is the link for both
1) xmlreader

2)xmlwriter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1