Click here to Skip to main content
14,768,167 members
Home » Discussions » .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionConnecting to remote server <IP> failed with the following error message. Pin
Y_Kaushik19-May-17 3:54
MemberY_Kaushik19-May-17 3:54 
AnswerRe: Connecting to remote server <IP> failed with the following error message. Pin
Richard Deeming23-May-17 2:28
mveRichard Deeming23-May-17 2:28 
QuestionCalling a Console Application with Elevated rights opening Windows continuously Pin
indian1431-May-17 9:59
Memberindian1431-May-17 9:59 
AnswerRe: Calling a Console Application with Elevated rights opening Windows continuously Pin
Dave Kreskowiak1-May-17 10:47
mveDave Kreskowiak1-May-17 10:47 
GeneralRe: Calling a Console Application with Elevated rights opening Windows continuously Pin
indian1431-May-17 11:04
Memberindian1431-May-17 11:04 
GeneralRe: Calling a Console Application with Elevated rights opening Windows continuously Pin
Dave Kreskowiak1-May-17 11:51
mveDave Kreskowiak1-May-17 11:51 
GeneralRe: Calling a Console Application with Elevated rights opening Windows continuously Pin
indian1431-May-17 14:07
Memberindian1431-May-17 14:07 
Question(VS 2008 - C# - Compact Framework) problem with e.Graphics.DrawString Pin
steve_949661327-Apr-17 5:27
professionalsteve_949661327-Apr-17 5:27 
Hello to all,
I'm writing an application using C# in VisualStudio 2008 for Windows Embedded Compact 7 operating system, that is a SmartDevice application.
I have a Form and a UserControl.
In the Form load event I create a UserControl instance and I put it in the form:
private void Form1_Load(object sender, EventArgs e)
{
  UserControl1 uc1 = new UserControl1();
  uc1.Parent = this;
  uc1.Location = new Point(607, 350);
}

This works.

I want also to write a string in the UserControl from Form1.
I tried writing the following function in the UserControl:
public void SetText(String testo)
    {
      Graphics gr;
      PaintEventArgs e;

      gr = CreateGraphics();
      Font f = new Font("Tahoma", 14, FontStyle.Bold);
      e = new PaintEventArgs(gr, ClientRectangle);
      SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText);
      StringFormat sf = new StringFormat();
      sf.Alignment = StringAlignment.Center;
      Rectangle centered = e.ClipRectangle;
      centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(testo, f).Height) / 2);

      e.Graphics.DrawString(testo, f, br, centered, sf);

    }

and I called it from Form1:
private void Form1_Load(object sender, EventArgs e)
{
  UserControl1 uc1 = new UserControl1();
  uc1.Parent = this;
  uc1.Location = new Point(607, 350);
  uc1.SetText("1");
}

but it doesn't work, it writes nothing without errors.

So I modified the function in the UserControl in this way:
public void SetText(String testo, PaintEventArgs e)
{
  Graphics gr;
  //PaintEventArgs e;
  gr = CreateGraphics();
  Font f = new Font("Tahoma", 14, FontStyle.Bold);
  //e = new PaintEventArgs(gr, ClientRectangle);
  SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText);
  StringFormat sf = new StringFormat();
  sf.Alignment = StringAlignment.Center;
  Rectangle centered = e.ClipRectangle;
  centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(testo, f).Height) / 2);

  e.Graphics.DrawString(testo, f, br, centered, sf);

}

and I called it in this way:
private void Form1_Load(object sender, EventArgs e)
{
  UserControl1 uc1 = new UserControl1();
  uc1.Parent = this;
  uc1.Location = new Point(607, 350);

  PaintEventArgs ea;
  Graphics gr;
  gr = CreateGraphics();
  ea = new PaintEventArgs(gr, uc1.ClientRectangle);

  uc1.SetText("1", ea);
}

still doesn't work without errors.

