lundi 20 avril 2015

Can I create a class simply to populate a DataGridView with the values I want from two other classes?

Is it against best practices to create a class that serves the purpose of combining data from two other existing classes in order to create a DataGridView with only the elements I want from each of said classes? For example:

Class 1
int classOneId (Primary Key)
string classOneName

Class 2
int classOneId
string classTwoResult
datetime classTwoDate

I want to populate a DataGridView with elements from both of these classes, so could I create a third class:

Class 3
int classOneName
string classTwoResult
datetime classTwoDate

An object of this class would then be made by an SQL statement that joined the two previous classes by the classOneId. Then I could create a list of Class 3 objects to populate the DataGridView. Is this acceptable to do?

Compare two Clipboard IDataObject in C#

I'm using Clipboard events to copy data from clipboard to my application which includes a Microsoft Word.

it will paste data to the word as soon as the user hits copy anywhere in windows. But I don't want to paste the same data if the user hits Ctrl + C twice. Now as this answer suggests, I can't use IsCurrent to compare DataObjects

So I'm doing it this way:

if (Clipboard.GetData(DataFormats.UnicodeText).ToString()!=oldData)
  {
     //Paste and stuffs
     oldData= Clipboard.GetData(DataFormats.UnicodeText).ToString();
  }

But it only works if the data contains some texts and I'm getting null reference error if it doesn't. So is there any way to know if the DataObject contains anthing else than text (say Bitmap) and be able to compare them?

MSChart CandleStick Color Options Powershell

I am writing a process in Powershell to give me a 30 day rolling Candlestick chart. I have everything working. But the one thing that is bugging me is that the wick line(min,max) is blue (the default MSChart color). I can't find any option to change this.

Does anyone know what it is?

PowerShell Windows Form

I am trying to get some dynamic array values into a PowerShell windows Form. When I use the same command on the PowerShell console it works but in form I get a weird .dot net class returned. Below I mark the code that results in wrong output.

Import-Module NetAdapter
$nics = Get-NetAdapter | Select-Object name, interfacedescription,macaddress

function GenerateForm
{
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$frmVMTask = New-Object System.Windows.Forms.Form
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$btnCancel = New-Object System.Windows.Forms.Button
# $btnOK = New-Object System.Windows.Forms.Button
$ChkVMTask = New-Object System.Windows.Forms.CheckedListBox

$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState


$handler_ChkVMTask_itemCheckChanged =
{
    $VMTask = $ChkVMTask.CheckedItems
    If ($ChkVMTask.CheckedItems.count -gt 1)
    {
        $buttons = [system.windows.forms.messageboxbuttons]::ok;
        [System.Windows.Forms.MessageBoxDefaultButton]::Button1;
        $icon = [system.windows.forms.messageboxicon]::error;

        $tasks = [System.Windows.Forms.MessageBox]::Show("

############################
# Too Many VM Tasks are selected. #
#############################`n
Please Select 1 item only.
"
        , "VM Task Error Summary", $buttons, $icon)
    }
    else
    {
        $VMTask = $ChkVMTask.CheckedItems
    }
}
{
    # Close the form if Cancel clicked
    $frmVMTask.close()

}

$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 16
$System_Drawing_Point.Y = 16
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 940
$System_Drawing_Size.Width = 800
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 0
$groupBox1.TabStop = $False
$groupBox1.Text = "NIC Automation Task"
$groupBox1.add_Enter($handler_groupBox1_Enter)

$frmVMTask.Controls.Add($groupBox1)

$btnCancel.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 280
$System_Drawing_Point.Y = 430
$btnCancel.Location = $System_Drawing_Point
$btnCancel.Name = "btnCancel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 75
$btnCancel.Size = $System_Drawing_Size
$btnCancel.TabIndex = 15
$btnCancel.Text = "Cancel"
$btnCancel.UseVisualStyleBackColor = $True
$btnCancel.add_Click($handler_btnCancel_Click)

$groupBox1.Controls.Add($btnCancel)

$groupBox1.Controls.Add($lblTask)
$ChkVMTask.[Windows.Forms.SelectionMode]::One;
$ChkVMTask.CheckOnClick = $True;

$ChkVMTask.DataBindings.DefaultDataSourceUpdateMode = 0
$ChkVMTask.FormattingEnabled = $True
foreach ($nic in $nics)
{

Below is where output is code and not same as get-netadapter

    $ChkVMTask.Items.Add("$Nic") | Out-Null
}

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 54
$ChkVMTask.Location = $System_Drawing_Point
$ChkVMTask.Name = "ChkVMTask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 158
$System_Drawing_Size.Width = 722
$ChkVMTask.Size = $System_Drawing_Size
$ChkVMTask.TabIndex = 1
$ChkVMTask.Tag = ""
$ChkVMTask.add_SelectedIndexChanged($handler_ChkVMTask_itemCheckChanged)
$groupBox1.Controls.Add($ChkVMTask)

#Save the initial state of the form
$InitialFormWindowState = $frmVMTask.WindowState
#Init the OnLoad event to correct the initial state of the form
$frmVMTask.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$frmVMTask.ShowDialog() | Out-Null
$frmVMTask.close() #new parent test close visible and hidden work perfectly. close not yet.

} #End Function

