|
I will post code does not work, I will post my code for you to see, what am I wrong with ?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.VirtualMode = false;
this.dataGridView1.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(this.dataGridView1_SortCompare);
dataGridView1.DataSource = CreateTable(5);
}
private static DataTable CreateTable(int RowCount)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("check1", typeof(bool));
tbl.Columns.Add("ID", typeof(int));
tbl.Columns.Add("Name", typeof(string));
tbl.Columns.Add("Number", typeof(int));
tbl.Columns.Add("Date", typeof(DateTime));
tbl.Columns.Add("check2", typeof(bool));
for (int i = 0; i < RowCount; i++)
tbl.Rows.Add(new object[] { false, i, String.Format("Name{0}", i), 3 - i, DateTime.Now.AddDays(i), true });
return tbl;
}
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());
if (e.SortResult == 0 && e.Column.Name != "ID")
{
e.SortResult = System.String.Compare(
dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
}
e.Handled = true;
}
}
image file http://www.mediafire.com/view/9ogb4uwvfr2ag3i/dgv2.jpg/file
|
|
|
|
|
|
all the code I copied from microsoft company, even the top of the page I posted without you reading my first questions, now you give the same link from microsoft company. Before you criticize others, have you tried my example ?
|
|
|
|
|
I posted that link to emphasise the point that you were using the Microsoft code. However, you have still, despite three requests, not explained what your problem is.
[edit]
And to answer your question, yes I have tried the example, and it works fine.
[/edit]
|
|
|
|
|
I found out why the dataGridView1_SortCompare method doesn't allow dataGridView1.DataSource to be assigned if using the sort feature.
|
|
|
|
|
Using the DataContract serialize/deserialize facilities in System.Runtime.Serialization is very convenient, reliable. It does produce verbose xml with rather stunningly long labels like:
</KeyValueOfPersonLikeIntensityvlZcIX2K></Value></KeyValueOfPersonArrayOfKeyValueOfPersonLikeIntensityvlZcIX2Kg_S_SinKSu></ArrayOfKeyValueOfPersonArrayOfKeyValueOfPersonLikeIntensityvlZcIX2Kg_S_SinKSu> That's serialized output from a graph engine I am writing. However, by using GZip you can compress that by factors of #20, or more.
It is interesting to observe that if a Class inherits from a generic structure, like:
[DataContract]
public Class Graph<TVertex, TEdge> : Dictionary<TVertex, Dictionary<TVertex, TEdge>> That will fail to serialize; however, by removing the 'DataContract Attribute, and adding the 'Serializable Attribute, it will serialize properly using a 'DataContractSerializer. This works:
[Serializable]
public Class Graph<TVertex, TEdge> : Dictionary<TVertex, Dictionary<TVertex, TEdge>> And, you can, of course still have the Class as generic, and make an internal field/property of the necessary Type marked with the 'DataMember Attribute:
[DataContract]
public Class Graph<TVertex, TEdge>
{
[DataMember]
public Dictionary<TVertex, Dictionary<TVertex, TEdge>> GraphDict { set; get; }
} I haven't seen any documentation on this, but the error message is what suggested trying to replace 'DataContract with 'Serializable:
System.Runtime.Serialization.InvalidDataContractException
HResult=0x80131500
Message=Type 'Graph_2019_2.PersonLikeGraph' cannot be ISerializable and have DataContractAttribute attribute. So, you be the judge: hack, or useful information ? Anomalous but reliable, or, anomalous and unreliable ?
Note: while the observations I cite are all based on FW 4.8: the behavior described has been encountered with many previous FW versions.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
modified 8-Oct-19 14:17pm.
|
|
|
|
|
BillWoodruff wrote: So, you be the judge: hack, or useful information ? Anomalous but reliable, or, anomalous and unreliable ? Since it is not documented, I'd say such behaviour might change between releases. For MS, anything undocumented is unsupported and "subject to change".
I wouldn't rely on it for anything in production. Does your engine require it, or can you live without?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
I made a "mental bet" with myself that Richard Deeming would respond to this with something relevant
As I read the article you cited, it appears to be talking about inheriting from Interfaces, not generic Type declarations. I don't see those two issues as the same, but, of course, may be wrong.
What interests me is that the Class marked as [Serializable] has its properties adorned with either [DataMember], or, [IgnoreDatamember]: and, the DCS used to serialize is doing the "right thing."
Seems to me the compiler should "see" the Class is marked with [DataMember], and inherits from a generic type declaration ... and throw a build error.
thanks, Bill
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Dear CodeProject members,
Hope you can help with C# that i have been doing since last week.
I'm new C# and compiled this but only getting IPv6
private void Ping_Click(object sender, EventArgs e)
Ping ping = new Ping();
PingReply Reply = ping.send(Hostname,1000);
LW ("IP Address [ + Reply.Address +"]");
LW ("IP Address [ + Reply.Status +"]");
private void LW (string Line = "")
this.Log.Text += Line;
Thank you and much appreciate your help.
|
|
|
|
|
|
If you pass in a host name, the Send method calls Dns.GetHostAddresses[^] and pings the first address returned.
If you want to force it to ping the IPv4 address - assuming there is one - then you'll need to call Dns.GetHostAddresses yourself. You can then find the address you want to ping, and pass that into the Send method instead of the host name.
Eg:
IPAddress address = Dns.GetHostAddresses(Hostname)
.OrderBy(a => a.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)
.FirstOrDefault();
if (address == null) throw new InvalidOperationException($"Cannot resolve host '{Hostname}'.");
PingReply reply = ping.Send(address, 1000);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you all for your information and i finally passed this part of the code thanks to both of you and to Richard for the explanation as it becomes easy to understand when it's explained like that.
Just a small remark that.OrderBy(a => a.AddressFamily == AddressFamily.InterNetwork ? 0 : 1) has to be 2 for IPv4.
Thanks again
modified 9-Oct-19 9:58am.
|
|
|
|
|
mniceguy81 wrote: has to be 2 for IPv4.
No it doesn't; you're simply calculating a value based on whether or not it's an IPv4 address, and sorting on that value. So long as the value returned for an IPv4 address is less than the value returned for other addresses, OrderBy will sort them correctly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard thank you for the feedback and you’re correct, I was just giving feedback on what worked for me.
Thank you and wish you a lovely evening.
How do we close this post as solved as you have helped me with my code.
|
|
|
|
|
|
First of all, that is not a special red line, nor is that a progress bar. That is merely a horizontal line, that you can draw—or even better you can configure a border for the bottom or top control to be shown as a red solid line.
Border Class (System.Windows.Controls) | Microsoft Docs
Secondly, this is a ProgressBar, that control in the picture is not. That control is known as a Slider, that lets your users slide across a range of values.
Quote: What should I install in the visual studio ? That depends on what you already have. Most of the controls are framework-based, such as WinForms or WPF. You need to download the specific framework and then develop the app in that framework.
There are some UI control providers too, such as Telerik, Infragistics, etc. that can help you author the UI using the controls that may not be available in the framework. But this depends on your use case, what framework do you have?
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
I found it. It's control TrackBar and control pannel
|
|
|
|
|
Hi there!
I'm playing around with graphics in C#. I've managed to create a simple application using graphic object to draw some lines and scroling text to a pictureBox. Now I've decided to put my code into a Class (like for further re-use... and mostly for the learning purposes!), but I'm stuck on how to get the graphics back from Class to my pictureBox. The problem (I think!) lies as I'm doing all my drawings in the timer_tick event...
Here's the current code I'm using...
CLASS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;
namespace Osciloscope
{
public class Osciloscope
{
int SCOPE_WIDTH, SCOPE_HEIGHT;
int SCOPE_HORIZONTAL_CENTER, SCOPE_VERTICAL_CENTER;
int SCAN_POINT_X;
int SCAN_POINT_Y;
int SCAN_POINT_X_NEW;
int SCAN_POINT_Y_NEW;
int TIMER_INTERVAL;
Bitmap _Image;
Graphics g;
Pen p;
public Osciloscope(PictureBox picture, bool Run, int Interval)
{
SCOPE_HEIGHT = picture.Height;
SCOPE_WIDTH = picture.Width;
TIMER_INTERVAL = Interval;
SCOPE_HORIZONTAL_CENTER = SCOPE_WIDTH / 2;
SCOPE_VERTICAL_CENTER = SCOPE_HEIGHT / 2;
SCAN_POINT_X = 1;
SCAN_POINT_Y = SCOPE_VERTICAL_CENTER;
_Image= new Bitmap(SCOPE_HEIGHT, SCOPE_WIDTH);
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
if (Run)
{
t.Enabled = true;
t.Interval = TIMER_INTERVAL;
t.Start();
}
else
{
t.Stop();
}
}
void t_Tick(object sender, EventArgs e)
{
g = Graphics.FromImage(_Image);
p = new Pen(Color.Green, 2f);
SCAN_POINT_X_NEW = SCAN_POINT_X + 1;
SCAN_POINT_Y_NEW = SCAN_POINT_Y;
g.DrawLine(p, SCAN_POINT_X, SCAN_POINT_Y, SCAN_POINT_X_NEW, SCAN_POINT_Y_NEW);
p.Dispose();
g.Dispose();
SCAN_POINT_X++;
if(SCAN_POINT_X==SCOPE_WIDTH)
{
SCAN_POINT_X = 1;
}
}
public Image DrawGraph
{
get
{
return _Image;
}
}
}
}
... and the form itself...
namespace Osciloscope
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
Osciloscope Osciloscope;
private void FrmMain_Load(object sender, EventArgs e)
{
Osciloscope = new Osciloscope(Scope, true, 5);
Scope.Image= Osciloscope.DrawGraph;
}
}
}
As I said: I just can't figure out how to get the drawing into the pictureBox... (Scope is the name of the pictureBox!)
Any help woul be highly appreciated!
Regards!
// EDIT: There's no scroling text in current code as I left it out for simplicity...
|
|
|
|
|
The usual way is to implement a drawing method in the class that will draw its content. You can then pass the details of the surface (x,y, width and height etc.) to the method and let it do the work.
|
|
|
|
|
Thanks!
Well, that's the problem I'm having... don't really know how to do it! (As I've mentioned before, I'm a novice trying to learn things the hard way...
I grabbed and sniffed various code samples here on CP and mollested Google for quite some time but to no avail...
Would you be so kind to just point me in the right direction?
Regards!
|
|
|
|
|
Create a method in the class that takes a reference (or maybe more than one) as input parameter. These references tell the class where to draw its contents, so one of the references must be the PictureBox, or whatever control surface you are drawing onto. The method then does exactly what you have already coded in your existing t_tick method. You already have all the code, you just need to move it into the relevant places.
|
|
|
|
|
Thank you 1M times!
I've got it working (well... sort of...). It works BUT it works SLOWLY and it draws only 2/3 of the width of the pictureBox! Any idea?
Here's the code so far...
CLASS:
namespace Osciloscope
{
public class Osciloscope
{
int SCOPE_WIDTH, SCOPE_HEIGHT;
int SCOPE_HORIZONTAL_CENTER, SCOPE_VERTICAL_CENTER;
int SCAN_POINT_X;
int SCAN_POINT_Y;
int SCAN_POINT_X_NEW;
int SCAN_POINT_Y_NEW;
int TIMER_INTERVAL;
Bitmap _Image;
Graphics g;
Pen p;
public Osciloscope(PictureBox picture, bool Run, int Interval)
{
SCOPE_HEIGHT = picture.Height;
SCOPE_WIDTH = picture.Width;
TIMER_INTERVAL = Interval;
SCOPE_HORIZONTAL_CENTER = SCOPE_WIDTH / 2;
SCOPE_VERTICAL_CENTER = SCOPE_HEIGHT / 2;
SCAN_POINT_X = 1;
SCAN_POINT_Y = SCOPE_VERTICAL_CENTER;
_Image = new Bitmap(SCOPE_HEIGHT, SCOPE_WIDTH);
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
if (Run)
{
t.Enabled = true;
t.Interval = TIMER_INTERVAL;
t.Start();
}
else
{
t.Stop();
}
}
void t_Tick(object sender, EventArgs e)
{
DrawGraph();
}
public Bitmap DrawGraph()
{
g = Graphics.FromImage(_Image);
p = new Pen(Color.Green, 1f);
SCAN_POINT_Y_NEW =(int) Math.Sin(SCAN_POINT_X);
g.DrawLine(p, SCAN_POINT_X, SCAN_POINT_Y, SCAN_POINT_X_NEW, SCAN_POINT_Y_NEW);
p.Dispose();
g.Dispose();
SCAN_POINT_X_NEW = SCAN_POINT_X;
SCAN_POINT_X=SCAN_POINT_X+10;
if (SCAN_POINT_X == SCOPE_WIDTH)
{
SCAN_POINT_X = 1;
}
return _Image;
}
}
}
And Form...
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
Osciloscope Osciloscope;
private void FrmMain_Load(object sender, EventArgs e)
{
Osciloscope = new Osciloscope(Scope, true, 1);
this.Text = Scope.Width.ToString();
Scope.Invalidate();
}
private void Scope_Paint(object sender, PaintEventArgs e)
{
Scope.Image = Osciloscope.DrawGraph();
}
}
|
|
|
|
|
You call DrawGraph from your main form and then again from your timer event which is not the way to do it. The DrawGraph method should just draw all the points that belong to the graph. You should probably have a separate method that collects the points based on some external occurrence.
|
|
|
|
|
Yes, I know... silly of me... I was trying to grasp how things work and got it all messed up at the end I've downloaded a sample from (How to create a simple Radar in CSharp using Visual Studi[^]) that demonstrates basic graphic (simple radar).
The original idea was to make things work from separate class - just for the sake of learning how stuff works and how it's done. At the end I wasn't able to get it to work - the problem was that all of the drawings was done in timer and I just couldn't get it to update the pictureBox...
Anyway, thanks for reply!
Regards!
|
|
|
|
|