|
Hello,
I got it working by using the read and try command.
And this in a loop that I can put later into a thread:
byte[] buffer = new byte[3];
Stream mStream = FormManager.bluetoothClient.GetStream();
while (FormManager.bluetoothClient.Connected)
{
try
{
mStream.Read(buffer, 0, buffer.Length);
System.Diagnostics.Debug.WriteLine("read status "+buffer[0] + " " + buffer[1] + " " + buffer[2] + " --> Status " + FinalResultBT);
}
catch
{
System.Diagnostics.Debug.WriteLine("read status fail ");
}
}
|
|
|
|
|
I'm using a typical 'n-tier' design. In most cases the BL just passes through called to and from the UI and DAL. But I do have some business rules that I'd like to check in the BL. I want to put the code there, versus in the UI because I have a WPF app and a Mobile app both calling the Web API.
So, the question is, how would you handle a rule violation in the BL? Would you throw an exception? Or return a string or other value?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
Richard Deeming wrote: Return an error status code
Thanks. I was leaning in that direction but wanted to get opinions.
I also posted a question in Arch & Design. I would welcome your thouhts on that.
K
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
HI im new to C#
im creating an test app to connect to a local Database, i have created a form which connects to my DB and server, once connected this opens a new form. So far so good. what i need to know now is how do i use the connection in the new form?
thanks
|
|
|
|
|
Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
MyForm mf = new MyForm();
mf.Show(); Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
But ... you shouldn't create a SqlConnection and "pass it around" your app - that's a bad idea as some operations such as a DataReader that fails to be closed and disposed properly can "block" the connection object from doing anything else. Instead, create a connection string and pass that around (or create it as a public static property and just use it everywhere). Then create your Connection within a using block each time you need one.
"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!
|
|
|
|
|
Thanks for your reply i will take look at the parent child
this is my first app, advancing from hello world . passing the connection string between forms is what i am trying to do. I am a complete Novice.
thanks
|
|
|
|
|
You don't have to "pass" anything. The connection string can be made available to any methods that want it by exposing it as a getter-only property in a static class.
Seriously, if you're moving up from Hello World to this, you may have taken your steps a bit too long.
|
|
|
|
|
If you're creating a connection to the database in your login form to share amongst all other forms, DON'T.
All database operations should be Create Connection as late as possible, do your work, and close connection as early as possible.
DO NOT HOLD A DATABASE CONNECTION OPEN FOR THE LIFE OF YOUR APP.
Also, is a login form really the correct place to open a database connection used by the entire app? Does it have anything to do with the operation of authenticating users in your app?
No, it does not. That is a violation of "separation of concerns". The authentication form should be limited to just that, getting the user credentials and authenticating them. That's it. Nothing else.
The database work should be handled different classes specific to their tasks.
|
|
|
|
|
I Parent, Owner
let me clear something up here:
1) when you declare and instantiate a Form within another Form: they do not have a Parent-Child relationship. It is simply the case at run-time that the instance of the Form that creates the second Form has a valid reference to the instance of the second Form.
2) imho, it's regrettable that it's legal to set a Form to be the Parent of another Form: that leads to a form-within-a-form which is a code smell: you can use the light-weight ContainerControls, like Panel, instead.
3) you can set a Form to be the Owner of another Form ... imho, that is a kind of Parent-Child relationship ... but, all it does is make sure the owned Form appears in front of the owning Form at run-time.
.
II Security in getting and passing data from one Form to another
0) the goal here is to implement security by encapsulation/separation of concerns: all objects/delegates created in the main form Load event do not persist outside that method. the log in form has no "world knowledge" of the main form. there is only one point of access to the log in form in the main form: being able to set the Action delegate 'SendResult in the log in form.
just for the sake of over-kill, we inject the executable code using the lambda format. for a good summary of using Action and Func rather than delegates: [^]
1) code example in a Main Form named Form1's Load EventHandler:
private void Form1_Load(object sender, EventArgs e)
{
LogInForm loginfrm = new LogInForm();
string username, password;
loginfrm.SendResult = (uname, pword) =>
{
username = uname;
password = pword;
};
DialogResult loginResult = loginfrm.ShowDialog();
if (loginResult == DialogResult.OK)
{
}
else if (loginResult == DialogResult.Cancel)
{
}
else if (loginResult == DialogResult.Abort)
{
}
} 2) code in the LogInForm
using System;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class LogInForm : Form
{
public LogInForm()
{
InitializeComponent();
}
public Action<string, string> SendResult;
private void btnLoginAttempt_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
if (SendResult != null)
{
SendResult(textBox1.Text, textBox2.Text);
this.DialogResult = DialogResult.OK;
}
else
{
this.DialogResult = DialogResult.Abort;
}
}
else
{
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
} An alternative use of the Delegate/Func: unless the user's log-in data is validated, the log-in form stays open. In real=world code, ypu'd want to keep track of the attempts, and abort the log in after so many attempts;
// in the Main Form
private void Form1_Load(object sender, EventArgs e)
{
LogInForm loginfrm = new LogInForm();
string username, password;
loginfrm.SendResult = SendResult;
DialogResult loginResult = loginfrm.ShowDialog();
if (loginResult == DialogResult.OK)
{
}
else if (loginResult == DialogResult.Cancel)
{
}
else if (loginResult == DialogResult.Abort)
{
}
}
private bool SendResult(string uname, string pword)
{
username = uname;
password - pword;
return false;
} the revised log-in form:
using System;
using System.Windows.Forms;
namespace YourNameSpaace
{
public partial class LogInForm : Form
{
public LogInForm()
{
InitializeComponent();
}
public Func<string, string, bool> SendResult;
private void btnLoginAttempt_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
if (SendResult != null)
{
if (SendResult(textBox1.Text, textBox2.Text))
{
this.DialogResult = DialogResult.OK;
}
}
else
{
this.DialogResult = DialogResult.Abort;
}
}
else
{
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
«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 3-Nov-20 21:08pm.
|
|
|
|
|
Thanks, i will have a good read and give that a bash 
|
|
|
|
|
I will add an alternate example where the 'SendResult Func/Delegate is triggered within the log-in form, and a more standard EventHandler declaration is used in the main form: this may be clearer.
«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
|
|
|
|
|
Im very new to this and i usually search the web for the answer but i got stucked and i need help from community.
I have two datagridview on different tables and i wanna show them on a same crystal report.
Iv done it with one of em using XML Schema but what to do with other datagridview.
TY in advance
|
|
|
|
|
2 reports right after one another is the same as 1 report. Or combine the 2 grids if you want "2 up" (and can't figure out how to put 2 grid controls on one row).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hello,
Can anybody give me any guidance on how to create an if else statement on a background colour of a label.
For example, if the background colour of a label is white then do this else do this.
|
|
|
|
|
if (backcolor == "white")
{
}
else
{
}
Although you may wish to clarify your question with some more details of exactly what your code is trying to do.
|
|
|
|
|
Hello
thanks for such a quick reply and help, I have a mouse leave event on label 28. where once you leave the label it changes white.
then when you finish the maze at finish_point i want it to check if the label28 background colour is white. if it is then do the below close between the {} else bring up a message box stating you haven't found the treasure/ loot.
private void Finish_Point(object sender, EventArgs e)
{
this.Close();
th = new Thread(opennewform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
|
|
|
|
|
Don't rely on colours - instead create a class level bool that tells you "the user left the label" instead. It's a lot more readable (and thus maintainable) and less prone to error later when you change things around.
"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!
|
|
|
|
|
if (myLabel.BackColor == Color.White)
{
... do something
}
else
{
... do something else
} Be aware though both that a color test is specific: a tiny change in shade will fail the test, and that you can't assume any colour scheme on the part of the user: if you check for White, and the user prefers a Dark scheme it will fail, while SystemColors.Control may match.
"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!
|
|
|
|
|
Brilliant, Thank you this has worked.
Great help
|
|
|
|
|
Hello
I have a maze game with 3 forms/ levels.
When hit certian labels it will take you to another level by closing the current form and then opening the specified form.
I am now wanting to add some sort of treasure or scoring system that you need to collect and then the game keep a track of your score.
Is there anybody who can help me with this? I can supply you with code if you wish to look at the application.
Thanks
|
|
|
|
|
We can't really help you with any specifics: we have no idea how your code works - and I for one don't want to wade through a pile of code trying to work out what part does what and why!
The way I'd do it is different: I wouldn't close the form and reopen a new one, I'd reuse the current form by clearing the play area and loading a new one from a file, using the same method I used when I first loaded a form - it's a lot more flexible that way than by designing a "static form" as a maze each time!
Having said that, I'd have a GameObject abstract base class, and derive Wall, Door, Player, Monster, Treasure, and ExitLevel classes from that. Your game board then becomes an array of GameObject instances, and the base class ensures that the derived classes have Move, Open, Attack, and so forth methods which they can handle. The Wall and Door classes for example refuse to move in any direction, but the Player can move provided nothing blocks his way. Doors and Chests can Open, Monsters can Attack and be Attacked and so forth.
How does yours work? Please, don't say "a button for each space on the board" because I'll get quite depressed ...
"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!
|
|
|
|
|
Some people use "story boards" to help with their design. You need to decide what to do with this "treasure", etc.
How to Storyboard Your Game - Gamescrye Blog
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi,
I have a DataGridView, a save button, and a load button. I have seen in some tutorial that it is possible to export DataGridView content into Excel or *.txt file. But I want to save my contents into an unknown file which can only be read by my own App. How is it possible? Is there any tutorial for this purpose (I use C#)? 
|
|
|
|
|