Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
QuestionTime change events Pin
monrobot133-Sep-05 6:25
monrobot133-Sep-05 6:25 
AnswerRe: Time change events Pin
if_mel_yes_else_no3-Sep-05 8:29
if_mel_yes_else_no3-Sep-05 8:29 
QuestionHttpWebRequest Timeout Problem Pin
surfman193-Sep-05 4:30
surfman193-Sep-05 4:30 
AnswerRe: HttpWebRequest Timeout Problem Pin
Andy Brummer3-Sep-05 14:21
sitebuilderAndy Brummer3-Sep-05 14:21 
GeneralRe: HttpWebRequest Timeout Problem Pin
surfman193-Sep-05 14:36
surfman193-Sep-05 14:36 
GeneralRe: HttpWebRequest Timeout Problem Pin
Andy Brummer3-Sep-05 17:03
sitebuilderAndy Brummer3-Sep-05 17:03 
GeneralRe: HttpWebRequest Timeout Problem Pin
surfman194-Sep-05 1:23
surfman194-Sep-05 1:23 
QuestionCurciut problem Pin
Niklas Ulvinge3-Sep-05 3:30
Niklas Ulvinge3-Sep-05 3:30 
I got a hard problem I don't know how to solve...
How do I, with the classes in item.cs, make elecrisity go from a battery throu two cords and back to the battery again. I'll implement the ohm's later...
How do the cord know when it should flow elecrisity.Confused | :confused:

