mardi 21 avril 2015

Function doesn't return value from another thread

I'm working with multithreading and faced with one strange moment. I have following code:

var advancedSearchView = ObjectFactory.ShowView<IGeneralReportAdvancedSearchView>()

In this case code works fine. But when I try to use BackgroundWorker fuction ShowView doesn't return any value.

BackgroundWorker backgroundWorker = new BackgroundWorker();

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
   var simpleView= ObjectFactory.ShowView<IGeneralReportSimpleView>();
}

string ShowView<T>()
{
return typeof(T).Name;
}

My backgroung worker calling from the same method as advancedSearchView

backgroundWorker.RunWorkerAsync();

Could you be so kind to advise how to solve current problem?

How to use onPaint on Custom Combobox?

Due to some constrains, I am in a position to create custom-combobox with which add a dot at right to it (while it load to the form). This can be label with filled color.

I try to use OnPaint event, it is not help me to achieve it.

Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)

    ' Create Label as per specification

    ' Add label to the parent of the combo-box
    Me.Parent.Controls.Add(lableControl)
End Sub

I may miss something here.. It may be very simple. But it swallows my time. I appreciate, suppose you have any other idea to achieve this. Thank you.

Custom group box not binding to bindingsource

I need to bind a GroupBox to a BindingSource, which in turn is bound to the following object:

public class CustomerType
{
    public int Id {get; set;}
    public string Name {get; set;}
    public MemberType MemberType {get; set;}
}

public enum MemberType {Adult, Child}

I followed this answer to create a custom GroupBox. I also set the data bindings as follows:

groupBoxMemberType.DataBindings.Add("Selected", this.bindingSource, "MemberType");

However, when loading an existing object, I get the following exception:

DataBinding cannot find a row in the list that is suitable for all bindings.

The exception occurs when setting the data source:

customerType = customerTypeRequest.Load(id);
bindingSource.DataSource = customerType;  //raises exception

What am I missing? Is there an alternative to get radio buttons to bind to a datasource, specifically a BindingSource?

lundi 20 avril 2015

How can i read all the lines from a text file in specific places and use the data?

string[] lines = File.ReadAllLines(@"c:\wmiclasses\wmiclasses1.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].StartsWith("ComboBox"))
                {

                }
            }

And this is how the text file content look like:

ComboBox Name cmbxOption

Classes Win32_1394Controller
Classes Win32_1394ControllerDevice

ComboBox Name cmbxStorage

Classes Win32_LogicalFileSecuritySetting
Classes Win32_TapeDrive

What i need to do is some things:

  1. Each time the line start with ComboBox then to get only the ComboBox name from the line for example cmbxOption.

    Since i have already this ComboBoxes in my form1 designer i need to identify where the cmbxOption start and end and when the next ComboBox start cmbxStorage.

  2. To get all the lines of the current ComboBox for example this lines belong to cmbxOption:

Classes Win32_1394Controller Classes Win32_1394ControllerDevice

  1. To create from each line a Key and Value for example from the line:

Classes Win32_1394Controller

Then the key will be Win32_1394Controller and the value will be only 1394Controller

Then the second line key Win32_1394ControllerDevice and value only 1394ControllerDevice

  1. To add to the correct belonging ComboBox only the value 1394Controller.

  2. To make that when i select in the ComboBox for example in cmbxOption the item 1394Controller it will act like i selected Win32_1394Controller.

For example in this event:

private void cmbxOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);

        }

In need that the SelectedItem will be Win32_1394Controller but the user will see in the cmbxOption only 1394Controller without the Win32_

This is the start of the method InsertInfo

private void InsertInfo(string Key, ref ListView lst, bool DontInsertNull)
        {

That's why i need that the Key will be Win32_1394Controller but i want that the user will see in the ComboBox only 1394Controller without the Win32_

Specified cast is not valid error in Datagridview c#

This is a really weird error I've never had before. I have added a checkbox to my datagridview like this:

DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
datagrid.Columns.Insert(0, checkBoxColumn);

I get the error

Specified cast is not valid

And it highlights this in cell formating of the datagrid:

for (int i = 0; i <= this.datagrid.Rows.Count - 1; i++)
{
    if ((int)datagrid.Rows[e.RowIndex].Cells[7].Value <= 5)
    {
        this.datagrid.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
    }

This checks if a value in cell 7 is below the value or equal to 5 and if it is changes it to red. I have tried changing the cell to 8 because I thought it had moved since I've added a new column at 0 however it didn't work.

Mouse pointer position when pointer is not on winform

I'm able to get the mouse position when it is within the form. Here is my code-

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    lblXPosition.Text = MousePosition.X.ToString();
    lblYPosition.Text = MousePosition.Y.ToString();
}

But it does not work when the pointer is outside the form. Any suggestion?

Loading form before while loop in game

I am developing a game in C# .But it has some infinite loop that prevents form from being loaded. I have tried many solutions available on internet, But that doesn't seems to work.

My problem is :"How to load the form before execution of while loop."?

My Program.cs Source

namespace SumSwamp
{
static class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var window = new Form1();
        window.Show();

    }
}
}

