|
Did you have a question or a problem with your code?
We're not here to do your homework for you.
|
|
|
|
|
Two things:
1) This is a C# forum, not C++. The two languages look similar, but are very, very different.
2) While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Just posting your assignment isn't going to get you anywhere.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am trying to run a process which is invoked from either a C# or C++ program. The process has its standard output and standard input redirected to the program which invoked it. In this way, the calling program can control the child process programmatically. (The child process happens to be the MAME arcade emulator so I can run commands through the Lua console.)
Things look about how I would expect them from the C++ program. I can read the output from the child process with the PeekNamedPipe() function. The characters are identical to what I see when I just run the process from the command line and observe the console.
But when I run a similar C# program, the console output is not quite right. There are at least two issues. Using the Streamread read() method (the child process's stdout is directed to a stream), there are always about 30 "junk" characters that are read back before I see the characters I expect. Additionally, it appears that the characters at 16-bits (not 8-bit chars). (I can live with this, and I think I can convert them into 8-bit chars if I need to.)
I have a strong suspicion that these characters are actually some low-level "under the hood" info about the string itself, like some kind of reflection data - maybe details about how it's encoded, length, etc.
I just can't seems to figure out how to cleanly extract the "real" data out of the stream using C#.
Can anyone make sense of this and give a hint of how I can get ASCII character data from the child process in C#?
Thanks!
|
|
|
|
|
C# uses Unicode - and the console it writes to is also Unicode by default.
You may get better results using the Console.OutputEncoding Property[^] set to Encoding.ASCII , but I've never needed to try myself.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have tried the suggestions found here and elsewhere but still no joy!
DataTable dataTable = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dataTable.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataRow drow = dataTable.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
drow[cell.OwningColumn.Name] = cell.Value;
}
dataTable.ImportRow(drow);
dataTable.AcceptChanges();
}
modified 23-Oct-22 6:13am.
|
|
|
|
|
If I use your code, but replace the ImportRow with your commented out Rows.Add code, it works fine for me:
private void MyOtherButton_Click(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
foreach (DataGridViewColumn col in myDataGridView.Columns)
{
dataTable.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in myDataGridView.Rows)
{
DataRow drow = dataTable.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
drow[cell.OwningColumn.Name] = cell.Value;
}
dataTable.Rows.Add(drow);
dataTable.AcceptChanges();
}
MyOtherDataGridView.DataSource = dataTable;
}
I get identical DGV's side by side ...
So what am I doing that you aren't?
And why are you doing this ass backwards? Normally you use the DataGridView.DataSource to populate data, not the actual rows and columns ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Please forgive me for being a bit slow. I am 70 years old and have just started getting back to programming because I can't do much else these days.
My aim is to be able to save the DataGridview data to a spreadsheet using EPPlus.
This is the code to get the data into the spreadsheet from a data table.
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
pck.Save();
}
I do not have any code to populate the spreadsheet directly from the DataGridView.
If you could show me how to hook up a DataTable to get the data from a DataGridView using a Datasource or another method I would be very thankful.
|
|
|
|
|
Start by looking at where you get the data into the DGV: you read it from somewhere into something, and add that to the DGV somehow - there are a number of ways so I can't tell you "do this and it'll work".
When you have that, let us know and we'll see where to go from that.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am getting the data from SQL server express and then saving it as an xlsx file.
using the following code that works well.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
DataTable dataTable = new DataTable();
BindingSource SBind = new BindingSource();
if (openFileDialog1.FileName != null)
{
ExcelEPP workbook = new ExcelEPP();
dataTable = workbook.ReadFromExcel(openFileDialog1.FileName);
}
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
SBind.DataSource = dataTable;
dataGridView1.DataSource = SBind;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
}
|
|
|
|
|
So you already have your data in a DataTable - which you read from Excel, not Sql Express - you then use that to source data to a BindingSource, which you then use as the DataGridView data source.
Why not just follow the links back to the original DataTable?
if (myDGV.DataSource is BindingSource bs)
{
if (bs.DataSource is DataTable dt)
{
... use the Datatable in dt here ...
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
modified 23-Oct-22 12:00pm.
|
|
|
|
|
Thank you for that bit of code as it does allow the saving of the original data table that was loaded from the file.
It, unfortunately, does not include any edits made to the DataGriview after the load.
What am I missing to update the DataTable so that it includes the changes made to the DataGridView?
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Excel Worksheets (*.xlsx)|*.xlsx|xls file (*.xls)|*.xls|All files (*.*)|*.*";
saveFileDialog1.FileName = "";
if (dataGridView1.DataSource is BindingSource SBind)
{
if (SBind.DataSource is DataTable dataTable)
{
using (ExcelPackage package = new ExcelPackage())
{
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
try
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("Countries");
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
package.SaveAs(new FileInfo(saveFileDialog1.FileName));
}
catch (Exception)
{
DialogResult reminder = MessageBox.Show("Cannot save file, file opened in another program.\nClose it first! ", "Save Failed", MessageBoxButtons.OK);
}
}
}
}
}
}
|
|
|
|
|
I just Needed to Refresh the DataGridview.
Thanks for your help, I coded for forty years and have forgotten most of it but if I keep at it I hope it will come back.
|
|
|
|
|
You're welcome!
Can I suggest that a book might help? There is a lot of stuff in C# that can be really helpful - but if you don't even know it exists, you can't use it! A book will cover the language and most of the framework so you get a good idea what is available even if you can't remember how to use it right at this moment?
For example little bits like declaring a local variable tb in a test:
if (sender is TextBox tb)
{
... Instead of the older style
TextBox tb = null;
if (sender is TextBox)
{
tb = (TextBox) sender;
... Or
TextBox tb = sender as TextBox;
if (tb != null)
{
...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Having a problem with one line of code to save only a DataGrid's Rows that have been Multi-selected.
The code works correctly to save all the rows to file, however code line 04 is incorrectly formatted
to save only those rows which have been multi-selected. Appreciate any help. With thanks.
// Export DataGridView Selected Rows to Text File
TextWriter writer = new StreamWriter(FilePathName);
if (dataGridView1.SelectedRows.Count != 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
for (int i = dataGridView1.Rows(row.Index); i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (j == dataGridView1.Columns.Count - 1)
{
writer.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString());
}
else
writer.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
writer.WriteLine("");
}
}
writer.Close();
modified 23-Oct-22 0:09am.
|
|
|
|
|
Why do you even have line 4?
Take a look at your code again. Line 2 iterates over the SelectedRows collection, using the variable "row". now, where do you use that variable in the rest of your code? Hint: You don't. You're completely ignoring each of the SelectedRows. You go right back to looking at every row and column in the DGV.
Get rid of line 4, and rewrite the rest of the code to use the "row" variable instead. You don't need to start dereferencing with "dataGridView1.Rows[]". You can just use "row.Cells[j]..."
|
|
|
|
|
Thanks Dave. Appreciate your insight. My copy paste was incorrect, as this is the first time on the forum. Have updated what I trying to achieve.
All good and with thanks Dave.
modified 23-Oct-22 0:21am.
|
|
|
|
|
OK, the new code you posted is no different than the first version. You're doing the exact same thing and need to fix it the exact same way I already mentioned.
|
|
|
|
|
Took your suggestion the first time. Hopefully this will assist others. Cheers.
TextWriter writer = new StreamWriter(FilePathName);
if (dataGridView1.SelectedRows.Count != 0)
{
try
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows.Cast<DataGridViewRow>().Reverse())
{
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (j == dataGridView1.Columns.Count - 1)
{
writer.Write("\t" + row.Cells[j].Value.ToString());
}
else
writer.Write("\t" + row.Cells[j].Value.ToString() + "\t" + "|");
}
writer.WriteLine("");
}
}
writer.Close();
}
catch
{ }
}
|
|
|
|
|
Hi, please help my. I am try 2 days to solve main problem. Arduino device send jb_ready and this IF function dont work:
if (jb_serial_read == "jb_ready")
{
MessageBox.Show("it work");
}
What i am want:
I am want control computer with arduino device. Because arduino device helo peoples with musculary dystrofi control computer. For control computer need working IF function.
C# code:
using System;
using System.IO.Ports;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace kp71
{
internal class Ai
{
private string jb_serial_read;
private SerialPort Jb_serial;
public void Start_comunication_with_jbhid()
{
Jb_serial = new SerialPort("COM3", 9600)
{
ReadTimeout = 1000,
WriteTimeout = 1000,
Encoding = Encoding.GetEncoding("iso-8859-1")
};
Jb_serial.DataReceived += Jb_serial_DataReceived;
Jb_serial.Open();
}
public void Help_enable()
{
if (jb_serial_read == "ok")
{
MessageBox.Show("it work");
}
}
private void Jb_serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Task jb_hid = new Task(() =>
{
try
{
SerialPort sp = (SerialPort)sender;
jb_serial_read = Jb_serial.ReadExisting();
}
catch (Exception Error)
{
}
});
jb_hid.Start();
}
private void Send_data(string jb_serial_write)
{
Jb_serial.WriteLine(jb_serial_write);
}
}
}
Arduino code:
#include <SoftwareSerial.h>
#include <EEPROM.h>
SoftwareSerial mySerial(10, 11);
#define cts 6
#define Watter_pump 7
#define Food_pump 8
#define Alarm 9
#define EMG A7;
String Command;
String Value;
String cmd;
void setup() {
pinMode(Alarm, OUTPUT);
pinMode(Food_pump, OUTPUT);
pinMode(Watter_pump, OUTPUT);
pinMode(cts, INPUT);
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("jbHID ready...");
}
void loop() {
get_cmd();
Serial.println("jb_ready");
delay(900);
}
|
|
|
|
|
Data receive events occur when a character arrives at the coms buffer, not when a message is complete.
So when you call ReadExisting you will get a string containing all the characters received since the last call - and with modern processors being fast and serial coms being slow (9600 baud means a fastest data transfer rate of 9600 bits per second, which translates to around 960 characters per second at best) the most likely result is that each time it's called you will get one and only ever one character at a time.
And since your code overwrites whatever was in the jb_serial_read string each time the event happens, that means that you will never see the complete message. You need to buffer data, spot teh end of data, and feed it back up the chain as a message instead of character by characters.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hey all!
Hope everything is fine.
I try to geht values of an optical encoder, sent via arduino by serial port to my C# Program...
My problem is, often I got salad .. The EventHandler is firing in the middle of a message and I get many wrong values.
this is my event handler:
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
string indata = port.ReadExisting();
TextBoxUpdate(indata);
}
whats the best option to send (i am very flexible in what i am sending on the serial port)
and whats a good and performing method to receive it without having salad for breakfest?
salad means.. only some chars, sometimes also charts from the next message...
Has anybody already done sth like this?
Thank you!
EDIT: Found it! I used readline command, then it was working more well!
modified 21-Oct-22 8:12am.
|
|
|
|
|
The snake head ```
<pre lang="C#"> 0``` does not move anywhere when ```
Console.ReadKey() ``` happens.
Here is the full code:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleSnakeGame_ConsoleApp
{
internal class Program
{
public bool gameOver = true;
public int width = 20;
public int height = 20;
public int x, y;
public int fruitX, fruitY;
public int score;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
static void Main(string[] args)
{
Program oyun = new Program();
oyun.Setup();
oyun.Draw();
oyun.Input();
oyun.Logic();
Console.ReadLine();
}
public void Setup()
{
gameOver = false;
string a = "!!!!! SIMPLE SNAKE GAME !!!!!";
Console.WriteLine(gameOver.ToString() + " " + a, "{0}" + "{1}");
dir = eDirection.STOP;
x = width / 2;
y = height / 2;
Random rnd = new Random();
fruitX = rnd.Next(1, 19);
fruitY = rnd.Next(1, 19);
score = 0;
}
void Draw()
{
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
if (i == y && j == x)
{
Console.Write("0");
}
else if (i == fruitY && j == fruitX)
{
Console.Write("F");
}
else if (j > 0 && j < height - 1 && i > 0 && i < width - 1)
{
Console.Write(" ");
}
else
{
Console.Write("#");
}
}
Console.WriteLine();
}
Console.WriteLine();
}
void Input()
{
ConsoleKey key;
key = Console.ReadKey(true).Key;
if (key == ConsoleKey.A)
{
dir = eDirection.LEFT;
}
else if (key == ConsoleKey.D)
{
dir = eDirection.RIGHT;
}
else if (key == ConsoleKey.W)
{
dir = eDirection.UP;
}
else if (key == ConsoleKey.S)
{
dir = eDirection.DOWN;
}
else if (key == ConsoleKey.X)
{
gameOver=true;
}
}
void Logic()
{
switch (dir)
{
case eDirection.LEFT:
x--;
break;
case eDirection.RIGHT:
x++;
break;
case eDirection.UP:
y--;
break;
case eDirection.DOWN:
y++;
break;
default:
break;
}
}
}
}
```
I guess the problem is ```
Console.ReadKey() ``` function here:
```
void Input()
{
ConsoleKey key;
key = Console.ReadKey(true).Key;
if (key == ConsoleKey.A)
{
dir = eDirection.LEFT;
}
else if (key == ConsoleKey.D)
{
dir = eDirection.RIGHT;
}
else if (key == ConsoleKey.W)
{
dir = eDirection.UP;
}
else if (key == ConsoleKey.S)
{
dir = eDirection.DOWN;
}
else if (key == ConsoleKey.X)
{
gameOver=true;
}
}
```
However I do not know what to replace ```
<pre lang="C#"> Console.ReadKey()``` with and how to do it.
|
|
|
|
|
At a quick glance, your program reads a key but does not redraw the image. After a key is read (your Input() routine), you call your Logic() routine and then exit. I guess that what you wanted to do was to loop and redo the Draw(), Input(), Logic() steps until the end of the game. Suggestion: Encapsulate those steps in a do ... while loop.
|
|
|
|
|
simply I want to call a string from if statement
like this one
if (PMorAM == "PM")
{
string string1 = "11"
string strint2 = "22"
}
I tried making a public string and replacing it like this
public string string1 = "1"
if (PMorAM == "PM")
{
string1.Replace("1", "11");
} but it didn't work
is there's any way to do this ?
|
|
|
|
|
That makes absolutely no sense whatsoever to anyone other than you - because we only get exactly what you type to work with.
You can't "call" a string, and your code examples make no real sense at all.
Take a step back, think about your assignment, read it really carefully, and then ask yourself this: "what do I need to tell people about my project to get help with it?" because without that, we can't actually help you...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|