item.cs:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace NiklasUlvinge.ChipsEater
{
    abstract class Item
    {
        //var's
        public Item[][] input;
        public int x;
        public int y;

        public abstract Strom Output
        {
            get;
        }
        public abstract Point[] Snap
        {
            get;
        }

        protected Item(int X, int Y)
        {
            x = X;
            y = Y;
            input = new Item[1][];
        }

        //abstract funcs
        public abstract void function();
        public abstract void draw(Graphics g);
        //funcs
        public void addBothInput(int index, int fromIndex, Item Input)
        {
            Input.addInput(fromIndex, this);

            this.addInput(index, Input);
        }
        private void addInput(int index, Item Input)
        {
            index = input.Length & index;

            Item[] inp = input[index];

            Item[] titem;

            if (inp == null)
            {
                titem = new Item[1];
            }
            else
                titem = new Item[inp.Length];

            for (int i = 0; i < titem.Length - 1; i++)
            {
                titem[i] = inp[i];
            }
            titem[titem.Length - 1] = Input;
            input[index] = titem;
        }
        public Strom combinedInput(int index)
        {
            Strom r = 0;
            if (input[index] == null)
                return 0;
            else
            {
                for (int i = 0; i < input[index].Length; i++)
                {
                    r += input[index][i].Output[j];
                }
                return r;
            }
        }

        //const
        protected Brush surf = Brushes.LightGray;
        protected Pen side = Pens.DarkGray;
        protected Pen loff = Pens.Black;
        protected Pen lon = Pens.Red;
    }

    class Null : Item
    {
        public override Strom Output
        {
            get
            {
            }
        }
        public override Point[] Snap
        {
            get
            {
                return new Point[0];
            }
        }

        public Null(int X, int Y)
            : base(X, Y)
        {
        }

        public override void function()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override void draw(Graphics g)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

    class Transistor : Item
    {
        protected float[] output;
        public Null theNull = new Null(-50, -50);


        public override Strom Output
        {
            get
            {
                Strom r;
                if (strom.l - strom.r > 0)
                {
                    r.l = strom.l;
                    r.r = strom.r;
                }
                else
                {
                    r.l = strom.l;
                    r.r = strom.r;
                }
            }
        }
        public override Point[] Snap
        {
            get
            {
                return new Point[3] {
                    new Point(x - 16, y - 1),
                    new Point(x + 3, y - 16),
                    new Point(x + 3, y + 16)};
            }
        }

        public Transistor(int X, int Y) : base(X, Y)
        {
            input = new Item[4][];
            output = new float[1];
        }

        public override void function()
        {
            if (combinedInput(0) > 0)
                output[0] = combinedInput(1);
            else
                output[0] = 0;
        }
        public override void draw(Graphics g)
        {
            IntPtr intp;
            //get the right image...
            if (combinedInput(0) == 0)
                if (combinedInput(1) == 0)
                    if (output[0] == 0)
                        intp = Properties.Resources.trans000.GetHbitmap();
                    else
                        intp = Properties.Resources.trans001.GetHbitmap();
                else
                    if (output[0] == 0)
                        intp = Properties.Resources.trans010.GetHbitmap();
                    else
                        intp = Properties.Resources.trans011.GetHbitmap();
            else
                if (combinedInput(1) == 0)
                    if (output[0] == 0)
                        intp = Properties.Resources.trans100.GetHbitmap();
                    else
                        intp = Properties.Resources.trans101.GetHbitmap();
                else
                    if (output[0] == 0)
                        intp = Properties.Resources.trans110.GetHbitmap();
                    else
                        intp = Properties.Resources.trans111.GetHbitmap();

            g.DrawImage(Image.FromHbitmap(intp), x - Image.FromHbitmap(intp).Size.Width / 2, y - Image.FromHbitmap(intp).Size.Height / 2);
        }
    }

    class Cord : Item
    {
        public int x2, y2;
        public Null theNull = new Null(-50, -50);

        public override Strom Output
        {
            get
            {
                Strom o,
                    l = combinedInput(0),
                    r = combinedInput(1);

                //what to do?
                
            }
        }
        public override Point[] Snap
        {
            get
            {
                return new Point[2] {
                    new Point(x, y),
                    new Point(x2, y2) };
            }
        }
        
        public Cord(int X1, int Y1)
            : base(X1, Y1)
        {
            input = new Item[2][];
        }

        public override void function()
        {
        }
        public override void draw(Graphics g)
        {
            g.DrawLine(combinedInput(0) > 0 ? lon : loff,
                x, y, x2, y2);
        }
    }

    class Battery : Item
    {
        public Null theNull = new Null(-50, -50);

        public override float[] Output
        {
            get
            {
                return new float[1] { 1 };
            }
        }
        public override Point[] Snap
        {
            get
            {
                return new Point[2] {
                    new Point(x - 5, y),
                    new Point(x + 5, y) };
            }
        }

        public Battery(int X, int Y)
            : base(X, Y)
        {
        }

        public override void function()
        {
        }
        public override void draw(Graphics g)
        {
            g.DrawLine(lon, x - 2, y - 10, x - 2, y + 10);
            g.DrawLine(lon, x + 2, y - 6, x + 2, y + 6);
            g.DrawLine(lon, x - 2, y, x - 5, y);
            g.DrawLine(lon, x + 2, y, x + 5, y);
        }
    }
}

form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NiklasUlvinge.ChipsEater
{
    public partial class Form1 : Form
    {
        Item[] items = new Item[0];
        bool mouseIsDown = false;
        Point startingPoint;
        Cord newCord;

        public Form1()
        {
            InitializeComponent();
        }

        private void PB_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.Clear(Color.White);

            for (int i = 0; i < items.Length; i++)
            {
                items[i].draw(g);
            }

        }


        private void addItem(Item item)
        {
            Item[] titem = new Item[items.Length + 1];

            for (int i = 0; i < items.Length; i++)
            {
                titem[i] = items[i];
            }
            titem[items.Length] = item;
            items = titem;
        }

        private void PB_MouseClick(object sender, MouseEventArgs e)
        {
            switch (treeItems.SelectedNode.Name)
            {
            case "NodeLst1":
                addItem(new Transistor(e.X,e.Y));
                break;
            case "NodeLst2":
                addItem(new Battery(e.X, e.Y));
                break;
            }

            this.Refresh();
        }

        private void PB_MouseDown(object sender, MouseEventArgs e)
        {
            switch (treeItems.SelectedNode.Name)
            {
            case "NodeLst0":
                startingPoint = new Point(-2,-2);
                //if the point should snapp
                for (int i = 0; i < items.Length; i++)
                {
                    Point[] pts = items[i].Snap;
                    for (int j = 0; j < pts.Length; j++)
                    {
                        if ((Math.Abs(pts[j].X - e.X) < 5) && (Math.Abs(pts[j].Y - e.Y) < 5))
                        {
                            startingPoint = new Point(pts[j].X, pts[j].Y);
                            newCord = new Cord(startingPoint.X, startingPoint.Y);
                            
                            items[i].addBothInput(0,0,newCord);
                        }
                    }
                }
                if (startingPoint == new Point(-2, -2))
                {
                    startingPoint = new Point(e.X, e.Y);
                    newCord = new Cord(startingPoint.X, startingPoint.Y);
                }
                mouseIsDown = true;
                break;
            }
        }
        private void PB_MouseUp(object sender, MouseEventArgs e)
        {
            switch (treeItems.SelectedNode.Name)
            {
            case "NodeLst0":
                mouseIsDown = false;
                int theX = e.X,
                    theY = e.Y;

                for (int i = 0; i < items.Length; i++)
                {
                    Point[] pts = items[i].Snap;
                    for (int j = 0; j < pts.Length; j++)
                    {
                        if ((Math.Abs(pts[j].X - e.X) < 5) && (Math.Abs(pts[j].Y - e.Y) < 5))
                        {
                            theX = pts[j].X;
                            theY = pts[j].Y;
                            items[i].addBothInput(j,0, newCord);
                        }
                    }
                }

                
                //if it snapped to input
                    newCord.x2 = theX;
                    newCord.y2 = theY;

                addItem(newCord);
                newCord = null;
                break;
            }
            this.Refresh();
        }
        private void PB_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseIsDown)
            {
                Graphics g = PB.CreateGraphics();
                this.Refresh();
                g.DrawLine(Pens.Black, startingPoint, new Point(e.X, e.Y));
            }
        }

    }
}


