|
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!
|
|
|
|
|
I am checking the time PM or AM
and then using if (PMorAM == "PM"){string string1 = "";}
if (PMorAM == "AM"){string string1 = "";}
now I want to use string1 in another codes
but I can't call it
when I type string1 it says there "The name 'string1' does not exist in the current context"
|
|
|
|
|
You need to understand variable scope. Since you're defining the variable inside the fi statement, they will only ever exist in that scope. No code outside of that scope will be able to see it.
Wrong:
if (PMorAM == "AM")
{
string string1 = "";
}
public void SomeMethod()
{
string string1 = "";
if (PMorAM == "AM")
{
string1 = "whatever";
}
}
If you want the variable to be visible to all code in the class, you have to define it at the class level.
public class SomeClass
{
private string1 = "";
public void SomeMethod()
{
if (PCorAM == "AM")
{
string1 = "whatever";
}
}
}
|
|
|
|
|
string1.Replace("1", "11");
... didn't work because .Replace "returns" a string; it doesn't "update" one.
string1 = string1.Replace("1", "11");
... would have gotten you further. Same applies to other "value" types like DateTime when it comes to adding days, etc.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
To add to Gerry's answer, the reason you can't just change a string in place is because of something called immutability. When a type is immutable (such as a string), this means that it cannot be changed. Instead, operations on an immutable type return a new instance of that type.
|
|
|
|
|
Is there a way to know if these 2 lines have the same size in pixels aka length? What code should I use to determine if they have the same length or not?
Just because these perpendicular lines created with ASCII, they differ in length size according to whether they were put next to one another or line by line under each other or not.
https://i.stack.imgur.com/gFgPx.png
|
|
|
|
|
Get a tape measure and measure your screen. Or use a screen ruler utility, such as the one included with Microsoft PowerToys[^].
Unless you have complete control over the end-user's system, you have no idea which font they're using for their console, so you have no way of knowing the actual width and height of text that you display on screen.
If you're looking for more advanced control over a command-line GUI, perhaps you should be using a toolkit? For example: Terminal.Gui: Cross Platform Terminal UI toolkit for .NET[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
To add to what Richard has said, most modern fonts are Proportional: older ones (like those used by DOS back in the day) are Non-proportional. In the latter all characters have the same width: 'i' is the same width as 'w':
iiiii
aaaaa
wwwww
In a Proportional font, they aren't:
iiiii
aaaaa
wwwww
And in modern fonts it gets really difficult to work out what the width is going to be, because text display systems also do something called "pair kerning" where they "slide" characters about if they can share space to improve the look of text: an uppercase A can "slide under" an uppercase "W" because the right hand side of each slopes the same way:
AWAWAW
But an uppercase H cannot:
HWHWHW
And then there is the point size, antialiasing, text effects, and the display pixel density, all of which can effect the final result of printing - for that you need the graphics context for the logical device you are printing the string onto! Oh, and two identical systems can be configured differently via a Zoom feature in Windows which makes text bigger or smaller to the user's preference and visual ability!
So to work out the length of a string as printed you need to know all that info, and pass it to the system to work out: there is a method to do that - Graphics.MeasureString Method (System.Drawing) | Microsoft Learn[^] - but be aware that it ... umm ... isn't too accurate under all circumstances. And there is also TextRenderer.MeasureText Method (System.Windows.Forms) | Microsoft Learn[^] which returns different numbers ...
And just to make life more complex, WPF adds its own drawing engine which will differ from both of those.
What I would suggest is to look at why you need to know the relative lengths and work around that, rather than assuming that it's going to be the same for all systems or even all applications - because it isn't!
"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 set Inheritance to disabled and to deny access to domain users on a directory (FullFolder). My code is:-
if (Directory.Exists(FullFolder))
{
DirectoryInfo di = new DirectoryInfo(FullFolder);
DirectorySecurity ds = di.GetAccessControl();
ds.SetAccessRuleProtection(true, true);
di.SetAccessControl(ds);
string DomainUsers = Environment.UserDomainName + @"\Users";
FileSystemAccessRule fsar = new FileSystemAccessRule(DomainUsers, FileSystemRights.FullControl, AccessControlType.Deny);
ds.AddAccessRule(fsar);
di.SetAccessControl(ds);
}
Disabled inheritance works fine.
However the line adding the AccessRule fsar give an error "System.Security.PrincipalNotMappedException"
Is the way I have set the user name wrong?
|
|
|
|
|
Shouldn't the group name be Domain Users rather than just Users ?
Try manually creating the rule on a folder, and then examining the ACL via code to see what the principal name looks like.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, yes looking at the ACE helped me see it.
|
|
|
|
|
I am using rdlc report and report viewer control.When i export the rdlc report to pdf the words written in hindi are not correctly displayed in the pdf.
|
|
|
|
|
Well, there's a secret error somewhere in your secret code then. You should fix that.
Seriously, how do you expect anyone to help you based on that description? The problem could be literally anything. You could be using a font that doesn't support Hindi characters. You could be storing or retrieving the data incorrectly. There could be a problem with the code that's exporting the report. We don't know, because you haven't told us anything other than "it's not working".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
Which is the best way to prevent this from throwing an exception, e.g. if it's null
(bool)(aDataTable.Rows[0]["aColumn"]))
Thanks
|
|
|
|
|
Take a look at Boolean.TryParse Method (System) method. That's what you are looking for.
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[ ^]
|
|
|
|