2013-01-03 16 views
6

Mam dość dużą (szerokość) C# WinForms aplikacji, która używa System.Windows.Forms.Label wewnątrz System.Windows.Forms.Panel jako markiza.Etykieta przypadkowo znika po pewnej długości

A System.Timers.Timer aktualizuje pozycję Label po zderzeniu.

int new_X_location = (label.Location.X + distance_invariant) % modulo; 
label.Location = new Point(new_X_location, label.Location.Y); 

Funkcjonalność namiotu nie jest kwestia, kiedy zmienić pole Label.Text etykieta znika!

string some_string = working_function_that_returns_string(); 
label.Text = some_string; //disappears! 

enter image description here

Wydaje się być ograniczone do długości około 2100 znaków lub tak, gdy rozmiar czcionki jest duży (24pt). Gdy jest mniejszy (10pt), ciąg może być znacznie dłuższy (label.Text.Length >= 4200).

string some_string = working_function_that_returns_string(); 
label.Text = some_string.SubString(0,2000); //it's still visibile here. 
... 
label.Text = some_string.SubString(0,2200) //it's not visible! 

Nie jestem pewien, czy ma do czynienia z szerokością ograniczeń lub ograniczenia rozmiaru czcionki lub pozycjonowania szerokości formy. . Pozycjonowanie jest poprawne przy mniejszych rozmiarach czcionek i krótszych łańcuchach. Dlatego nie jest to błąd pozycjonowania.

+1

mógłbyś pisać odpowiedni kod i/lub zdjęcia swojego problemu? – 3aw5TZetdf

+2

Nigdy nie używaj System.Timers.Timer do robienia czegokolwiek ze sterowaniem. Ustaw właściwość CheckForIllegalCrossThreadCalls na wartość true. –

+0

Zajmuję się tym z 'InvokeRequired' i' .Invoke() 'dziękuję. Nie ma żadnych problemów w tym zakresie. –

Odpowiedz

0

Czy próbowałeś/aś mieć etykietę o stałym rozmiarze (AutoSize false) z właściwością AutoEllipsis true? Jeśli jest to spowodowane ograniczeniem szerokości lub owinięciem, to powinno zniknąć.

Jeśli to nie rozwiąże problemu, możesz spojrzeć na kod pozycjonowania. Jeśli używa się szerokości etykiety przy obliczaniu pozycji, wówczas zmiany szerokości ze względu na zmianę tekstu mogą powodować pewne niespodzianki w niektórych przypadkach narożnych. Ponownie, pomocna może być ustalona etykieta rozmiaru (lub mająca maksymalny rozmiar).

+0

Właściwie chciałbym dokładnie odwrotne ("AutoSize = true", "AutoEllipsis = false"). Próbowałem tego, co powiedziałeś, a tekst nie zniknął, ale wydaje się bardzo krótki, ponieważ nie zmienia rozmiaru. to mogłoby oznaczać, że pozycjonowanie jest w porządku. –

+1

@ user1934851, jaka jest wartość rozmiaru etykiety przy dłuższym tekście? Określ próg (rozmiaru etykiety lub rozmiaru tekstu) i przełącz na "AutoSize = false, AutoEllipsis = true". Innym sposobem byłoby umożliwienie zawijania tekstu poprzez zwiększenie wysokości, ale jest to bardziej uciążliwe niż w poprzedniej opcji. – VinayC

+0

Nawet jeśli usunę zdarzenie tick, sprawiając, że etykieta będzie nieruchoma, problem pozostanie. Czy możesz bardziej szczegółowo opisać "zawijanie tekstu przez zwiększenie wysokości"? –

0

Używam też czegoś takiego! Wypróbuj to:

label1.Size = CreateGraphics().MeasureString(label_txt, label1.Font).ToSize(); 
0

możesz używać punktów załamania do tej funkcji.

(brkpnt)| string some_string = working_function_that_returns_string(); 

następnie podczas debugowania "wprowadź" tę funkcję. debuguj go krok po kroku. Sprawdź zmienne z okna autos. może mieć LOGICAL MISTAKE, który zwraca "" pusty ciąg znaków.

Musisz napisać, że working_function_that_returns_string();

inne İdea. można modyfikować textbox.bacgorundcolor niepodważalna, aby wyglądał jak etykiety

0

Moja próba usunięcia błędu:

// Try disabling the "AutoSize"-property, and use the panels' sizes 
label.AutoSize = false; 
label.Width = yourRedPanel.Width; 
label.Height = yourRedPanel.Height; 
label.TextAlign = ContentAlignment.MiddleLeft; 
label.AutoEllipsis = true; 

// Check that "new_X_location"-variable is not negative or 
// too big to move the label out of the viewable area 
label.Location = new System.Drawing.Point(new_X_location, label.Location.Y); 

// Check that some_string.Length is as great or greater than given Substring length argument 
label.Text = some_string.Substring(0, 2200); 

// The max font size is the biggest possible value of float 
Font testFont = new Font("Arial", float.MaxValue); 

