|
Since you create a number of textboxes (which is probably a poor idea - have a look at a DataGridView or similar instead, it does most of the work for you) you will have to search for them (or use the inputTextBoxes collection you added them to).
Searching is relatively simple - they are in the form Controls array along with all the others - but I'd just look in your collection! Just move it to a private class level variable and access it in your Click event handler.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi Antony, and welcome to CodeProject !
I think you are using a strategy that is far too complex here.
Why not create one UserControl that has both a Label, and TextBox; then create multiple instances of that UserControl as needed ?
In the UserControl try setting the AutoSize property to 'true on the Label, and its Dock property to 'Left; set the TextBox 'Dock property to 'Fill. That way you can avoid the business your doing now to measure the width of the string.
Within the UserControl, you can expose the TextBox by creating a Public property that references the contents of the TextBox, or exposing the TextBox TextChanged Event.
I like to use "lazy-load" techniques, so I might write something like this in the UserControl:
private string _currentText;
public string CurrentText
{
set
{
_currentText = value;
textBox1.Text = value;
}
get { return textBox1.Text; }
}
As you create the instances of the UserControl, you can, at the same time, create a generic List or Array to store either the TextBox itself or its Text data. Then, when your Button is pressed, you can just iterate over the reference to stored values, or data, and get what you need.
You could use Linq to make an IEnumerable<string>, containing all the strings in all the TextBoxes, or whatever else you need. Assuming I used the string Property shown above, and I kept a List named 'LblTbList<LabelAndTextUserControl> of the Custom User Controls in the Form where they created"
public IEnumerable<string> GetData()
{
return LblTbList.Select(itm => itm.CurrentText);
} That function would return an IEnumerable containing all the strings in all the TextBoxes.
And the code in the context that "consumes" the data (the code your showing using 'ShowDialog) can then iterate over that, or whatever.
private void button2_Click(object sender, EventArgs e)
{
foreach (string txt in YourTestForm.GetData().ToList())
{
Console.WriteLine(txt);
}
} cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
I have this code:
TimelineStart = new DateTime(2015, 08, 11, 10, 00, 00);
TimelineEnd = new DateTime(2015, 08, 14, 20, 00, 00);
TotalHours = (TimelineEnd - TimelineStart).TotalHours;
Day 1 = 10 Hours
Day 2 = 24 Hours
Day 3 = 24 Hours
Day 4 = 20 Hours
TOTAL 78 Hours
The TimeSpan is showing TotalHours = 82.
What's wrong here?
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: Day 1 = 14 Hours
Day 2 = 24 Hours
Day 3 = 24 Hours
Day 4 = 20 Hours
TOTAL 82 Hours
FTFY!
from 10 am to midnight there are 14 hours of day 1 in the total elapsed time!
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
That's wrong.. From 12 am to 10 am is only 10 hours. 10 hours have elapsed.
It's not 10 am until 12 midnight.
If it's not broken, fix it until it is
|
|
|
|
|
You're right in your assertion, but you're wrong in your assumption. The Timespan of day 1 is from 10 AM to Midnight - or 14 hours.
|
|
|
|
|
If I ask you what time it is, and you say "It's 10 am", you're saying "10 hours have past since midnight".
Assuming you're right, and the TimeSpan class represents it as "10 AM to Midnight", then the TimeSpan class is implemented stupidly. Why then is the "Day 4 = 20 Hours", correct? According to what you said, that should be 4 hours.
Time is not "Until midnight". It's "Time elapsed".
That makes no sense at all.
If it's not broken, fix it until it is
|
|
|
|
|
See this diagram:
Day 1 (11-Aug) | Day 2 (12-Aug) | Day 3 (13-Aug) | Day 4 (14-Aug) |
10am - midnight | whole day | whole day | midnight to 8pm |
14 hrs | 24 hrs | 24 hrs | 20 hrs |
Time interval = 14 + 24 + 24 + 20 = 82 hours
/ravi
|
|
|
|
|
Okay, stop thinking that your computer and the rest of us are out to mess with your mind. Let's take this from a none computer perspective and that should make things simpler - imagine you're 7 years old and it's 10 O'Clock on Christmas Eve. How many hours are there until Christmas Day starts? Now, suppose you're told that you can't open your presents until 4PM on Christmas Day. How many hours do you have to wait from the 10 O'Clock time until you can open your presents?
That's all that's going on.
|
|
|
|
|
I understand what you're saying, I just think the implementation is wrong
If it's not broken, fix it until it is
|
|
|
|
|
I don't think the implementation is wrong at all. On the contrary, it seems that your comprehension of the mathematics is what is wrong. If you are interested in computing an elapsed period of time, then that is exactly what is happening.
Take this for example. If you started the clock at 10AM yesterday and stopped it at 10AM today then a total of 24 hours has passed. Yes? The 24 hours is easily broken down in to 10 hours TODAY and 14 hours YESTERDAY. So a time which has elapsed takes the last fraction of the day your started and the first fraction of the day you finished.
|
|
|
|
|
I see you got it now. This was the reply I was writing to your deleted post.
----
How many hours are there between Mon 11:00pm and Tue 2:00am? 3, right?
Even though I said 11:00pm (which is the 23rd hour of the day), there's only one hour left in that day. THAT's the start of the time interval. So you end up with 1 hr left on Mon and 2 hours elapsed on Tue, giving us a total of 3 hours.
----
/ravi
|
|
|
|
|
It's not that I don't get it... I just don't agree with the implementation. TimeSpan is starting from 10 and counting forward, whereas in real life we consider time as time past.
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: TimeSpan is starting from 10 and counting forward Yes. But wouldn't you do the same when computing a time interval? For the first day, you'd count the hours left (not elapsed) and for all successive days you'd count the hours elapsed, right? Hence, Mon 10pm to Tue 1am would be a time interval of 3 hours, right?
/ravi
|
|
|
|
|
The TimeSpan isn't counting forward or backward from any specific time.
It is representing (in this case) the elapsed time from one DateTime to another.
It has no concept of forward from "10 am".
If you used an hourglass to count time from 10 am Aug 11 until 8pm Aug 14, you would just be turning the glass over for the 82nd time at the 8pm Aug 14. The TimeSpan is just representing the amount of sand that flowed through neck of the hourglass (with much more precision than counting each grain of sand!!)
It is a signed difference.
If you did the subtraction in the other order it would return -82 hours.
It is just a counter.
[Edit]
var TimelineStart = new DateTime(2015, 08, 11, 10, 00, 00);
var TimelineEnd = new DateTime(2015, 08, 14, 20, 00, 00);
TimeSpan delta = TimelineEnd - TimelineStart;
Debug.WriteLine(TimelineEnd.Ticks);
Debug.WriteLine(TimelineStart.Ticks);
Debug.WriteLine(delta.Ticks);
gives the output:
635751792000000000
635748840000000000
2952000000000
The DateTime items are counters forward from a reference date and time.
The TimeSpan is just representing the difference.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
modified 17-Aug-15 13:20pm.
|
|
|
|
|
Consider a simpler example:
TimelineStart = new DateTime(2015, 08, 11, 10, 00, 00);
TimelineEnd = new DateTime(2015, 08, 11, 11, 00, 00);
TotalHours = (TimelineEnd - TimelineStart).TotalHours;
Subtracting two DateTime instances gives you a TimeSpan representing the difference between the two points on the timeline - ie: how many hours are there between 10:00 and 11:00 on 11th August 2015.
With your approach to the problem, TotalHours would contain 21 . Unless you live next to a black hole, there aren't 21 hours between 10:00 and 11:00 on the same day.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm using VS Community 2013 and I'd like to start a VSTO (Excel) project. However, I don't find any templates listed, local or online.
|
|
|
|
|
|
I have a form and want to display formatted text. I use a RichTextBox control. But I want windows message such as mouse clicks on the text to be handled by the parent form. So I disable the control. But that changes the background colour to grey, which is unacceptable as I want to set the background colour. Okay, so I override the WndProc method of the RichTextBox and use SendMessage to send messages such as mouse double click to the parent. That works, the form WndProc DOES receive the double click. But, the form's handler for a mouse double click is not invoked. Why is this? Does the system invoke Form event handlers instead of WndProc?
Note that the mouse bouble click message includs the client position in the lParam arg. I changed that from the location in the RichTextBox to the location in the parent form.
SOLVED
The problem is how to make a RichTextBox transparent to mouse events, so that it does no more than draw text on screen. Disabling the richtextbox sets a fixed background colour, which is not what I want.
The solution is to handle mouse events from the richeditbox in the form. Then in each event handler call Form.OnXXXXXX method e.g. Form.OnDoubleClick().
So in the end this is actually quite simple!
Thanks all for comments.
modified 14-Aug-15 11:03am.
|
|
|
|
|
You actually don't need to override WndProc callback in your RichEdit control class. Really, You have to declare and send a notify message as follows:
#define EN_MYMSG1 (WM_USER + 200)
NMHDR nmhdr;
::ZeroMemory((LPVOID)&nmhdr, sizeof(NMHDR));
nmhdr.hwndFrom = hRichEditWnd;
nmhdr.idFrom = IDC_MYRICHEDIT;
nmhdr.code = EN_MYMSG;
::SendMessage(hWndView, WM_NOTIFY, (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
And then, process this message by handling WM_NOTIFY in your parent window class using whatever predefined MFC message map macros or overriding WndProc callback method:
ON_NOTIFY(EN_MYMSG, IDC_MYRICHEDIT, OnMyMessage) or
ON_NOTIFY_REFLECT(EN_MYMSG, OnMyMessage)
afx_msg void OnMyMessage(NMHDR* pnmhdr , LRESULT* pResult);
void CMyWnd::OnMyMessage(NMHDR* pnmhdr, LRESULT* pResult)
{
if (pnmhdr->code == EN_MYMSG)
{
}
*pResult = 0;
}
LRESULT CALLBACK CMyWnd::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_NOTIFY:
NMHDR* pnmhdr = &(NMHDR)message.GetLParam(typeof(NMHDR));
if (pnmhdr->code == EN_MYMSG)
{
}
break;
default: break;
}
return WndProcDefault(hWnd, uMsg, wParam, lParam);
}
That's all
modified 14-Aug-15 8:19am.
|
|
|
|
|
That doesn't look like C# to me!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Maybe it is .Net 5.
|
|
|
|
|
Besides, this code has been written using C++ MFC, but you can easily convert it into C#.NET
|
|
|
|
|
This code is just a prototype and describes the basic concept of notify message handling, it's no matter you implement it in C++ or C#. The notify message handling works similar in either C#.NET or C++ MFC. The main post of this post is the concept.
|
|
|
|
|
Sadly I don't think it does do what I need. Basically I want the RichTextBox to display text, with the correct font, bold, background colour, etc, but to be transparent to mouse events such as double click.
I am able to intercept mouse double clicks in a class derived from RichTextBox, and SendMessage them to the parent form. They are passed in to the parent form WndProc. But for some reason they do not invoke the MouseDoubleClick event handler. I tried overriding the Form.WndProc, but could not invoke MouseDoubleClick, for reasons I understand.
|
|
|
|
|