|
Hi, how am I suppose to implement it with my code.
i have code that is doing almost everything I want, only not printing the contents from the text.Box. The strange thing is, when I use a RichTextBox then everything gets printed into the .csv file I am trying to create.
Were you thinking about something like this?
private void Logging()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew | FileMode.Append))
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine(textSampling.Text);
}
}
|
|
|
|
|
I already explained, that if you use FileMode.CreateNew you will be creating a new file every time you try to write a log entry. You only need to use that once, at the beginning of a session perhaps*, and use FileMode.Append for every subsequent call.
* You need to use some sort of file name cycling in your programs so you do not overwrite the previously created files. A common method of doing this is to create files with the date as part of the filename.
|
|
|
|
|
For "simple" logging, all you need is:
1) File.AppendAllText, or
2) File.AppendAllLines
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Itried that but it didnt work
|
|
|
|
|
What is "it"?
If it didn't work, you did "it" wrong.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Yes you are completely correct . I did something very WRONG.
Holy Sh*t , I was writing from wrong text box the whole time.
My initial code actually works;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName,FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine(textSensorValues.Text);
}
}
|
|
|
|
|
Been there, done that.
One "relaxes" AFTER the problem is solved; not before.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
<label class="col-sm-2 control-label">Vehical Mileage<span style="color:red">*</span></label>
<div class="col-sm-10">
<input type="text" name="mileage" class="form-control" required>
$query->bindParam(':mileage',$mileage,PDO::PARAM_STR);
the above is a code im using to insert data into the mileage row, im creating a car rental system, i was wonder if anyone could help me with a code that can relate to the mileage.
say the mileage is about to reach 1000,
when the mileage reaches one thousand the status should change to IN SERVICE.
|
|
|
|
|
Sounds like "if this, then that". Just "bind" to something that says "IN SERVICE" or (not) in service, or whatever. Even "blank" is an option. Or "hide" the element ... jeez.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
That doesn't look anything like C#.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
So why have you posted it in the C# forum?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm trying to make a ToDo app but I'm stuck. I did a UserControl named Item that consists of a panel which includes a checkbox, a textbox and a button: [^]
The problem is that I don't know how to manage those dinamically generated items. For instance, if I delete one item by pressing that X button (I call the method Dispose on DeleteButton_Click ), how could I arrange the remaining items in order to be one after another? See this screenshot: [^]
Any advice?
modified 11-Feb-18 19:32pm.
|
|
|
|
|
How about trying the FlowLayoutPanel in the Toolbox.
|
|
|
|
|
Calling Dispose doesn't remove anything from anywhere, it just closes down and releases any resources the object used - which means that just calling Dispose on objects in the display will not only not work, but is very liable to throw an ObjectDisposedException[^] when it gets subsequently looked at.
Start by looking at how you add new objects, because that is where you need to delete them: an object can't delete itself, because it doesn't (or shouldn't) know what container it is held in.
If you add them to a panel:
Item item = new Item();
myPanel.Controls.Add(item); Then you need to remove them from the panel:
myPanel.Controls.Remove(item);
item.Dispose(); But it's going to be similar for all other containers.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, Griff,
Calling 'Dispose on a WinForm Control will remove it from its container.
However, as you state, that is not best practice.
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Hi Bill!
Thanks for that. It does if you call the bool version (which I didn't know), but it doesn't if you call "vanilla" Dispose :
void IDisposable.Dispose() {
if (hBitmapDC.Handle == IntPtr.Zero || hMetafileDC.Handle == IntPtr.Zero || hBitmap.Handle == IntPtr.Zero) {
return;
}
bool success;
try {
success = DICopy(hMetafileDC, hBitmapDC, destRect, true);
Debug.Assert(success, "DICopy() failed.");
SafeNativeMethods.SelectObject(hBitmapDC, hOriginalBmp);
success = SafeNativeMethods.DeleteObject(hBitmap);
Debug.Assert(success, "DeleteObject() failed.");
success = UnsafeNativeMethods.DeleteCompatibleDC(hBitmapDC);
Debug.Assert(success, "DeleteObject() failed.");
}
finally {
hBitmapDC = NativeMethods.NullHandleRef;
hBitmap = NativeMethods.NullHandleRef;
hOriginalBmp = NativeMethods.NullHandleRef;
GC.SuppressFinalize(this);
}
}
Reference Source[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, Griff,
Strange, I just checked in VS 2017, FrameWork 4.7.1, and the vanilla no-param version did remove the Control.
TextBox tbx = new TextBox { Text = "wtf" };
this.Controls.Add(tbx);
var c1 = this.Controls.Count;
tbx.Dispose();
var c2 = this.Controls.Count;
bool isnull = tbx == null;
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
So the reference sources aren't the source code to .NET? Ouch!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, this is what I see:
=public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize((object) this);
} Maybe source is different for Thailand ?
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Or I misread it all ... equally possible!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hypothesis: the 'Dispose source you looked at is different from the 'Dispose source in the 'ComponentModel class.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
In the last ten years, I think I've written about ten variations on this type of control
Key point: Controls added to a ControlCollection and docked 'Top followed by the use of 'BringToFront on the Control will be added beneath other controls. When one of these controls is removed/deleted, the other Controls will rearrange so that the order is maintained. I suggest:
0. in the 'ToDo UserControl: expose only the UI elements through public properties initialized in the constructor.
1. make a second UserControl called 'ToDoContainer:
a. put a Panel, docked 'Top, in it to hold UI elements that affect instances of the 'ToDo controls, like a Button to add another ToDo.
b. add a second Panel to 'ToDoContainer, docked 'Fill, with 'AutoScroll 'true.
When the add Button on the top Panel is clicked:
a. create the ToDo instance:
a.1. set event handlers for the ToDo close-button Click, and checkbox CheckStateChanged, events.
a.2. add the new ToDo to the controls of the second Panel.
a.3. call the 'BringToFront method on the new ToDo: this will add it to the bottom of any other ToDos displayed in the Panel.
Of course, you will probably want to expose Events in the container for external consumers of your code, like: ToDo added, ToDo deleted, ToDo activated/de-activated, etc.
And, some nice features to add are:
1. highlight the current/selected/active ToDo visually
2. use color in some way to indicate priority
3. implement keyboard handling to navigate in ToDo collection.
4. implement sorting of ToDos by criteria like date, priority, etc.
Suggestion: by making add and close/delete ToDo a result of the user using the top panel controls, you can keep the ToDo control itself simpler.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
modified 12-Feb-18 8:02am.
|
|
|
|
|
Thanks for this useful post. However I don't understand this:
Quote: a.1. set event handlers for the ToDo close-button Click, and checkbox CheckStateChanged, events.
Where should I put these event handlers: in the ToDoContainer or in the Item usercontrol? On a side note, I'm confused about how could I transmit the events from a usercontrol back to the MainForm (e.g. if I click a Delete button from usercontrol how could I notify the MainForm about this?).
PS: I uploaded the VC solution so far if you want to take a look: download[^].
modified 12-Feb-18 10:42am.
|
|
|
|
|
I think you're doing fine. When a Control manages/hosts a set of other Controls, my preference is to keep the hosted controls (your 'Item) as "dumb" as possible, and to centralize their management by code in the container Control. So the 'Item might look like this:
public ToDo()
{
InitializeComponent();
CbxDone = cbxDone;
LblID = lblId;
BtnClose = btnClose;
TxtBx = textBox1;
}
public CheckBox CbxDone { set; get; }
public Label LblID { set; get; }
public Button BtnClose { set; get; }
public TextBox TxtBx { set; get; } In the host container the 'Add button handler might look like this:
private void btnAdd_Click(object sender, EventArgs e)
{
ToDo todo = new ToDo();
currentToDos.Add(todo);
currentToDo = todo;
todo.LblID.Text = (idCount).ToString();
idCount++;
todo.Enter += TodoOnEnter;
todo.Leave += TodoOnLeave;
todo.BtnClose.Click += BtnCloseOnClick;
todo.CbxDone.CheckStateChanged += CbxDoneOnCheckStateChanged;
todopanel.Controls.Add(todo);
todo.Dock = DockStyle.Top;
todo.BringToFront();
this.ActiveControl = todo;
} When you close one of the hosted controls"
currentToDos.Remove(currentToDo);
this.todopanel.Controls.Remove(currentToDo);
currentToDo.Dispose();
this.Invalidate(); I would keep track of the hosted controls in several ways: I might keep separate lists for completed tasks, incomplete tasks, selected tasks, and, I might even not destroy tasks, but either disable them while leaving them visible), or keep the ones removed from the display list in a list for future use. Your choices will depend on what you want your app to do, how you want to preserve the state of the controls and their data when the app closes, etc.
In production code, I would be concerned about making the hosted controls only accessible to the host, through design-time attributes like:
[DesignTimeVisible(false)]
public partial class ToDo : UserControl
And other techniques.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|