Click here to Skip to main content
15,886,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a program works like the notepad with win form.

I want to open some txt file, and display them.

I have a class called DirtyCheckingTextBox which derived from TextBox,
I use the DirtyCheckingTextBox tbxContent in the form to display text.

in the openMenu click event :
C#
{
  // .. openfiledialog stuff
  var content = FileServices.GetContent(fileName); // get the txt content
  tbxContent = new DirtyCheckingTextbox(content);
}


the Constructor:

C#
 public DirtyCheckingTextbox() : this(String.Empty)
 {
 }

public DirtyCheckingTextbox(string initialText)
{
    this.Text = initialText;
    this.CleanText = this.Text;
}


but the problem is that I found the the debug window the tbxContent.Text has the txt files content, but the form doesn't display the text, why?

by default the tbxContent call the default constructor, I think the problem maybe I call the constructor again new DirtyCheckingTextbox(content);
Posted
Updated 12-Mar-14 1:22am
v2
Comments
ZurdoDev 12-Mar-14 7:22am    
You need to add tbxContent to the form, right? The way you have it now, it is only in memory.

As RyanDev said the testbox needs to be added to the form.

C#
TextBox DirtyCheckingTextbox= new TextBox();
DirtyCheckingTextbox.Location = new Point(200, 200);
this.Controls.Add(DirtyCheckingTextbox);
DirtyCheckingTextbox.Text = content ;


Good luck.
 
Share this answer
 
The problem is that you are creating a new instance of the DirtyCheckingTextbox and assigning it to the tbxContent variable - which doesn't display it, or update the version which is already on the display.

Instead, move your code into a method, and call that:
C#
public DirtyCheckingTextbox() : this(String.Empty)
    {
    }
    
public DirtyCheckingTextbox(string initialText)
    {
    Reset(initialText);
    }

public void Reset(string initialText)
    this.Text = initialText;
    this.CleanText = this.Text;
    }

C#
{
// .. openfiledialog stuff
var content = FileServices.GetContent(fileName); // get the txt content
tbxContent.Reset(content);
}


[edit]Types - very odd typos - OriginalGriff[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900