PLEASE HELP : newbe want to put a text in a textbox and as a screenheader

G

G.Esmeijer

I'm coming from vb6 and starting with c# now. (a major leap)
I just want to set some properties in a class en and le the class set some
controls on the form

I have a form with on it a textBox (Test) and a button (txtNaam) and another
textBox (txtNaam2)

In the following code I want to fill the property screnheader with the conta
text and with the methode SetScreenHeader I want to put this text as a
formheader. and in the textbox txtNaam2.

The folloing code is behind the form:
But it simple produces nithing as the header of the screen.

WHY NOT : PLEASE HELP

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 frm0000 : System.Windows.Forms.Form
{

public frm0000()


class TestApp
{
public static void Main()
{
Application.Run(new frm0000());
}
}


private void cmdTest_Click(object sender, System.EventArgs e)
{
BuildScreen screen = new BuildScreen();
screen.ScreenHeader = txtNaam.Text;

screen.txtNaam.Text = screen.ScreenHeader;
screen.SetScreenHeader();

}

class BuildScreen
{
protected string screenheader;
protected frm0000 screen = new frm0000();

public string ScreenHeader
{
set { screenheader = value; }
get { return screenheader; }
}

public void SetScreenHeader()
{
screen.txtNaam2.Text = screenheader;
screen.Text = screenheader;
}
}
}
}
 
F

Frank Dzaebel

Hello,

it seems, that your example is not compilable.
Your row
screen.txtNaam.Text = screen.ScreenHeader;
must produce a compile error because the member
"txtNaam" is not defined in class BuildScreen().

May be give us your corrected code.
bye, Frank Dzaebel
 
G

Gerrit Esmeijer

It really can be compiled =--> no errors

You can check it:
Her is the complete code for that form
=================================================

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 frm0000 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtNaam;
private System.Windows.Forms.Button cmdTest;
private System.Windows.Forms.TextBox txtNaam2;


//private frm0000 screen = new frm0000();

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frm0000()
{
InitializeComponent();
}



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()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frm0000));
this.txtNaam = new System.Windows.Forms.TextBox();
this.cmdTest = new System.Windows.Forms.Button();
this.txtNaam2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtNaam
//
this.txtNaam.Location = new System.Drawing.Point(24, 32);
this.txtNaam.Name = "txtNaam";
this.txtNaam.Size = new System.Drawing.Size(136, 20);
this.txtNaam.TabIndex = 0;
this.txtNaam.Text = "";
//
// cmdTest
//
this.cmdTest.Location = new System.Drawing.Point(184, 32);
this.cmdTest.Name = "cmdTest";
this.cmdTest.Size = new System.Drawing.Size(56, 24);
this.cmdTest.TabIndex = 1;
this.cmdTest.Text = "test";
this.cmdTest.Click += new System.EventHandler(this.cmdTest_Click);
//
// txtNaam2
//
this.txtNaam2.Location = new System.Drawing.Point(24, 72);
this.txtNaam2.Name = "txtNaam2";
this.txtNaam2.Size = new System.Drawing.Size(136, 20);
this.txtNaam2.TabIndex = 2;
this.txtNaam2.Text = "";
//
// frm0000
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 134);
this.Controls.Add(this.txtNaam2);
this.Controls.Add(this.cmdTest);
this.Controls.Add(this.txtNaam);
this.Icon =
((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "frm0000";
this.ShowInTaskbar = false;
this.ResumeLayout(false);

}
#endregion

class TestApp
{
public static void Main()
{
Application.Run(new frm0000());
}
}


private void cmdTest_Click(object sender, System.EventArgs e)
{
BuildScreen screen = new BuildScreen();
screen.ScreenHeader = txtNaam.Text;

//txtNaam.Text = screen.ScreenHeader;
screen.SetScreenHeader();

}

class BuildScreen
{
protected string screenheader;
protected frm0000 screen = new frm0000();

public string ScreenHeader
{
set { screenheader = value; }
get { return screenheader; }
}

public void SetScreenHeader()
{
screen.txtNaam2.Text = screenheader;
screen.Text = screenheader;

}
}
}
}

============================================
 
T

Tyler

Your problem is that you are not showing the form on which you are setting
the text in the textbox and title bar - so you never see what the button
clicks are doing.

At the end of your SetScreenHeader() method add "screen.Show();" and then
you will see the form you have created with the textbox and title bar
populated with the contents of the textbox.

Tyler
 
G

G.Esmeijer

Tyler,

Thanks for your answer but...
A new screen is generated every time I call the SetScreenHeader() method.
How do I change the code in order to get the results on the screen that I
have active??
Regards
Gerrit esmeijer
 
F

Frank Dzaebel

If you replace your line
screen.Text = screenheader;
with
screen.Text = screenheader; screen.Show();
you see something more.

