Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    77

    C# Location PictureBox

    salve ragazzi ho 1 problema .. ho una picturebox in coordinati (10,15) devo fare un ciclo for che la sposta fino a coordinate (x,y) questa picturebox contiene una foto però quando mando in esecuzione la picturebox spostandosi visualizza solo lo sfondo nero senza foto per poi farla ricomparire appena è arrivata a destinazione ... come dovrei fare ?

  2. #2
    Dov'è il codice? Usi un timer? Oppure una routine su un thread diverso? Usi Windows Forms oppure WPF? E quale versione del framework?

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    77
    codice:
     
         private void Sposta(string oggettoPadre,string oggettoDaSpostare, int OggettoDaSpostareX, int OggettoDaSpostareY)
            {
                //Se OggettoDaSpostareX e y sono più distanti dell'oggetto padre
                if (OggettoDaSpostareX > Controls[oggettoPadre].Location.X && OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareX > Controls[oggettoPadre].Location.X || OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        if (OggettoDaSpostareX > Controls[oggettoPadre].Location.X)
                            OggettoDaSpostareX--;
                        if (OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                            OggettoDaSpostareY--;
                        for (int i = 0; i < 2700000; i++)
                        { }
                        
                    }
                }
    
                //Se y è più distante dell'oggetto padre e x è uguale
                if (OggettoDaSpostareX == Controls[oggettoPadre].Location.X && OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        OggettoDaSpostareY--;
                        for (int i = 0; i < 2700000; i++)
                        { Controls[oggettoDaSpostare].Refresh(); }
                        
                    }
                }
    
                // Se x è meno distante del contenitore padre e Y è + distante
                if (OggettoDaSpostareX < Controls[oggettoPadre].Location.X && OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareX < Controls[oggettoPadre].Location.X || OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        if (OggettoDaSpostareX < Controls[oggettoPadre].Location.X)
                            OggettoDaSpostareX++;
                        if (OggettoDaSpostareY > Controls[oggettoPadre].Location.Y)
                            OggettoDaSpostareY--;
                        for (int i = 0; i < 2700000; i++)
                        { }
                    }
                }
    
                //Se x è meno distante dell'oggetto padre e y è uguale
                if (OggettoDaSpostareX < Controls[oggettoPadre].Location.X && OggettoDaSpostareY == Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareX < Controls[oggettoPadre].Location.X)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        OggettoDaSpostareX++;
                        for (int i = 0; i < 2700000; i++)
                        { }
                    }
                }
    
                //Se x e y sono meno distanti dell'oggetto padre
                if (OggettoDaSpostareX < Controls[oggettoPadre].Location.X && OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareX < Controls[oggettoPadre].Location.X || OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        if (OggettoDaSpostareX < Controls[oggettoPadre].Location.X)
                            OggettoDaSpostareX++;
                        if (OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                            OggettoDaSpostareY++;
                        for (int i = 0; i < 2700000; i++)
                        { }
                    }
                }
    
                //Se y è meno distante dell'oggetto padre e x è uguale
                if (OggettoDaSpostareX == Controls[oggettoPadre].Location.X && OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        OggettoDaSpostareY++;
                        for (int i = 0; i < 2700000; i++)
                        { }
                    }
                }
    
                // Se x è più distante del contenitore padre e Y è meno distante
                if (OggettoDaSpostareX > Controls[oggettoPadre].Location.X && OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareX > Controls[oggettoPadre].Location.X || OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        if (OggettoDaSpostareX > Controls[oggettoPadre].Location.X)
                            OggettoDaSpostareX--;
                        if (OggettoDaSpostareY < Controls[oggettoPadre].Location.Y)
                            OggettoDaSpostareY++;
                        for (int i = 0; i < 2700000; i++)
                        { }
                    }
                }
    
                //Se x più meno distante dell'oggetto padre e y è uguale
                if (OggettoDaSpostareX > Controls[oggettoPadre].Location.X && OggettoDaSpostareY == Controls[oggettoPadre].Location.Y)
                {
                    while (OggettoDaSpostareX > Controls[oggettoPadre].Location.X)
                    {
                        Controls[oggettoDaSpostare].Location = new Point(OggettoDaSpostareX, OggettoDaSpostareY);
                        OggettoDaSpostareX--;
                        for (int i = 0; i < 2700000; i++)
                        { }
                    }
                }
    
            }
    Questo è il metodo che sposta l'oggetto .... cmq uso un win form ... Framework 4.0

  4. #4
    Ovvio. Tutto il tuo codice è sincrono. Finché non termina, nessun'altra azione può essere eseguita, compreso il refresh dell'interfaccia. Se vuoi che lo spostamento avvenga asincronamente devi eseguire la routine in un thread diverso, oppure al tick di un timer.
    Se decidi di lanciarla su un altro thread, ricorda che non puoi modificare le proprietà dei controlli da quel thread, ma devi passare attraverso il thread che li ha creati.
    codice:
    private void Timer_Tick(Object sender, EventArgs e)
    {
        Controls["oggetto1"].Location = new Point(...);
    }
    codice:
    (new Thread(() => { Sposta("oggetto1", "oggetto2", x0, y0); })).Start()
    Altri consigli:
    - Quei for sono osceni. Potevi usare Thread.Sleep (che comunque è sconsigliabile).
    - Se una variabile si chiama OggettoDaSpostare, si presume che contenga l'oggetto da spostare, non il suo nome. Quindi decidi: o la chiami "NomeOggettoDaSpostare" (o qualcosa di più corto), oppure ne cambi il tipo in Control.
    - A cosa servono le coordinate X e Y se l'oggetto viene comunque spostato alle coordinate dell'oggetto padre?
    - "Se OggettoDaSpostareX e y sono più distanti dell'oggetto padre" non ha senso. Una coordinata non può essere "più distante" di un oggetto. Distante da cosa, poi? Semmai un oggetto - o un punto - è più distante di un altro oggetto o un altro punto da un ulteriore oggetto o punto.
    - Tutti queli if e while sono inutili.
    codice:
    while ( (currentX != targetX) && (currentY != targetY) )
    {
        currentX += (Int32)Math.Sign(targetX - currentX);
        currentY += (Int32)Math.Sign(targetY - currentY);
    }
    currentX e currentY rappresentano le tue Controls[OggettoDaSpostare].Location.X o .Y. targetX e targetY sono le coordinate da raggiungere.

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    77
    si ti ringrazio ho usato i for solamente per un primo approccio .... grazie dei consigli

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.