Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 user control added to the main form.
1.UserControl1 -Take Input from UserControl2 and display message by using drawString
2.UserControl2 have TextBox,User can enter the text on it causes UserControl1 to display user entered text
PaintEvent is registered with UserControl1.
Upadate ,refresh and invalidate could not redraw the user entered text

[edit]
C#
// UserControl1
{
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        _titleText = textBox1.Text;
        _mainform = new MainForm();
        this.titleChanged += new TitleChangedHAndler(_mainform.onTitleChanged);
        if (titleChanged != null)
        {
            titleChanged(_titleText);
        }
    }     
}

// MainForm
    public void onTitleChanged(string strText)
    {
       UserControl2.setText(strText);
       ;
    }

// UserControl2
    private void paint(object sender, PaintEventArgs e)
    {
        drawString(Text,...);
    }
    public void setText(theText)
    {
        Text = theText;
        this.Invalidate();
        this.Update();
    }
}

[/edit]
Posted
Updated 24-Mar-15 4:43am
v5
Comments
Richard MacCutchan 24-Mar-15 9:49am    
Well that is sad. But if you want someone to help you you will need to show the relevant parts of your code.
Dave Kreskowiak 24-Mar-15 9:53am    
Well that is sad.

LOL!
Sarita S 24-Mar-15 9:55am    
content moved to question
Richard MacCutchan 24-Mar-15 10:07am    
I have moved your code to the original question, where it belongs.
I cannot see when or where the various parts of the code are called; I suggest you use your debugger, set some breakpoints, and check what is happening.
Sarita S 24-Mar-15 10:32am    
I debug the code it reach upto.
public void setText(theText)
{
Text = theText;
this.invalidate();
}
this.invalidate() line simple get scanned without calling the paint();

1 solution

your
C#
paint 
method is just private method. It will never be called unless you cal it specifically in your code. You need to override OnPaint and place your painting code there. It will be trigerred by on Invalidate.


C#
protected override void OnPaint(PaintEventArgs e)
{

    //Add your painting code  here
    base.OnPaint(e);
}
 
Share this answer
 
Comments
Sarita S 25-Mar-15 4:44am    
I used the onPaint() still could not call the paint.
Sarita S 25-Mar-15 4:46am    
// UserControl1
{
private void textBox1_TextChanged(object sender, EventArgs e)
{
_titleText = textBox1.Text;
_mainform = new MainForm();
this.titleChanged += new TitleChangedHAndler(UserControl2.onTitleChanged);
if (titleChanged != null)
{
titleChanged(_titleText);
}
}
}
// UserControl2
private void paint(object sender, PaintEventArgs e)
{
drawString(Text,...);
}
public void onTitleChanged(text)
{
Text = text;
this.invalidate
}
I did this changes..still could not cal the paint again.
Adam Zgagacz 25-Mar-15 9:19am    
Hmm... I dont see any OnPaint override in code in your comment. Are you sure you added it?

protected override void OnPaint(PaintEventArgs e)
{

//Add your painting code here
paint(this, e);
base.OnPaint(e);
}
Adam Zgagacz 25-Mar-15 9:21am    
Set the breakpoint in fist line of OnPaint. You should see how often it is called.
Sarita S 25-Mar-15 10:22am    
// UserControl1
{
private void textBox1_TextChanged(object sender, EventArgs e)
{
_titleText = textBox1.Text;
_mainform = new MainForm();
this.titleChanged += new TitleChangedHAndler(UserControl2.onTitleChanged);
if (titleChanged != null)
{
titleChanged(_titleText);
}
}
}
// UserControl2
protected override void OnPaint(PaintEventArgs e)
{
drawString(Text,...);
paint(this, e);
base.OnPaint(e);
}
public void onTitleChanged(text)
{
Text = text;
this.invalidate
}
Actually this is the new code.
Set the breakpoint at first line it get called only once.

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