// If this doesn't help, wrap your code to try-catch block 
// Run the code line by line (F10), and see where it jumps to "catch", 
// if there occurs errors 
try 
{ 
    // Code here 
} 
catch (Exception ex) { MessageBox.Show(ex.Message); } 
1

Utworzyłem aplikację testową, aby sprawdzić i myślę, że problem jest związany z użycie GDI+ i przyspieszania sprzętowego, z którego korzysta biblioteka. Na moim komputerze szerokość większa niż 8192 pikseli nie jest renderowana poprawnie (etykieta znika jak w twoim przypadku, gdy zmieniam tekst).

Ustawienie właściwości UseCompatibleTextRendering etykiety (a więc przy użyciu GDI i nie GDI+) tekst czynią prawie poprawnie, ale niektóre fragmenty są glify visibile na spodniej stronie etykiety.

Musisz przełamać swoją etykietę na kilka etykiet:

  • spróbować zerwać etykietę w chuncks nie większych niż 8192 pikseli.
  • jeśli tekst składa się z różnych okresów, można utworzyć etykietę dla każdego okresu (jest to szybsze i łatwiejsze).

Jest to kod testowy:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace LabelMaxChars 
{ 
    public partial class Form1 : Form 
    { 
     private Panel pnlStrip; 
     private Label lblText; 
     private Timer timer; 

     public Form1() 
     { 
      InitializeComponent(); 

      pnlStrip = new Panel(); 
      pnlStrip.Dock = DockStyle.Top; 
      pnlStrip.Height = 64; 
      pnlStrip.BackColor = SystemColors.ActiveCaption; 
      pnlStrip.ForeColor = Color.White; 

      lblText = new Label(); 
      lblText.Font = new Font("Microsoft Sans Serif", 12.0f, FontStyle.Regular, 
            GraphicsUnit.Point, ((byte)(0))); 
      lblText.Location = new Point(582, 6); 
      lblText.AutoSize = true; 
      lblText.UseCompatibleTextRendering = true; 
      lblText.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "+ 
          "Sed vestibulum elit ac nunc feugiat, non varius enim commodo. "+ 
          "Etiam congue, massa sollicitudin congue dapibus, odio erat blandit "+ 
          "lectus, non vehicula nisi lacus sed orci. Vestibulum ante ipsum primis "+ 
          "in faucibus orci luctus et ultrices posuere cubilia Curae; Donec ullamcorper "+ 
          "feugiat dui, at imperdiet elit pulvinar in. Sed ac fermentum massa. "+ 
          "Mauris hendrerit magna sit amet mi eleifend fringilla. "+ 
          "Donec pretium augue gravida enim fermentum placerat. "+ 
          "Vestibulum malesuada nisl a odio imperdiet condimentum. Sed vitae neque nulla. "+ 
          "Curabitur sed facilisis odio. Integer adipiscing, ante ac cursus dignissim, "+ 
          "ante sapien auctor ligula, id faucibus elit mauris nec nulla. "+ 
          "Sed elementum nisl id quam convallis dictum. Nullam nulla turpis, "+ 
          "elementum ac nisi in, faucibus eleifend est. "; 

      lblText.Text += lblText.Text; 
      lblText.Text += lblText.Text; 
      lblText.Text += lblText.Text; 
      lblText.Text += lblText.Text; 

      Console.WriteLine("Text length {0}", lblText.Text.Length); 

      pnlStrip.Controls.Add(lblText); 
      this.Controls.Add(pnlStrip); 

      timer = new Timer(); 
      timer.Interval = 10; 
      timer.Enabled = true; 
      timer.Tick += new EventHandler(timer_Tick); 
     } 

     private void timer_Tick(object sender, EventArgs e) 
     { 
      --lblText.Left; 
      if (lblText.Left == this.ClientSize.Width >> 1) 
      { 
       lblText.Text = "Nullam id nisl tortor. Donec in commodo magna. Integer dignissim vestibulum ipsum, " + 
       "ac lobortis nisl faucibus ac. Pellentesque convallis placerat est, " + 
       "non tempus mi scelerisque in. Sed vel aliquam tellus. " + 
       "Donec tincidunt elit et imperdiet egestas. Cras vel dictum lacus. " + 
       "Nullam mollis neque ac lectus congue, eget imperdiet risus feugiat. " + 
       "In commodo odio quis purus scelerisque, ut vestibulum justo vulputate. " + 
       "Proin sit amet facilisis libero. Donec mollis, enim at ultrices rhoncus, " + 
       "quam lectus condimentum ante, a varius urna nisl rutrum mi. " + 
       "Pellentesque sodales tincidunt suscipit. Cras semper sem vulputate, " + 
       "ornare eros sed, fringilla libero. Sed risus turpis, mollis vitae dictum eu, " + 
       "malesuada et magna. Etiam quis orci nunc. Morbi mattis ante a nibh hendrerit vehicula. "; 

       lblText.Text += lblText.Text; 
       lblText.Text += lblText.Text; 
       lblText.Text += lblText.Text; 
       lblText.Text += lblText.Text; 

       Console.WriteLine("Text length {0}", lblText.Text.Length); 
      } 

     } 
    } 
} 
Powiązane problemy