Form1.cs :

namespace SumSwamp
{
public partial class Form1 : Form
{
    public static int DieLargeNum = 0;
    public static int DieSmallNum = 0;
    public static int DieOperator = 0;
    public static int DieTotal = 0;
    public static int TotalSpaces = 42;
    public static int CompSum = 0;
    public static int PlayerSum = 0;
    public static Boolean PlayersRoll = true;
    public static Boolean WaitForRoll = true;
    public static int Turn = 0;

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(this.Form1_Load);
        while(Turn == 0) //INFINITE LOOP
        {
            if (WaitForRoll==false)
            {
                DieTotal=DieLargeNum;
                Random rnd1 = new Random();
                DieLargeNum = rnd1.Next(1, 7);
                if (DieTotal>DieLargeNum)
                {
                    Turn = 1;
                    labelStatus.Text = "Player 1's Turn";
                    WaitForRoll=true;
                }
                else
                {
                    Turn = 2;
                    labelStatus.Text = "Player 2's Turn";
                    WaitForRoll = false;
                }
            }
        }

        while ((CompSum < TotalSpaces) & (PlayerSum < TotalSpaces))//INFINITE LOOP
        {
            while (Turn == 1)
            {
                if (WaitForRoll == false)
                {
                    if (DieOperator == 1)
                    {
                        DieTotal = DieLargeNum + DieSmallNum;
                    }
                    else
                    {
                        if (DieLargeNum > DieSmallNum)
                        {
                            DieTotal = DieLargeNum - DieSmallNum;
                        }
                        else
                        {
                            DieTotal = DieSmallNum - DieLargeNum;
                        }

                    }
                    PlayerSum = PlayerSum + DieTotal;
                    Turn = 2;
                    PlayersRoll = false;
                    labelStatus.Text = "Player 2's Turn";
                }
            }

            while (Turn == 2)
            {
                Random rnd1 = new Random();
                DieLargeNum = rnd1.Next(1, 7);
                Random rnd2 = new Random();
                DieSmallNum = rnd2.Next(1, 7);
                Random rnd3 = new Random();
                DieOperator = rnd3.Next(1, 3);
                labelDieLargeNum.Text = DieLargeNum.ToString();
                labelDieSmallNum.Text = DieSmallNum.ToString();
                if (DieOperator == 1)
                {
                    labelDieOperator.Text = "+";
                }
                else
                {
                    labelDieOperator.Text = "-";

                }

                if (DieOperator == 1)
                {
                    DieTotal = DieLargeNum + DieSmallNum;
                }
                else
                {
                    if (DieLargeNum > DieSmallNum)
                    {
                        DieTotal = DieLargeNum - DieSmallNum;
                    }
                    else
                    {
                        DieTotal = DieSmallNum - DieLargeNum;
                    }
                }
                CompSum = CompSum + DieTotal; 
                Turn = 1;
                PlayersRoll = true;
                labelStatus.Text = "Player 1's Turn";                    
            }

        }
        if (CompSum>=TotalSpaces)
        {
            labelResult.Text = "CPU Player has won!";
        }
        else
        {
            labelResult.Text = "You win!";
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void buttonRoll_Click(object sender, EventArgs e)
    {
        if (PlayersRoll == true)
        {
            Random rnd1 = new Random();
            DieLargeNum = rnd1.Next(1, 7);
            Random rnd2 = new Random();
            DieSmallNum = rnd2.Next(1, 7);
            Random rnd3 = new Random();
            DieOperator = rnd3.Next(1, 3);
            WaitForRoll = false;
            labelDieLargeNum.Text = DieLargeNum.ToString();
            labelDieSmallNum.Text = DieSmallNum.ToString();
            if(DieOperator == 1)
            {
                labelDieOperator.Text = "+";
            }
            else
            {
                labelDieOperator.Text = "-";

            }

        }
    }

}
}