|
There's no need to add a new one. after setting the data source, the column is already there. I just need to change its type to be checkbox, but couldn't find a way to do that.
|
|
|
|
|
Why not simply define the bound columns at design time instead of autogenerating them?
|
|
|
|
|
I'm having trouble getting a schedule generator working properly. The way I have my program structured, there are 32 teams, all of which are a part of the "league". For the schedule generator, each team gets paired with another team for a game for 16 times (16 weeks). The specific problem is each team's schedule doesn't consist of more than 5 games maximum. (Each team should have 16 games.) In fact, it unusually varies from 1 game to 5 games. for each team's schedule.
I can't figure out what is wrong. Perhaps someone can point out my problem for me.
This is in my Team class:
[code]
private List<Game> games = new List<Game>();
public ReadOnlyCollection<Game> Games { get { return games.AsReadOnly(); } }
public void AssignGame(Game game)
{
games.Add(game);
}
public IEnumerable<Game> GamesWon { get { return games.Where(f => f.Played && f.Winner == this); } }
public IEnumerable<Game> GamesLost { get { return games.Where(f => f.Played && f.Winner != this); } }
[/code]
This is in my league class:
[code]
private const int HOME = 0;
private const int AWAY = 1;
private List<Team> teamList;
public ReadOnlyCollection<Team> Tms { get { return teamList.AsReadOnly(); } }
public int GetRanking(Team team)
{
TeamRankingComparer trc = new TeamRankingComparer();
List<Team> copyOfTeams = teamList.ToList();
copyOfTeams.Sort(trc);
return copyOfTeams.IndexOf(team) + 1;
}
private List<Game> games = new List<Game>();
public ReadOnlyCollection<Game> Games { get { return games.AsReadOnly(); } }
public void Schedule()
{
games.Clear();
Random r = RandomFactory.Create();
int weeks = 16;
for (int i = 0; i <= weeks; i++)
{
foreach (Team team in Teams)
{
List<Team> opponents = new List<Team>();
opponents.AddRange(Teams.Where(t => t != team));
Team opponent = opponents[r.Next(0, opponents.Count)];
if (!games.Any(g => g.Home == team && g.Away == opponent) && !games.Any(g => g.Home == team || g.Away == team))
{
games.Add(new Game(team, opponent));
}
else if (!games.Any(g => g.Home == team || g.Away == team))
{
games.Add(new Game(opponent, team));
}
}
}
}
[/code]
Other classes:
[code]
static class RandomFactory
{
private static Random globalRandom = new Random();
public static Random Create()
{
lock (globalRandom)
{
Random newRandom = new Random(globalRandom.Next());
return newRandom;
}
}
}
public class Game
{
public Game(Team home, Team away)
{
Home = home;
Away = away;
Home.AssignGame(this);
Away.AssignGame(this);
}
public Team Home { get; private set; }
public Team Away { get; private set; }
public Team Winner { get; private set; }
public bool Played { get { return Winner != null; } }
public void OnPlayed(Team winner)
{
Winner = winner;
}
}
internal class TeamRankingComparer : IComparer<Team>
{
public int Compare(Team team1, Team team2)
{
int team1Wins = team1.GamesWon.Count();
int team2Wins = team2.GamesWon.Count();
if (team1Wins > team2Wins)
{
return 1;
}
else if (team2Wins > team1Wins)
{
return -1;
}
else if (team1Wins == 0 && team2Wins == 0)
{
return String.Compare(team1.Name, team2.Name, true);
}
else
{
return 0;
}
}
}
public static class PrintScheduleStatistics
{
public static void PrintTeamStatistics(Team team)
{
foreach (Game g in team.Games)
{
string venue = "Home";
Team opponent = g.Away;
if (g.Home != team)
{
venue = "Away";
opponent = g.Home;
}
Console.WriteLine(String.Format("{0} to {1}", venue, opponent.Name));
}
}
}
[/code]
This call is in my Main() method:
[code]
League league = new League();
LoadRosters(league);
league.Schedule();
PrintScheduleStatistics.PrintTeamStatistics(league.Teams[1]);
[/code]
|
|
|
|
|
Why does your RandomFactory.Create work the way it does?
Surely you realize that it won't increase the entropy of your random numbers
I can't think of anything else it might be expected to accomplish, but you might have a perfectly legitimate reason to "restart" your PRNG with a seed that is a value from the old PRNG
|
|
|
|
|
Well it's no wonder is it?
For each team in the league, you loop through the 16 weeks.
For each week, you draw an opponent at random.
If there are no games (team vs opponent) in the league
AND no games where the team is either the home team or the away team
=> create (team vs opponent) game
if (!games.Any(g => g.Home == team && g.Away == opponent) && !games.Any(g => g.Home == team || g.Away == team))
..
else if (!games.Any(g => g.Home == team || g.Away == team))
First, you clearly didn't mean what your code says - that a game should only be created if there are no games where the current team plays either at home or away. Second, you've forgotten that if your draw results in a game that already exists you can't just move on to the next week without creating a game!
I'm not sure I've understood exactly what you *want* to happen, but if you want to generate a normal leage where all teams play all the other teams once at home and once away, why not just create them systematically using a nested loop and randomize the schedule afterwards?
Or, if it's not a normal league but more of a sort of cup, why not do like in a real draw and remove the teams that have been drawn so you can't draw the same combination again?
|
|
|
|
|
Hello.
How to do, so when i click a button, you can choose a place, where the richboxtext shall load from?
It's a kind of a little texteditor
Ahlmo
|
|
|
|
|
Have a look at OpenFileDialog class, and study some of the articles here at CodeProject. Just search for editor, or RichTextBox.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
What?
I dont understand what do u mean?
Can you help me plx
|
|
|
|
|
|
Which books are good for this kind of coding?
|
|
|
|
|
Sorry, I seldom recommend books as their value very much depends on someones background and prior knowledge;
I tend to like the Microsoft Step by Step series; e.g. "MicroSoft Visual C#.NET Step by Step" by John Sharp and Jon Jagger.
What I do recommend is described here[^].
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Ok, but can u give me an example plz Can't get my to work :S
|
|
|
|
|
You should be more concerned with learning programming than with getting a program to work, simply because the other way around is an unnatural order to proceed in, and thus a very difficult way to go about it. If you don't want to learn programming there are probably plenty of free programs on the market that do all you want and more.
That said, this is all there is to showing a dialog and reading the contents of a text file:
string readTextFile()
{
var dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK) return null;
using (StreamReader sr = File.OpenRead(dlg.FileName))
return sr.ReadToEnd();
}
This is a simplistic implementation. Normally you'd want to add the dialog as a component to your form, just because that will let you set filters or other properties using the designer rather than coding, and use that dialog in the method. If you were to support huge files you also might not want to read the entire file since the user won't be able to view more than a tiny bit of it at a time and it's kind of wasteful to keep it all in memory (and may not be possible to keep it all in the textbox control at once). But given that you couldn't figure out how to read a file even with links to articles about the subject I think you should just forget about large files.
Hopefully you can figure out how to assign the Text property on your own.
|
|
|
|
|
hi
How to print (and preview) any document - but not with CrystalReport
(working on VS2008 - C#)
ex. i have 6 val's in my C# program that i want to print (and preview)
how i can do it ?
thank's in advance
|
|
|
|
|
Printing is a rather complex matter. Printing a Form is based on the Paint handler and the PrintDocument class. My Sokoban article offers a simple example.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
your project not open on my VS2008
is there any sample code for this ?
|
|
|
|
|
It probably is a VS2005 project, which VS2008 can convert.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
E_Gold wrote: How to print (and preview) any document
And while on the subject of magic: How to do anything and everything automatically?
Hint: Any document implies a system that can intelligently reverse-engineer any format presented to it, past, present or future, as well as one that can break encryption that is considered unbreakable. In short, you are asking for nothing less than magic.
|
|
|
|
|
it's 00:30 and I lost my mind in a few beers, came home and would do some coding before I hit the showers, but now I forgot an important part...
I remember how to add values and text to a combobox via a datasource, but can any remember how to add both text and value in a static way?
ie.
Combobox1.Items.Add(new ????("text","value"));
yes, yes I know, don't code drunk.
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:59am.
|
|
|
|
|
nah can't really use that solution, then I wouldnt be able to use SelectedValue to get the current selected value and would have to rewrite too much code as I then couldnt load the combobox values elsewhere.
but thanks for the effort.
|
|
|
|
|
Why don't you create a static datatable with your static data??
static DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
dt.Columns.Add("MyTextToShow");
dt.Columns.Add("MyValueToShow");
dt.Rows.Add("OneText", 1);
dt.Rows.Add("TwoText", 2);
dt.Rows.Add("ThreeText", 3);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "MyValueToShow";
comboBox1.DisplayMember = "MyTextToShow";
}
regards... and don't code drunk! is hateful haha
|
|
|
|
|
hi there
i am writing a program for video conference over internet i was looking for a sample for capturing a image from webcam i found that there is two way for doing this first is using WIA and second is using API i found a sample that use API (avicap32.dll) it work fine now i want to add another camera or webcam and the aouther is not accessible .i reviewed codes several times but i cant understand that. it is really complicated for me and as i cant understand the code i cant extend it for capturing image or video from any other devices synchronouslly here is the code and i would be really appriciated if you explan about the codes :
the class for capturing and sending image:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WebCam_Capture
{
[System.Drawing.ToolboxBitmap(typeof(WebCamCapture), "CAMERA.ICO")]
[Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(System.ComponentModel.Design.IDesigner))]
public class WebCamCapture : System.Windows.Forms.UserControl
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
private int m_TimeToCapture_milliseconds = 100;
private int m_Width = 320;
private int m_Height = 240;
private int mCapHwnd;
private ulong m_FrameNumber = 0;
private WebCam_Capture.WebcamEventArgs x = new WebCam_Capture.WebcamEventArgs();
private IDataObject tempObj;
private System.Drawing.Image tempImg;
private bool bStopped = true;
public delegate void WebCamEventHandler (object source, WebCam_Capture.WebcamEventArgs e);
public event WebCamEventHandler ImageCaptured;
#region API Declarations
[DllImport("user32", EntryPoint="SendMessage")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("avicap32.dll", EntryPoint="capCreateCaptureWindowA")]
public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
[DllImport("user32", EntryPoint="OpenClipboard")]
public static extern int OpenClipboard(int hWnd);
[DllImport("user32", EntryPoint="EmptyClipboard")]
public static extern int EmptyClipboard();
[DllImport("user32", EntryPoint="CloseClipboard")]
public static extern int CloseClipboard();
[DllImport("avicap32.dll", EntryPoint = "capgetdeviiceproperties")]
public static extern int capgetdeviiceproperties(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
#endregion
#region API Constants
public const int WM_USER = 1024;
public const int WM_CAP_CONNECT = 1034;
public const int WM_CAP_DISCONNECT = 1035;
public const int WM_CAP_GET_FRAME = 1084;
public const int WM_CAP_COPY = 1054;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41;
public const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42;
public const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43;
public const int WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44;
public const int WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45;
public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46;
public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
#endregion
#region NOTES
#endregion
public WebCamCapture()
{
InitializeComponent();
}
~WebCamCapture()
{
this.Stop();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.Name = "WebCamCapture";
this.Size = new System.Drawing.Size(342, 252);
}
#endregion
#region Control Properties
public int TimeToCapture_milliseconds
{
get
{ return m_TimeToCapture_milliseconds; }
set
{ m_TimeToCapture_milliseconds = value; }
}
public int CaptureHeight
{
get
{ return m_Height; }
set
{ m_Height = value; }
}
public int CaptureWidth
{
get
{ return m_Width; }
set
{ m_Width = value; }
}
public ulong FrameNumber
{
get
{ return m_FrameNumber; }
set
{ m_FrameNumber = value; }
}
#endregion
#region Start and Stop Capture Functions
public void Start(ulong FrameNum)
{
try
{
this.Stop();
mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, this.Handle.ToInt32(), 0);
Application.DoEvents();
SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0);
m_FrameNumber = FrameNum;
this.timer1.Interval = m_TimeToCapture_milliseconds;
bStopped = false;
this.timer1.Start();
}
catch (Exception excep)
{
MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + excep.Message);
this.Stop();
}
}
public void Stop()
{
try
{
bStopped = true;
this.timer1.Stop();
Application.DoEvents();
SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0);
}
catch (Exception excep)
{
}
}
#endregion
#region Video Capture Code
private void timer1_Tick(object sender, System.EventArgs e)
{
try
{
this.timer1.Stop();
SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);
SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
if (ImageCaptured != null)
{
tempObj = Clipboard.GetDataObject();
tempImg = (System.Drawing.Bitmap) tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
x.WebCamImage = tempImg.GetThumbnailImage(m_Width, m_Height, null, System.IntPtr.Zero);
this.ImageCaptured(this, x);
}
Application.DoEvents();
if (! bStopped)
this.timer1.Start();
}
catch (Exception excep)
{
MessageBox.Show("An error ocurred while capturing the video image. The video capture will now be terminated.\r\n\n" + excep.Message);
this.Stop();
}
}
#endregion
}
}
thanks in advance
modified on Saturday, November 21, 2009 1:52 PM
|
|
|
|
|
Firstly you should put your code between <pre></pre> tags so it retains its formatting. The above code is very difficult to read, please edit the entry.
Secondly if you do not understand the code then you really should not be trying to use it. The most important aspect of any application is that you understand the code you are using. This forum does not have the space (and contributors do not have the time) to provide tutorials in this fashion. If this code came from an internet article then you should spend time studying the article until you understand it.
|
|
|
|
|
Whats the betting he just puts <pre>...</pre>tags around the existing unformated code?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
"Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
|
|
|
|
|