But the reason we cant help so much is,
that it is not so good explained
WHAT you want to do, sorry.

Nevertheless all beginning is difficult :)
 
G

G.Esmeijer

Thanks Frank,

I have written quit a lot in vb6.
Want to switch now to c#. (as I told a giant leap)
Bought a book from Tom Archer "Inside c#" and want to start of with some
simple things.
So I started with a form with on it two textBoxes (txtNaam and txtNaam2) and
a button.
I want to make a class with a propert and a method.
So I take the content of of txtNaam and store in the property screenheader
I now start the method SetScreenheader which does the following: placethe
content of screenheader on top of the form and also in textbox2 (txtNaam2).
All on the same screen

It was all ment to be a very simple exercise but it turn out to be a
brainbreaker for me.

So I hope that you can help me

regards
gerrit
 
F

Frank Dzaebel

Hi Gerrit,

the problem was that you created always a new instance
of class frm0000().
I corrected it by giving the class the original Form-
instance. You see it in the following code.
VB6 was not so much OOP-oriented.
With C# you learn a lot more in this area.

ciao, Frank Dzaebel

___________________

//Howto modify properties of form-objects with a class

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


namespace Newsgroup_screen
{
/// <summary>Summary description for Form1.</summary>
public class frm0000 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtNaam;
private System.Windows.Forms.Button cmdTest;
private System.Windows.Forms.TextBox txtNaam2;


//private frm0000 screen = new frm0000();

/// <summary>Required designer variable.</summary>
private System.ComponentModel.Container components
= null;

public frm0000()
{
InitializeComponent();
}



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.txtNaam = new System.Windows.Forms.TextBox
();
this.cmdTest = new System.Windows.Forms.Button();
this.txtNaam2 = new System.Windows.Forms.TextBox
();
this.SuspendLayout();
//
// txtNaam
//
this.txtNaam.Location = new System.Drawing.Point
(24, 32);
this.txtNaam.Name = "txtNaam";
this.txtNaam.Size = new System.Drawing.Size(136,
20);
this.txtNaam.TabIndex = 0;
this.txtNaam.Text = "my test string";
//
// cmdTest
//
this.cmdTest.Location = new System.Drawing.Point
(184, 32);
this.cmdTest.Name = "cmdTest";
this.cmdTest.Size = new System.Drawing.Size(56,
24);
this.cmdTest.TabIndex = 1;
this.cmdTest.Text = "test";
this.cmdTest.Click += new System.EventHandler
(this.cmdTest_Click);
//
// txtNaam2
//
this.txtNaam2.Location = new System.Drawing.Point
(24, 72);
this.txtNaam2.Name = "txtNaam2";
this.txtNaam2.Size = new System.Drawing.Size
(136, 20);
this.txtNaam2.TabIndex = 2;
this.txtNaam2.Text = "";
//
// frm0000
//
this.AutoScaleBaseSize = new System.Drawing.Size
(5, 13);
this.ClientSize = new System.Drawing.Size(292,
134);
this.Controls.Add(this.txtNaam2);
this.Controls.Add(this.cmdTest);
this.Controls.Add(this.txtNaam);
this.Name = "frm0000";
this.ShowInTaskbar = false;
this.ResumeLayout(false);

}
#endregion

class TestApp
{
public static void Main()
{
Application.Run(new frm0000());
}
}


private void cmdTest_Click(object sender,
System.EventArgs e)
{
BuildScreen screen = new BuildScreen
(this); //Gerrit, I added: (this)
screen.ScreenHeader = txtNaam.Text;
screen.SetScreenHeader();
}

class BuildScreen
{
protected string screenheader;
protected frm0000 screen; //Gerrit, I deleted:
= new frm0000()

/// <summary>Gerrit, I added this function: it
is the constructor of the class BuildScreen. You want
that the BuildScreen-class is doing all for you. So you
must give the class the Form-instance. Now you can access
the members of the instance of the class </summary>
/// <param name="form">The instance of the
frm0000-class to modify</param>
public BuildScreen(frm0000 frm)
{ screen = frm;
}

public string ScreenHeader
{
set { screenheader = value; }
get { return screenheader; }
}

public void SetScreenHeader()
{
screen.txtNaam2.Text = screenheader;
screen.Text = screenheader;
}
}
}
}
 
G

G.Esmeijer

Frank,

It works!!
Thank you very much for taking the effort and the patience to go through my
code and correct it where necesarry to make it work.
I must admit that vb6 was not that much OO-oriented. That the reason that
the step from vb6 to C# is a big one. I was already experimenting in all
possible ways to make this basic sample work. I see a lot of examples in
which controls are affected from only fromout the Main function.
I see that your originating somewhere from the .de area so I hope that
Danke herzlich will certainly be understood.

regards
Gerrit Esmeijer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top