|
I am trying to set the caret at the end of a textbox so it scrolls up as new lines get added. These are added by:
this.textBox1.Text += ThisText + System.Environment.NewLine;
However even though the value of this.textBox1.SelectionStart is correct (i.e. a large number), when I get to these lines:-
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.Refresh();
The textbox doesn't scroll.
Can someone tell me what I am missing?
|
|
|
|
|
As easy way to do this is to just call
textBox1.AppendText(ThisText + Environment.NewLine);
Stop putting this on everything. You don't need it as it's already implied.
|
|
|
|
|
|
Dave Kreskowiak wrote: Stop putting this on everything. That was my idea to remind him where the control lives (he was creating a new form and lost the plot)
Implied can be confusing the this newbie, thought it might help.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
|
This is a swamp I have waded in, before.
The cheap solution: put code to update the TextBox caret in the Form's 'Shown event handler.
But, if you want to make the TextBox as independent as possible of its container, and, assuming:
1. this is a WinForm app with a TextBox set to show a vertical scrollbar
2. you will set the Text property at design time, or in the 'Load event of its container.
3. when the Form is shown, you want the TextBox scrolled to the end, and the TextBox selected/focused so if you start typing, what you type is added to the TextBox:
using System;
using System.Windows.Forms;
namespace YOURNAMESPACE
{
public partial class AutoScrolledTbx : UserControl
{
public AutoScrolledTbx()
{
InitializeComponent();
}
public AutoScrolledTbx(string txt)
{
textBox1.Text = txt;
this.textBox1.Select(this.textBox1.TextLength + 1, 0);
this.textBox1.ScrollToCaret();
this.textBox1.Capture = true;
this.textBox1.Focus();
}
private void AutoScrolledTbx_Load(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)this.ForceScroll);
}
private void ForceScroll()
{
this.Invoke(
(MethodInvoker)delegate
{
this.textBox1.Select(this.textBox1.TextLength + 1, 0);
this.textBox1.ScrollToCaret();
this.textBox1.Focus();
ActiveControl = this.textBox1;
}
);
}
public void AppendText(string txt)
{
this.textBox1.AppendText(txt);
}
}
} Discussion:
When a Form is being 'Loaded, then displayed, Controls may not appear in an expected state. By pushing a task onto the UI thread using Marc Gravell's technique shown here, you have achieved the equivalent of executing the update code in the Form's 'Shown event.
In case you wonder why a UserControl is used here, rather than a sub-classed TextBox: a TextBox offers no 'Load or other initialization event to manipulate.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 24-Feb-19 10:28am.
|
|
|
|
|
Hi experts, I am a student and trying to learn to display bitcoin and cryptocurrency price via an API from CoinGecko API[^]
How do I accomplish that in C#? Is Net Http or some library where i should be starting?
Thanks you
|
|
|
|
|
Please don't repost if your message does not appear immediately - both of these were sent by the automated system to moderation as "possible spam" and required a human decision to "let through" or "ban the spammy poster".
When you post multiple times, it increases the chances that you are taken as a spammer, and means that I have to chase down the duplicates and tidy up after you.
I've deleted the "spare".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Go to that page, scroll down, download the .NET client as an example.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I am trying to convert a piece of PHP cURL code to C# using the WebClient but I can't figure out how to implement the CurlFile parameter from the array.
Here is the original PHP code:
$post_data = array('password'=>'pswd',
'username'=>'usr',
'textfile'=>new CURLFile('C:\data_stats.txt'),
);
$curl_obj = curl_init('https://www.website.com');
curl_setopt($curl_obj, CURLOPT_POST, true);
curl_setopt($curl_obj, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl_obj);
And there is the C# code I came up with (I managed to send the password and username parameters but I don't know how to send the file with the key 'textfile' along with those parameters)
using (var client = new WebClient())
{
var post_data = new NameValueCollection();
post_data["password"] = "pswd";
post_data["username"] = "usr";
var response = client.UploadValues("www.website.com", post_data);
}
|
|
|
|
|
With the WebClient class, it's far from simple. There's an example of how to do it here:
Multipart Form Post in C# - Brian Grinstead[^]
If you can use the HttpClient class[^] instead, things are much easier:
var client = new HttpClient();
using (var content = new MultipartFormDataContent())
{
content.Add(new StringContent("pswd"), "password");
content.Add(new StringContent("usr"), "username");
content.Add(new StreamContent(File.OpenRead("C:\\data_stats.txt"), "textfile");
using (var response = await client.PostAsync("https://www.website.com", content))
{
response.EnsureSuccessStatusCode();
string data = await response.Content.ReadAsStringAsync();
...
}
}
Or, as Brian's blog suggests, you could use RestSharp[^] instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have an application which starts with:
public Form1()
{
InitializeComponent();
form = this;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = "Initialising.....";
this.Show();
Refresh();
When I run it from within VS the form appears as expected.
When I publish the app, install it and then run I never see the form.
Changing this.Show() to Form1.Show() gives error CS0120.
Can anyone help?
|
|
|
|
|
Why on earth are you doing a Show and a Refresh in Form_Load? The form is drawn for the first time AFTER your Form_Load event handler returns. There's nothing to show yet and there's nothing to refresh!
Remove those two lines of code:
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = "Initialising.....";
}
You also spelled "Initializing" wrong.
|
|
|
|
|
|
Hello All:
I'm building a C# Windows app using VS 2013. In it, we need to create invoices. These invoices have disparate data and there are comparison decisions to be made to know what data to display and what not to display.
In detail, we show the customer info in the header so that has to be part of the data set. In the body, we show a list of products with their prices, grouped by product categories using a table. So far so good. I've got two data tables in the data set, one for the "static" data in the header and one for the dynamic data in the body.
Where I'm getting hung up is the last part of the invoice where everything is totaled up. Part of the decision is that if there is a minimum fee assigned to the customer that has to be compared to the totaled fee and then whichever is greater needs to be displayed.
I'm sure the best way to do that is not in the report itself but in the data set building process. So far I'm using data tables for each "section" of the report and adding those to the data set.
My question is, is there a better way? Should I create and use an "Invoice" object instead? If so, that object would have to have a few properties of the List<dictionary<string, object="">> type. Would the report be able to interpret that?
I'm just wondering what the best way to go about this is.
Thanks in advance.
|
|
|
|
|
Put a "total" field in the "header" (table), and use that for summing the details (before reporting).
Copy the "old" table to a new table / datatable that has the required fields if that's what's required (using SQL or whatever).
The "total" / summary calculations from the header (table) then go in the header or "footer" of the report.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Thanks very much for taking the time Gerry.
What I ended up doing was creating a method in my utility class that returns a dataset. Inside that method it does all of the calculations and decision making. That dataset is then used as the data source for the report. So far it works well but there is more to be done so we'll see.
Thanks again.
|
|
|
|
|
I'm having trouble getting a textBox to accept a string from within a method.
Start with:-
private static Form1 form = null;
public Form1()
{
InitializeComponent();
form = this;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = "Initialising.....";
this.Show();
Refresh();
textBox2 shows up OK. However
form.UpDateText("Checking validity of " + FileName);
and
public void UpDateText(string ThisText)
{
using (StreamWriter logfile = new StreamWriter("D:\\TeklaZipping.log", true))
{
logfile.WriteLine(ThisText);
logfile.Close();
}
Form1 frm1 = new Form1();
frm1.textBox1.Text += ThisText + System.Environment.NewLine;
frm1.Refresh();
}
runs, and writes to logfile, but doesn't update textBox1.
Any help appreciated.
ormonds
|
|
|
|
|
It is updating textBox1 in a new instance of frm1 . Try something like below;
this.textBox1.Text += ThisText + System.Environment.NewLine;
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
ormonds wrote: Form1 frm1 = new Form1(); This creates a new instance of Form1 and updates the textbox on the new instance. Remove the line and change frm1.textbox1 to this.textbox1 (this is not required but will help you to identify the source of the control).
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
|
I read the daily CodeProject news, and came this article:
tps:
It is about C# finally getting multiple inheritance, kind of like C++, albeit through interfaces
I am very excited for this.
But when will this come out for say VS2017?
And I am referring to something like an official patch or update and/or maybe the next Visual Studio.
The article goes int details that this feature will only be for CoreCLR, which I've not tried yet.
Is there any news on it going to regular .NET Framework?
Also, how will custom attributes work with default interface methods?
This is something I've wanted since C#'s inception. It will greatly improve code STABILITY, re-useability and sharing, even more so than extension methods. I only wish C# would go full bore multiple inheritance so we can have multiple constructors getting launched.
But before going, I really want to just harp on my statement about STABILITY.
I can truly say that for 20+ years of C++ development, with multiple inheritance, I never ONCE even slightly encountered the "Diamond of Death". It was never an issue. C++ multiple inheritance allowed code to be re-used, over and over and over again, which meant, code got more and more tested as time progressed. that translated to better and more STABLE code.
When C# prevented multiple inheritance, and especially in the few years before entension methods, code in C# was cut/copy/pasted over and over and over again, causing old code with problems, to get cloned in their BAD-STATE, and then re-used for all time, until someone came out and hunted each and every instance of the clones down, and got rid of them. This cloning syndrome went on until extension methods came out, which was pretty much from 2001 till 2010.
But even after 2010, in my experience, there were so many people still harping about the "Diamond of Death", that they actually gimped development companies, to not allow extension methods. These ~people~ forced all development to have to cut/copy/paste code, over and over again, even if it was known to not be optimal and/or maybe even out right have BUGS.
|
|
|
|
|
Member 10474771 wrote: It is about C# finally getting multiple inheritance, kind of like C++, albeit through interfaces
I am very excited for this. No, it is not. You can already assign multiple interfaces to a class.
It is about duck-typing interfaces, and how this creates a multiple-inheritence problem.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
WHAT?? Did you really read the article or just skim it?
No, we're NOT getting the ability to inherit from multiple classes.
We're getting default implementations of interfaces so you don't have to copy the same method implementation into all of the classes that use the interface.
It DOES NOT SOLVE the "Diamond of Death" problem, but, instead, will INTRODUCE IT into interface inheritance.
|
|
|
|
|
Eddy is right: it doesn't do any such thing. Read it more carefully, and you will notice this bit:
Quote: It means that a class doesn’t inherit members from the interfaces it implements It just allows a default implementation so interfaces can be expanded without breaking the existing codebase: those default methods cannot be referenced on implementing classes unless they are either specifically implemented, or the instance is cast as the interface type.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|