If I use the same code in the UserControl Paint event it works:
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
  Font f = new Font("Tahoma", 14, FontStyle.Bold);
  string s = "1";
  SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText);
  StringFormat sf = new StringFormat();
  sf.Alignment = StringAlignment.Center;
  Rectangle centered = e.ClipRectangle;
  centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(s, f).Height) / 2);

  e.Graphics.DrawString(s, f, br, centered, sf);
}


It works also if I call the SetText function from the UserControl Paint Event:
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
  SetText("1");
}

or:
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
  SetText("1",e);
}


I think the problem could be in the PaintEventArgs: when I work inside the UserControl Paint event I have the "right" PaintEventArgs objec, when I try to write my string from Form1 I'm not able to point to the "right" PaintEventArgs.

Obviously my suspicion can be wrong.

I don't know how to solve this problem, someone can help me?

Thanks in advance.
AnswerRe: (VS 2008 - C# - Compact Framework) problem with e.Graphics.DrawString Pin
Dave Kreskowiak27-Apr-17 5:44
mveDave Kreskowiak27-Apr-17 5:44 
GeneralRe: (VS 2008 - C# - Compact Framework) problem with e.Graphics.DrawString Pin
steve_949661327-Apr-17 6:21
professionalsteve_949661327-Apr-17 6:21 
QuestionVS 2017 Could not find required file 'setup.bin' in csproj folder Pin
Vitor_Silva26-Apr-17 11:07
MemberVitor_Silva26-Apr-17 11:07 
AnswerRe: VS 2017 Could not find required file 'setup.bin' in csproj folder Pin
Richard MacCutchan26-Apr-17 22:11
mveRichard MacCutchan26-Apr-17 22:11 
QuestionHow to install SignTool.exe for VS 2017? Pin
Vitor_Silva26-Apr-17 5:43
MemberVitor_Silva26-Apr-17 5:43 
AnswerRe: How to install SignTool.exe for VS 2017? Pin
Vitor_Silva26-Apr-17 6:18
MemberVitor_Silva26-Apr-17 6:18 
AnswerRe: How to install SignTool.exe for VS 2017? Pin
User 418025426-Apr-17 6:42
MemberUser 418025426-Apr-17 6:42 
GeneralRe: How to install SignTool.exe for VS 2017? Pin
Vitor_Silva26-Apr-17 9:49
MemberVitor_Silva26-Apr-17 9:49 
QuestionRun Applications as an Administrator using Scheduled Task Pin
indian14325-Apr-17 14:36
Memberindian14325-Apr-17 14:36 
QuestionType thanglish in keyboard when display tamil letters in textbox Pin
Member 1296305725-Apr-17 2:34
MemberMember 1296305725-Apr-17 2:34 
AnswerRe: Type thanglish in keyboard when display tamil letters in textbox Pin
User 418025425-Apr-17 2:59
MemberUser 418025425-Apr-17 2:59 
AnswerRe: Type thanglish in keyboard when display tamil letters in textbox Pin
Afzaal Ahmad Zeeshan25-Apr-17 3:51
mveAfzaal Ahmad Zeeshan25-Apr-17 3:51 
QuestionGet the week of the month in string using C# Pin
indian14320-Apr-17 14:02
Memberindian14320-Apr-17 14:02 
AnswerRe: Get the week of the month in string using C# Pin
Afzaal Ahmad Zeeshan20-Apr-17 14:24
mveAfzaal Ahmad Zeeshan20-Apr-17 14:24 
AnswerRe: Get the week of the month in string using C# Pin
Richard MacCutchan20-Apr-17 23:26
mveRichard MacCutchan20-Apr-17 23:26 
GeneralRe: Get the week of the month in string using C# Pin
indian14325-Apr-17 14:29
Memberindian14325-Apr-17 14:29 
QuestionMy (previously working) DateTimePickers have gone nuts! Pin
Peter R. Fletcher18-Apr-17 13:55
MemberPeter R. Fletcher18-Apr-17 13:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.