Niklas Ulvinge aka IDK
AnswerRe: Curciut problem Pin
rudy.net3-Sep-05 5:21
rudy.net3-Sep-05 5:21 
GeneralRe: Curciut problem Pin
Niklas Ulvinge3-Sep-05 5:33
Niklas Ulvinge3-Sep-05 5:33 
QuestionNow, how to extract low nibble and high nibble from a byte? Pin
bouli3-Sep-05 3:15
bouli3-Sep-05 3:15 
AnswerRe: Now, how to extract low nibble and high nibble from a byte? Pin
Colin Angus Mackay3-Sep-05 3:21
Colin Angus Mackay3-Sep-05 3:21 
AnswerRe: Now, how to extract low nibble and high nibble from a byte? Pin
Guffa3-Sep-05 6:09
Guffa3-Sep-05 6:09 
QuestionPlease Heeeeeeeeelp: Problem with Pinvoke??? Pin
E6AD3-Sep-05 2:46
E6AD3-Sep-05 2:46 
Questionlooping statement Pin
Anonymous3-Sep-05 1:53
Anonymous3-Sep-05 1:53 
QuestionRe: looping statement Pin
Guffa3-Sep-05 2:10
Guffa3-Sep-05 2:10 
AnswerRe: looping statement Pin
Libor Tinka3-Sep-05 2:42
Libor Tinka3-Sep-05 2:42 
AnswerRe: looping statement Pin
Colin Angus Mackay3-Sep-05 3:23
Colin Angus Mackay3-Sep-05 3:23 
QuestionMy C++ MFC-app into a dll -&gt; to be used in .NET app&#8230; Pin
anderslundsgard3-Sep-05 1:25
anderslundsgard3-Sep-05 1:25 
QuestionHow to concatenate 2 nibbles in 1 byte? Pin
bouli3-Sep-05 1:22
bouli3-Sep-05 1:22 
AnswerRe: How to concatenate 2 nibbles in 1 byte? Pin
Guffa3-Sep-05 2:10
Guffa3-Sep-05 2:10 
GeneralRe: How to concatenate 2 nibbles in 1 byte? Pin
bouli3-Sep-05 2:11
bouli3-Sep-05 2:11 
AnswerRe: How to concatenate 2 nibbles in 1 byte? Pin
leppie3-Sep-05 2:11
leppie3-Sep-05 2:11 
GeneralRe: How to concatenate 2 nibbles in 1 byte? Pin
bouli3-Sep-05 2:12
bouli3-Sep-05 2:12 
AnswerRe: How to concatenate 2 nibbles in 1 byte? Pin
Anonymous3-Sep-05 2:13
Anonymous3-Sep-05 2:13 

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.