#Call the Function
GenerateForm

Upload file to server .Net

I am using C# .net and I am trying to upload a file to my server. I am not using asp .net.

I followed This Question and it doesn't seem to work for me. On a sidenote, this question was written back in 2008.

I am using this code as shown in the question above:

File.Copy("zQn69PZUx.png", "\\\\198.57.247.152\\~shiro\\public_html\\new");
//tried this also
File.Copy("zQn69PZUx.png", "\\\\198.57.247.152\\~shiro\\new");

The error I get:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The network path was not found.

I also tried using my domain name and use the path like it shows up in the browser, without http:// because it complained about the format, saying its not accepted, like this: domain-name.com\\new, and still got the same error as a result.

Any ideas what I could be doing wrong, or have a better solution on how to upload a file to a server ? You can see here that the http://ift.tt/1O9iw4s path exists.

Stopwatch throws null exception

I am currently trying to create a game of battleship using the kinect sensor in c# WinForms. I have gotten all the basics and an have been trying to implement a class in my project that will convert the skeleton frame data into something that I can use to control the mouse with my had. i have been following a bit of sample code that I have found online, but have run into a big issue with Stopwatches. With the code I have, i am calling

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Coding4Fun.Kinect.Wpf;
using Microsoft.Kinect;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.Windows;

namespace TCP_Server
{
   class motionClass
   {
      //other objects are declared in here as well.
      private readonly Stopwatch _clickHoldingTimer;

and in my error output window, I get the following:

Field 'TCP_Server.motionClass._clickHoldingTimer' is never assigned to, and will always have its default value null

the purpose of the stopwatch is to determine how long a user has made a grabbing motion and preform a mouse click if its been held for a certain amount of time.

if I run the program to the point where my motionClass is initialized, as soon as it sees a skeleton in the frame that is being tracked and runs into the first call for the stopwatch, I get thrown a nullValueException.

why is a stopwatch throwing this exception, and is there any way to fix it? I can provide more code and show where the exception is thrown if needed.

Form does not contain a constructor that takes 1 arguments

I am attempting at filling a combobox with the groupname's and id in another form so I could edit them, however I am getting this UpdatePlayer does not contain constructor that takes 1 arguments and I have been stuck on this for over an hour now. It's getting ridiculous, I have tried {get; set;} and other things but failing. Bare in mind I am trying to learn object orientated programming and have only done it twice, so I am sorry if this is a stupid question.

public class GroupsPlayers
{
    public string GroupID;
    public string GroupName;
}

var smallGroups = new List<GroupsPlayers>();

while(rd.Read())
{
    var currentPlayer = new GroupsPlayers() {
        GroupID = rd["GroupID"],
        GroupName = rd["GroupName"]
    };

    smallGroups.Add(currentPlayer);
}

string msgBoxMsg = string.Format("There are {0} groups with fewer than 3 players. Do you want to add new players to the group?", smallGroups.Count);

MessageBoxResult userResponse = MessageBox.Show(msgBoxMsg, "Groups Manager", MessageBoxButtons.YesNo);

if(userResponse == DialogResult.Yes)
{
    UpdatePlayer frmPlayers = new UpdatePlayer(this, smallGroups);
}