|
Hi,
I want to write a code like this that runs perfectly in WinForms:
int count = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
if (((Label)c).BackColor == Color.Red)
count++;
}
But in Xamarin, I cannot use Control class. There is no namespace for it. How can I do the same in Xamarin?
|
|
|
|
|
Your WinForms code would be better written as:
int count = 0;
foreach (Label l in this.Controls.OfType<Label>())
{
if (l.BackColor == Color.Red)
{
count++;
}
} Or simply:
int count = this.Controls.OfType<Label>().Count(l => l.BackColor == Color.Red);
Xamarin forms doesn't have a similar concept. You might be able to do something similar by walking the visual tree - eg:
the urban canuk, eh: VisualTreeHelper for Xamarin.Forms[^]
However, it would almost certainly be better to use MVVM concepts to achieve your goal.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I'm working on Xamarin project. I want to pass a parameter from one page to another.
I used this code for one of pages:
public TabbedMainPage(string parameter)
{
InitializeComponent();
On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
On<Android>().SetIsSmoothScrollEnabled(false);
lbl01.Text = parameter;
}
App.xaml.cs:
public App()
{
InitializeComponent();
MainPage = new TabbedMainPage();
}
I have error under
TabbedMainPage in App.xaml.cs
How can I solve that error?
|
|
|
|
|
MainPage = new TabbedMainPage( <parameter> );
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
|
|
|
|
|
I wrote MainPage = new TabbedMainPage(<parameter>);
But it has error under <parameter>);
|
|
|
|
|
You're supposed to supply your "own" parameter; the <parameter> is a "place holder"; you don't actually enter it.
Why did you define a parameter in your constructor when you don't know how to use it?
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
|
|
|
|
|
I tried it. My parameter name is "parameter". I used MainPage = new TabbedMainPage(parameter);
but it has error under parameter
|
|
|
|
|
Where did you "define" parameter? Since it's a string, you could use a literal; e.g. "xxx". (Don't code "xxx"; it's just an example).
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
|
|
|
|
|
It would be a good idea to actually say what the error is. That might help people to help you.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You have defined your constructor as public TabbedMainPage(string parameter) , so it is expecting a string, which you then post into lbl01.Text . So you need something like:
public App()
{
InitializeComponent();
MainPage = new TabbedMainPage("This is the text that will appear in lbl01");
}
|
|
|
|
|
I solved it.
private async void Button_Clicked(object sender, EventArgs e)
{
var result = await Navigation.ShowPopupAsync(new popup01());
lbl01.Text = result.ToString();
}
|
|
|
|
|
That is a different issue.
|
|
|
|
|
Hello everybody,
I have this using ASP.NET
<asp:table id="Table1" runat="server" font-size="Small" width="100%">
It is create a table (of course), like this:
Group Pounds Credits....
xxxxxxxx xx,xx xx,xxx.xx
Total by group: xxx xxxxxx
This is Dynamic table
Any control a windforms?
GridView?
Any help?
|
|
|
|
|
|
Hello everyone,
Having a difficult time adding code to reject non numeric user inputs into text boxes. Can someone please view the following code and give me a path forward? Anything I've tried has resulted in errors.
<
private void btnCalculate_Click(object sender, EventArgs e)
{
double dblPresentValue = 0;
double dblSalvageValue = 0;
int intYearsOfDepreciation = 0;
int intCounter = 0;
string strDisplay = "";
txtDisplay.Text = "";
dblPresentValue = Convert.ToDouble(txtPresentValue.Text);
dblSalvageValue = Convert.ToDouble(txtSalvageValue.Text);
intYearsOfDepreciation = Convert.ToInt32(txtYearsOfDepreciation.Text);
if (dblPresentValue < 1 | dblPresentValue > 1000000)
{
MessageBox.Show("Present value must be between $1 and $1,000,000");
txtPresentValue.Select();
txtPresentValue.SelectAll();
}
else if (dblSalvageValue > dblPresentValue)
{
MessageBox.Show("Salvage value cannot be greater than Present Value");
txtSalvageValue.Select();
txtSalvageValue.SelectAll();
}
else if (intYearsOfDepreciation < 0 | intYearsOfDepreciation > 25)
{
MessageBox.Show("Years of Depreciation must be in 1 - 25 range");
txtYearsOfDepreciation.Select();
txtYearsOfDepreciation.SelectAll();
}
else
{
strDisplay = "Year Asset Value\r\n------- ---------------";
double period = (dblPresentValue - dblSalvageValue)/intYearsOfDepreciation;
int curYear = DateTime.Now.Year;
while (intCounter < intYearsOfDepreciation)
{
strDisplay += "\r\n"+ curYear.ToString() + ": " + dblPresentValue.ToString("C2");
intCounter = intCounter + 1;
dblPresentValue -= period;
curYear++;
}
txtDisplay.Text = strDisplay;
}
}
|
|
|
|
|
First off, stop using Convert functions on user input - they are designed to throw an exception if the value is not numeric, and that means when your user misstypes your app crashes.
Use the TryParse methods instead, and exit the function when it fails:
double presentValue;
if (!double.TryParse(txtPresentValue.Text, out presentValue))
{
MessageBox.Show("Present value must be a numeric value");
return;
} Then you can add your validations, and again return if they do not succeed.
A better way is to use a NumericUpDown control instead of a textbox as the user can't enter invalid numbers into them at all...
"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!
|
|
|
|
|
This worked great, appreciate the help!
|
|
|
|
|
You're welcome!
"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!
|
|
|
|
|
Hello,
I have a problem with my application at the time of printing. When I print with the print button of the report viewer in the viewer my printing works fine. And when I run printing without previewing the report limit is limited to where the rectangle is limited. I specify well I use a thermal printer in roll paper
Here is my code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Reporting.WinForms;
namespace POS_System_Inventory
{
public static class LocalReportExtensions
{
public static void PrintToPrinter(this LocalReport report)
{
PageSettings pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;
Print(report, pageSettings);
}
public static void Print(this LocalReport report, PageSettings pageSettings)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
var streams = new List<Stream>();
var pageIndex = 0;
report.Render("Image", deviceInfo,
(name, fileNameExtension, encoding, mimeType, willSeek) =>
{
MemoryStream stream = new MemoryStream();
streams.Add(stream);
return stream;
}, out warnings);
foreach (Stream stream in streams)
stream.Position = 0;
if (streams == null || streams.Count == 0)
throw new Exception("Aucun flux à imprimer.");
using (PrintDocument printDocument = new PrintDocument())
{
printDocument.DefaultPageSettings = pageSettings;
if (!printDocument.PrinterSettings.IsValid)
throw new Exception("Impossible de trouver l'imprimante par défaut.");
else
{
printDocument.PrintPage += (sender, e) =>
{
Metafile pageImage = new Metafile(streams[pageIndex]);
Rectangle adjustedRect = new Rectangle(e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height);
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
e.Graphics.DrawImage(pageImage, adjustedRect);
pageIndex++;
e.HasMorePages = (pageIndex < streams.Count);
e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
};
printDocument.EndPrint += (Sender, e) =>
{
if (streams != null)
{
foreach (Stream stream in streams)
stream.Close();
streams = null;
}
};
printDocument.Print();
}
}
}
}
}
modified 22-Jun-21 8:24am.
|
|
|
|
|
Repost: this is the same problem you asked about 3 days ago: Printe page limit - C# Discussion Boards[^]
Don't repost the same question repeatedly - it just wastes time and effort!
"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!
|
|
|
|
|
Looking at your code with a very limited understanding of the domain, I am wondering if
Member 14192216 wrote:
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>"; is meant to be an interpolated string. If so, try changing the @"..." to $@"..." so that the expressions inside {...} sections are evaluated. It may be that the uninterpolated values are being treated as invalid number, which are treated a undefined items so the defaults for the geometry are used.
|
|
|
|
|
You're confusing "logical" printing and "physical" printing.
There is no evidence that the "thermal printer" understands what you're sending it. (thermal printers can have their own "language")
You start by sending "one line" / chaeacter to the printer. When that works, you'll have some confidence you're on the right track; otherwise, you're just grasping.
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
|
|
|
|
|
|
I only found "commercial" "high performance" charting software. For open-source, it's not a priority.
And then you need to choose Windows Forms and / or WPF, etc.
If it is static or dynamic (I needed dynamic for a client). With static you could use a (WPF/UWP) ViewBox for scrolling.
For about $1000, you get to do less thinking about it.
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
|
|
|
|
|
@honeythecodewitch
Check out honeythecodewitch's work on IOT controller graphics: [^].
I'd say she is the right person on CP to advise you.
«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
|
|
|
|